001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Map;
030import java.util.Objects;
031import java.util.Optional;
032import java.util.Set;
033import java.util.TreeMap;
034import java.util.TreeSet;
035import java.util.function.Function;
036import java.util.regex.Matcher;
037import java.util.regex.Pattern;
038import org.apache.hadoop.hbase.Coprocessor;
039import org.apache.hadoop.hbase.HConstants;
040import org.apache.hadoop.hbase.TableName;
041import org.apache.hadoop.hbase.exceptions.DeserializationException;
042import org.apache.hadoop.hbase.security.User;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.yetus.audience.InterfaceAudience;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
050
051/**
052 * Convenience class for composing an instance of {@link TableDescriptor}.
053 * @since 2.0.0
054 */
055@InterfaceAudience.Public
056public class TableDescriptorBuilder {
057  public static final Logger LOG = LoggerFactory.getLogger(TableDescriptorBuilder.class);
058  @InterfaceAudience.Private
059  public static final String SPLIT_POLICY = "SPLIT_POLICY";
060  private static final Bytes SPLIT_POLICY_KEY = new Bytes(Bytes.toBytes(SPLIT_POLICY));
061  /**
062   * Used by HBase Shell interface to access this metadata
063   * attribute which denotes the maximum size of the store file after which a
064   * region split occurs.
065   */
066  @InterfaceAudience.Private
067  public static final String MAX_FILESIZE = "MAX_FILESIZE";
068  private static final Bytes MAX_FILESIZE_KEY
069          = new Bytes(Bytes.toBytes(MAX_FILESIZE));
070
071  @InterfaceAudience.Private
072  public static final String OWNER = "OWNER";
073  @InterfaceAudience.Private
074  public static final Bytes OWNER_KEY
075          = new Bytes(Bytes.toBytes(OWNER));
076
077  /**
078   * Used by rest interface to access this metadata attribute
079   * which denotes if the table is Read Only.
080   */
081  @InterfaceAudience.Private
082  public static final String READONLY = "READONLY";
083  private static final Bytes READONLY_KEY
084          = new Bytes(Bytes.toBytes(READONLY));
085
086  /**
087   * Used by HBase Shell interface to access this metadata
088   * attribute which denotes if the table is compaction enabled.
089   */
090  @InterfaceAudience.Private
091  public static final String COMPACTION_ENABLED = "COMPACTION_ENABLED";
092  private static final Bytes COMPACTION_ENABLED_KEY
093          = new Bytes(Bytes.toBytes(COMPACTION_ENABLED));
094
095  /**
096   * Used by HBase Shell interface to access this metadata
097   * attribute which denotes if the table is split enabled.
098   */
099  @InterfaceAudience.Private
100  public static final String SPLIT_ENABLED = "SPLIT_ENABLED";
101  private static final Bytes SPLIT_ENABLED_KEY = new Bytes(Bytes.toBytes(SPLIT_ENABLED));
102
103  /**
104   * Used by HBase Shell interface to access this metadata
105   * attribute which denotes if the table is merge enabled.
106   */
107  @InterfaceAudience.Private
108  public static final String MERGE_ENABLED = "MERGE_ENABLED";
109  private static final Bytes MERGE_ENABLED_KEY = new Bytes(Bytes.toBytes(MERGE_ENABLED));
110
111  /**
112   * Used by HBase Shell interface to access this metadata
113   * attribute which represents the maximum size of the memstore after which its
114   * contents are flushed onto the disk.
115   */
116  @InterfaceAudience.Private
117  public static final String MEMSTORE_FLUSHSIZE = "MEMSTORE_FLUSHSIZE";
118  private static final Bytes MEMSTORE_FLUSHSIZE_KEY
119          = new Bytes(Bytes.toBytes(MEMSTORE_FLUSHSIZE));
120
121  @InterfaceAudience.Private
122  public static final String FLUSH_POLICY = "FLUSH_POLICY";
123  private static final Bytes FLUSH_POLICY_KEY = new Bytes(Bytes.toBytes(FLUSH_POLICY));
124  /**
125   * Used by rest interface to access this metadata attribute
126   * which denotes if it is a catalog table, either <code> hbase:meta </code>.
127   */
128  @InterfaceAudience.Private
129  public static final String IS_META = "IS_META";
130  private static final Bytes IS_META_KEY
131          = new Bytes(Bytes.toBytes(IS_META));
132
133  /**
134   * {@link Durability} setting for the table.
135   */
136  @InterfaceAudience.Private
137  public static final String DURABILITY = "DURABILITY";
138  private static final Bytes DURABILITY_KEY
139          = new Bytes(Bytes.toBytes("DURABILITY"));
140
141  /**
142   * The number of region replicas for the table.
143   */
144  @InterfaceAudience.Private
145  public static final String REGION_REPLICATION = "REGION_REPLICATION";
146  private static final Bytes REGION_REPLICATION_KEY
147          = new Bytes(Bytes.toBytes(REGION_REPLICATION));
148
149  /**
150   * The flag to indicate whether or not the memstore should be
151   * replicated for read-replicas (CONSISTENCY =&gt; TIMELINE).
152   */
153  @InterfaceAudience.Private
154  public static final String REGION_MEMSTORE_REPLICATION = "REGION_MEMSTORE_REPLICATION";
155  private static final Bytes REGION_MEMSTORE_REPLICATION_KEY
156          = new Bytes(Bytes.toBytes(REGION_MEMSTORE_REPLICATION));
157
158  private static final Bytes REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY
159          = new Bytes(Bytes.toBytes(RegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY));
160  /**
161   * Used by shell/rest interface to access this metadata
162   * attribute which denotes if the table should be treated by region
163   * normalizer.
164   */
165  @InterfaceAudience.Private
166  public static final String NORMALIZATION_ENABLED = "NORMALIZATION_ENABLED";
167  private static final Bytes NORMALIZATION_ENABLED_KEY
168          = new Bytes(Bytes.toBytes(NORMALIZATION_ENABLED));
169
170  @InterfaceAudience.Private
171  public static final String NORMALIZER_TARGET_REGION_COUNT =
172      "NORMALIZER_TARGET_REGION_COUNT";
173  private static final Bytes NORMALIZER_TARGET_REGION_COUNT_KEY =
174      new Bytes(Bytes.toBytes(NORMALIZER_TARGET_REGION_COUNT));
175
176  @InterfaceAudience.Private
177  public static final String NORMALIZER_TARGET_REGION_SIZE = "NORMALIZER_TARGET_REGION_SIZE";
178  private static final Bytes NORMALIZER_TARGET_REGION_SIZE_KEY =
179      new Bytes(Bytes.toBytes(NORMALIZER_TARGET_REGION_SIZE));
180
181  /**
182   * Default durability for HTD is USE_DEFAULT, which defaults to HBase-global
183   * default value
184   */
185  private static final Durability DEFAULT_DURABLITY = Durability.USE_DEFAULT;
186
187  @InterfaceAudience.Private
188  public static final String PRIORITY = "PRIORITY";
189  private static final Bytes PRIORITY_KEY
190          = new Bytes(Bytes.toBytes(PRIORITY));
191
192  /**
193   * Relative priority of the table used for rpc scheduling
194   */
195  private static final int DEFAULT_PRIORITY = HConstants.NORMAL_QOS;
196
197  /**
198   * Constant that denotes whether the table is READONLY by default and is false
199   */
200  public static final boolean DEFAULT_READONLY = false;
201
202  /**
203   * Constant that denotes whether the table is compaction enabled by default
204   */
205  public static final boolean DEFAULT_COMPACTION_ENABLED = true;
206
207  /**
208   * Constant that denotes whether the table is split enabled by default
209   */
210  public static final boolean DEFAULT_SPLIT_ENABLED = true;
211
212  /**
213   * Constant that denotes whether the table is merge enabled by default
214   */
215  public static final boolean DEFAULT_MERGE_ENABLED = true;
216
217  /**
218   * Constant that denotes whether the table is normalized by default.
219   */
220  public static final boolean DEFAULT_NORMALIZATION_ENABLED = false;
221
222  /**
223   * Constant that denotes the maximum default size of the memstore in bytes after which
224   * the contents are flushed to the store files.
225   */
226  public static final long DEFAULT_MEMSTORE_FLUSH_SIZE = 1024 * 1024 * 128L;
227
228  public static final int DEFAULT_REGION_REPLICATION = 1;
229
230  public static final boolean DEFAULT_REGION_MEMSTORE_REPLICATION = true;
231
232  private final static Map<String, String> DEFAULT_VALUES = new HashMap<>();
233  private final static Set<Bytes> RESERVED_KEYWORDS = new HashSet<>();
234
235  static {
236    DEFAULT_VALUES.put(MAX_FILESIZE,
237            String.valueOf(HConstants.DEFAULT_MAX_FILE_SIZE));
238    DEFAULT_VALUES.put(READONLY, String.valueOf(DEFAULT_READONLY));
239    DEFAULT_VALUES.put(MEMSTORE_FLUSHSIZE,
240            String.valueOf(DEFAULT_MEMSTORE_FLUSH_SIZE));
241    DEFAULT_VALUES.put(DURABILITY, DEFAULT_DURABLITY.name()); //use the enum name
242    DEFAULT_VALUES.put(REGION_REPLICATION, String.valueOf(DEFAULT_REGION_REPLICATION));
243    DEFAULT_VALUES.put(NORMALIZATION_ENABLED, String.valueOf(DEFAULT_NORMALIZATION_ENABLED));
244    DEFAULT_VALUES.put(PRIORITY, String.valueOf(DEFAULT_PRIORITY));
245    DEFAULT_VALUES.keySet().stream()
246            .map(s -> new Bytes(Bytes.toBytes(s))).forEach(RESERVED_KEYWORDS::add);
247    RESERVED_KEYWORDS.add(IS_META_KEY);
248  }
249
250  /**
251   * @deprecated namespace table has been folded into the ns family in meta table, do not use this
252   *             any more.
253   */
254  @InterfaceAudience.Private
255  @Deprecated
256  public final static String NAMESPACE_FAMILY_INFO = "info";
257
258  /**
259   * @deprecated namespace table has been folded into the ns family in meta table, do not use this
260   *             any more.
261   */
262  @InterfaceAudience.Private
263  @Deprecated
264  public final static byte[] NAMESPACE_FAMILY_INFO_BYTES = Bytes.toBytes(NAMESPACE_FAMILY_INFO);
265
266  /**
267   * @deprecated namespace table has been folded into the ns family in meta table, do not use this
268   *             any more.
269   */
270  @InterfaceAudience.Private
271  @Deprecated
272  public final static byte[] NAMESPACE_COL_DESC_BYTES = Bytes.toBytes("d");
273
274  /**
275   * <pre>
276   * Pattern that matches a coprocessor specification. Form is:
277   * {@code <coprocessor jar file location> '|' <class name> ['|' <priority> ['|' <arguments>]]}
278   * where arguments are {@code <KEY> '=' <VALUE> [,...]}
279   * For example: {@code hdfs:///foo.jar|com.foo.FooRegionObserver|1001|arg1=1,arg2=2}
280   * </pre>
281   */
282  private static final Pattern CP_HTD_ATTR_VALUE_PATTERN =
283    Pattern.compile("(^[^\\|]*)\\|([^\\|]+)\\|[\\s]*([\\d]*)[\\s]*(\\|.*)?$");
284
285  private static final String CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN = "[^=,]+";
286  private static final String CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN = "[^,]+";
287  private static final Pattern CP_HTD_ATTR_VALUE_PARAM_PATTERN = Pattern.compile(
288    "(" + CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN + ")=(" +
289      CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN + "),?");
290  private static final Pattern CP_HTD_ATTR_KEY_PATTERN =
291    Pattern.compile("^coprocessor\\$([0-9]+)$", Pattern.CASE_INSENSITIVE);
292
293  /**
294   * Table descriptor for namespace table
295   * @deprecated since 3.0.0 and will be removed in 4.0.0. We have folded the data in namespace
296   *   table into meta table, so do not use it any more.
297   * @see <a href="https://issues.apache.org/jira/browse/HBASE-21154">HBASE-21154</a>
298   */
299  @Deprecated
300  public static final TableDescriptor NAMESPACE_TABLEDESC =
301    TableDescriptorBuilder.newBuilder(TableName.NAMESPACE_TABLE_NAME)
302      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(NAMESPACE_FAMILY_INFO_BYTES)
303        // Ten is arbitrary number. Keep versions to help debugging.
304        .setMaxVersions(10).setInMemory(true).setBlocksize(8 * 1024)
305        .setScope(HConstants.REPLICATION_SCOPE_LOCAL).build())
306      .build();
307
308  private final ModifyableTableDescriptor desc;
309
310  /**
311   * @param desc The table descriptor to serialize
312   * @return This instance serialized with pb with pb magic prefix
313   */
314  public static byte[] toByteArray(TableDescriptor desc) {
315    if (desc instanceof ModifyableTableDescriptor) {
316      return ((ModifyableTableDescriptor) desc).toByteArray();
317    }
318    return new ModifyableTableDescriptor(desc).toByteArray();
319  }
320
321  /**
322   * The input should be created by {@link #toByteArray}.
323   * @param pbBytes A pb serialized TableDescriptor instance with pb magic prefix
324   * @return This instance serialized with pb with pb magic prefix
325   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
326   */
327  public static TableDescriptor parseFrom(byte[] pbBytes) throws DeserializationException {
328    return ModifyableTableDescriptor.parseFrom(pbBytes);
329  }
330
331  public static TableDescriptorBuilder newBuilder(final TableName name) {
332    return new TableDescriptorBuilder(name);
333  }
334
335  public static TableDescriptor copy(TableDescriptor desc) {
336    return new ModifyableTableDescriptor(desc);
337  }
338
339  public static TableDescriptor copy(TableName name, TableDescriptor desc) {
340    return new ModifyableTableDescriptor(name, desc);
341  }
342
343  /**
344   * Copy all values, families, and name from the input.
345   * @param desc The desciptor to copy
346   * @return A clone of input
347   */
348  public static TableDescriptorBuilder newBuilder(final TableDescriptor desc) {
349    return new TableDescriptorBuilder(desc);
350  }
351
352  private TableDescriptorBuilder(final TableName name) {
353    this.desc = new ModifyableTableDescriptor(name);
354  }
355
356  private TableDescriptorBuilder(final TableDescriptor desc) {
357    this.desc = new ModifyableTableDescriptor(desc);
358  }
359
360  public TableDescriptorBuilder setCoprocessor(String className) throws IOException {
361    return setCoprocessor(CoprocessorDescriptorBuilder.of(className));
362  }
363
364  public TableDescriptorBuilder setCoprocessor(CoprocessorDescriptor cpDesc) throws IOException {
365    desc.setCoprocessor(Objects.requireNonNull(cpDesc));
366    return this;
367  }
368
369  public TableDescriptorBuilder setCoprocessors(Collection<CoprocessorDescriptor> cpDescs)
370    throws IOException {
371    for (CoprocessorDescriptor cpDesc : cpDescs) {
372      desc.setCoprocessor(cpDesc);
373    }
374    return this;
375  }
376
377  public TableDescriptorBuilder setColumnFamily(final ColumnFamilyDescriptor family) {
378    desc.setColumnFamily(Objects.requireNonNull(family));
379    return this;
380  }
381
382  public TableDescriptorBuilder setColumnFamilies(
383    final Collection<ColumnFamilyDescriptor> families) {
384    families.forEach(desc::setColumnFamily);
385    return this;
386  }
387
388  public TableDescriptorBuilder modifyColumnFamily(final ColumnFamilyDescriptor family) {
389    desc.modifyColumnFamily(Objects.requireNonNull(family));
390    return this;
391  }
392
393  public TableDescriptorBuilder removeValue(Bytes key) {
394    desc.removeValue(key);
395    return this;
396  }
397
398  public TableDescriptorBuilder removeValue(byte[] key) {
399    desc.removeValue(key);
400    return this;
401  }
402
403  public TableDescriptorBuilder removeColumnFamily(final byte[] name) {
404    desc.removeColumnFamily(name);
405    return this;
406  }
407
408  public TableDescriptorBuilder removeCoprocessor(String className) {
409    desc.removeCoprocessor(className);
410    return this;
411  }
412
413  public TableDescriptorBuilder setCompactionEnabled(final boolean isEnable) {
414    desc.setCompactionEnabled(isEnable);
415    return this;
416  }
417
418  public TableDescriptorBuilder setSplitEnabled(final boolean isEnable) {
419    desc.setSplitEnabled(isEnable);
420    return this;
421  }
422
423  public TableDescriptorBuilder setMergeEnabled(final boolean isEnable) {
424    desc.setMergeEnabled(isEnable);
425    return this;
426  }
427
428  public TableDescriptorBuilder setDurability(Durability durability) {
429    desc.setDurability(durability);
430    return this;
431  }
432
433  public TableDescriptorBuilder setFlushPolicyClassName(String clazz) {
434    desc.setFlushPolicyClassName(clazz);
435    return this;
436  }
437
438  public TableDescriptorBuilder setMaxFileSize(long maxFileSize) {
439    desc.setMaxFileSize(maxFileSize);
440    return this;
441  }
442
443  public TableDescriptorBuilder setMemStoreFlushSize(long memstoreFlushSize) {
444    desc.setMemStoreFlushSize(memstoreFlushSize);
445    return this;
446  }
447
448  public TableDescriptorBuilder setNormalizerTargetRegionCount(final int regionCount) {
449    desc.setNormalizerTargetRegionCount(regionCount);
450    return this;
451  }
452
453  public TableDescriptorBuilder setNormalizerTargetRegionSize(final long regionSize) {
454    desc.setNormalizerTargetRegionSize(regionSize);
455    return this;
456  }
457
458  public TableDescriptorBuilder setNormalizationEnabled(final boolean isEnable) {
459    desc.setNormalizationEnabled(isEnable);
460    return this;
461  }
462
463  /**
464   * @deprecated since 2.0.0 and will be removed in 3.0.0.
465   * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
466   */
467  @Deprecated
468  public TableDescriptorBuilder setOwner(User owner) {
469    desc.setOwner(owner);
470    return this;
471  }
472
473  /**
474   * @deprecated since 2.0.0 and will be removed in 3.0.0.
475   * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
476   */
477  @Deprecated
478  public TableDescriptorBuilder setOwnerString(String ownerString) {
479    desc.setOwnerString(ownerString);
480    return this;
481  }
482
483  public TableDescriptorBuilder setPriority(int priority) {
484    desc.setPriority(priority);
485    return this;
486  }
487
488  public TableDescriptorBuilder setReadOnly(final boolean readOnly) {
489    desc.setReadOnly(readOnly);
490    return this;
491  }
492
493  public TableDescriptorBuilder setRegionMemStoreReplication(boolean memstoreReplication) {
494    desc.setRegionMemStoreReplication(memstoreReplication);
495    return this;
496  }
497
498  public TableDescriptorBuilder setRegionReplication(int regionReplication) {
499    desc.setRegionReplication(regionReplication);
500    return this;
501  }
502
503  public TableDescriptorBuilder setRegionSplitPolicyClassName(String clazz) {
504    desc.setRegionSplitPolicyClassName(clazz);
505    return this;
506  }
507
508  public TableDescriptorBuilder setValue(final String key, final String value) {
509    desc.setValue(key, value);
510    return this;
511  }
512
513  public TableDescriptorBuilder setValue(final Bytes key, final Bytes value) {
514    desc.setValue(key, value);
515    return this;
516  }
517
518  public TableDescriptorBuilder setValue(final byte[] key, final byte[] value) {
519    desc.setValue(key, value);
520    return this;
521  }
522
523  /**
524   * Sets replication scope all & only the columns already in the builder. Columns added later won't
525   * be backfilled with replication scope.
526   * @param scope replication scope
527   * @return a TableDescriptorBuilder
528   */
529  public TableDescriptorBuilder setReplicationScope(int scope) {
530    Map<byte[], ColumnFamilyDescriptor> newFamilies = new TreeMap<>(Bytes.BYTES_RAWCOMPARATOR);
531    newFamilies.putAll(desc.families);
532    newFamilies
533        .forEach((cf, cfDesc) -> {
534          desc.removeColumnFamily(cf);
535          desc.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(cfDesc).setScope(scope)
536              .build());
537        });
538    return this;
539  }
540
541  public TableDescriptor build() {
542    return new ModifyableTableDescriptor(desc);
543  }
544
545  /**
546   * TODO: make this private after removing the HTableDescriptor
547   */
548  @InterfaceAudience.Private
549  public static class ModifyableTableDescriptor
550          implements TableDescriptor, Comparable<ModifyableTableDescriptor> {
551
552    private final TableName name;
553
554    /**
555     * A map which holds the metadata information of the table. This metadata
556     * includes values like IS_META, SPLIT_POLICY, MAX_FILE_SIZE,
557     * READONLY, MEMSTORE_FLUSHSIZE etc...
558     */
559    private final Map<Bytes, Bytes> values = new HashMap<>();
560
561    /**
562     * Maps column family name to the respective FamilyDescriptors
563     */
564    private final Map<byte[], ColumnFamilyDescriptor> families
565            = new TreeMap<>(Bytes.BYTES_RAWCOMPARATOR);
566
567    /**
568     * Construct a table descriptor specifying a TableName object
569     *
570     * @param name Table name.
571     * TODO: make this private after removing the HTableDescriptor
572     */
573    @InterfaceAudience.Private
574    public ModifyableTableDescriptor(final TableName name) {
575      this(name, Collections.EMPTY_LIST, Collections.EMPTY_MAP);
576    }
577
578    private ModifyableTableDescriptor(final TableDescriptor desc) {
579      this(desc.getTableName(), Arrays.asList(desc.getColumnFamilies()), desc.getValues());
580    }
581
582    /**
583     * Construct a table descriptor by cloning the descriptor passed as a
584     * parameter.
585     * <p>
586     * Makes a deep copy of the supplied descriptor.
587     * @param name The new name
588     * @param desc The descriptor.
589     * TODO: make this private after removing the HTableDescriptor
590     */
591    @InterfaceAudience.Private
592    @Deprecated // only used by HTableDescriptor. remove this method if HTD is removed
593    public ModifyableTableDescriptor(final TableName name, final TableDescriptor desc) {
594      this(name, Arrays.asList(desc.getColumnFamilies()), desc.getValues());
595    }
596
597    private ModifyableTableDescriptor(final TableName name, final Collection<ColumnFamilyDescriptor> families,
598            Map<Bytes, Bytes> values) {
599      this.name = name;
600      families.forEach(c -> this.families.put(c.getName(), ColumnFamilyDescriptorBuilder.copy(c)));
601      this.values.putAll(values);
602      this.values.put(IS_META_KEY,
603        new Bytes(Bytes.toBytes(Boolean.toString(name.equals(TableName.META_TABLE_NAME)))));
604    }
605
606    /**
607     * Checks if this table is <code> hbase:meta </code> region.
608     *
609     * @return true if this table is <code> hbase:meta </code> region
610     */
611    @Override
612    public boolean isMetaRegion() {
613      return getOrDefault(IS_META_KEY, Boolean::valueOf, false);
614    }
615
616    /**
617     * Checks if the table is a <code>hbase:meta</code> table
618     *
619     * @return true if table is <code> hbase:meta </code> region.
620     */
621    @Override
622    public boolean isMetaTable() {
623      return isMetaRegion();
624    }
625
626    @Override
627    public Bytes getValue(Bytes key) {
628      Bytes rval = values.get(key);
629      return rval == null ? null : new Bytes(rval.copyBytes());
630    }
631
632    @Override
633    public String getValue(String key) {
634      Bytes rval = values.get(new Bytes(Bytes.toBytes(key)));
635      return rval == null ? null : Bytes.toString(rval.get(), rval.getOffset(), rval.getLength());
636    }
637
638    @Override
639    public byte[] getValue(byte[] key) {
640      Bytes value = values.get(new Bytes(key));
641      return value == null ? null : value.copyBytes();
642    }
643
644    private <T> T getOrDefault(Bytes key, Function<String, T> function, T defaultValue) {
645      Bytes value = values.get(key);
646      if (value == null) {
647        return defaultValue;
648      } else {
649        return function.apply(Bytes.toString(value.get(), value.getOffset(), value.getLength()));
650      }
651    }
652
653    /**
654     * Getter for fetching an unmodifiable {@link #values} map.
655     *
656     * @return unmodifiable map {@link #values}.
657     * @see #values
658     */
659    @Override
660    public Map<Bytes, Bytes> getValues() {
661      // shallow pointer copy
662      return Collections.unmodifiableMap(values);
663    }
664
665    /**
666     * Setter for storing metadata as a (key, value) pair in {@link #values} map
667     *
668     * @param key The key.
669     * @param value The value. If null, removes the setting.
670     * @return the modifyable TD
671     * @see #values
672     */
673    public ModifyableTableDescriptor setValue(byte[] key, byte[] value) {
674      return setValue(toBytesOrNull(key, v -> v),
675              toBytesOrNull(value, v -> v));
676    }
677
678    public ModifyableTableDescriptor setValue(String key, String value) {
679      return setValue(toBytesOrNull(key, Bytes::toBytes),
680              toBytesOrNull(value, Bytes::toBytes));
681    }
682
683    /*
684     * @param key The key.
685     * @param value The value. If null, removes the setting.
686     */
687    private ModifyableTableDescriptor setValue(final Bytes key,
688            final String value) {
689      return setValue(key, toBytesOrNull(value, Bytes::toBytes));
690    }
691
692    /*
693     * Setter for storing metadata as a (key, value) pair in {@link #values} map
694     *
695     * @param key The key.
696     * @param value The value. If null, removes the setting.
697     */
698    public ModifyableTableDescriptor setValue(final Bytes key, final Bytes value) {
699      if (value == null) {
700        values.remove(key);
701      } else {
702        values.put(key, value);
703      }
704      return this;
705    }
706
707    private static <T> Bytes toBytesOrNull(T t, Function<T, byte[]> f) {
708      if (t == null) {
709        return null;
710      } else {
711        return new Bytes(f.apply(t));
712      }
713    }
714
715    /**
716     * Remove metadata represented by the key from the {@link #values} map
717     *
718     * @param key Key whose key and value we're to remove from TableDescriptor
719     * parameters.
720     * @return the modifyable TD
721     */
722    public ModifyableTableDescriptor removeValue(Bytes key) {
723      return setValue(key, (Bytes) null);
724    }
725
726    /**
727     * Remove metadata represented by the key from the {@link #values} map
728     *
729     * @param key Key whose key and value we're to remove from TableDescriptor
730     * parameters.
731     * @return the modifyable TD
732     */
733    public ModifyableTableDescriptor removeValue(final byte[] key) {
734      return removeValue(new Bytes(key));
735    }
736
737    /**
738     * Check if the readOnly flag of the table is set. If the readOnly flag is
739     * set then the contents of the table can only be read from but not
740     * modified.
741     *
742     * @return true if all columns in the table should be read only
743     */
744    @Override
745    public boolean isReadOnly() {
746      return getOrDefault(READONLY_KEY, Boolean::valueOf, DEFAULT_READONLY);
747    }
748
749    /**
750     * Setting the table as read only sets all the columns in the table as read
751     * only. By default all tables are modifiable, but if the readOnly flag is
752     * set to true then the contents of the table can only be read but not
753     * modified.
754     *
755     * @param readOnly True if all of the columns in the table should be read
756     * only.
757     * @return the modifyable TD
758     */
759    public ModifyableTableDescriptor setReadOnly(final boolean readOnly) {
760      return setValue(READONLY_KEY, Boolean.toString(readOnly));
761    }
762
763    /**
764     * Check if the compaction enable flag of the table is true. If flag is
765     * false then no minor/major compactions will be done in real.
766     *
767     * @return true if table compaction enabled
768     */
769    @Override
770    public boolean isCompactionEnabled() {
771      return getOrDefault(COMPACTION_ENABLED_KEY, Boolean::valueOf, DEFAULT_COMPACTION_ENABLED);
772    }
773
774    /**
775     * Setting the table compaction enable flag.
776     *
777     * @param isEnable True if enable compaction.
778     * @return the modifyable TD
779     */
780    public ModifyableTableDescriptor setCompactionEnabled(final boolean isEnable) {
781      return setValue(COMPACTION_ENABLED_KEY, Boolean.toString(isEnable));
782    }
783
784    /**
785     * Check if the split enable flag of the table is true. If flag is false then no split will be
786     * done.
787     *
788     * @return true if table region split enabled
789     */
790    @Override
791    public boolean isSplitEnabled() {
792      return getOrDefault(SPLIT_ENABLED_KEY, Boolean::valueOf, DEFAULT_SPLIT_ENABLED);
793    }
794
795    /**
796     * Setting the table region split enable flag.
797     * @param isEnable True if enable region split.
798     *
799     * @return the modifyable TD
800     */
801    public ModifyableTableDescriptor setSplitEnabled(final boolean isEnable) {
802      return setValue(SPLIT_ENABLED_KEY, Boolean.toString(isEnable));
803    }
804
805    /**
806     * Check if the region merge enable flag of the table is true. If flag is false then no merge
807     * will be done.
808     *
809     * @return true if table region merge enabled
810     */
811    @Override
812    public boolean isMergeEnabled() {
813      return getOrDefault(MERGE_ENABLED_KEY, Boolean::valueOf, DEFAULT_MERGE_ENABLED);
814    }
815
816    /**
817     * Setting the table region merge enable flag.
818     * @param isEnable True if enable region merge.
819     *
820     * @return the modifyable TD
821     */
822    public ModifyableTableDescriptor setMergeEnabled(final boolean isEnable) {
823      return setValue(MERGE_ENABLED_KEY, Boolean.toString(isEnable));
824    }
825
826    /**
827     * Check if normalization enable flag of the table is true. If flag is false
828     * then no region normalizer won't attempt to normalize this table.
829     *
830     * @return true if region normalization is enabled for this table
831     */
832    @Override
833    public boolean isNormalizationEnabled() {
834      return getOrDefault(NORMALIZATION_ENABLED_KEY, Boolean::valueOf, DEFAULT_NORMALIZATION_ENABLED);
835    }
836
837    /**
838     * Check if there is the target region count. If so, the normalize plan will be calculated based
839     * on the target region count.
840     * @return target region count after normalize done
841     */
842    @Override
843    public int getNormalizerTargetRegionCount() {
844      return getOrDefault(NORMALIZER_TARGET_REGION_COUNT_KEY, Integer::valueOf,
845        Integer.valueOf(-1));
846    }
847
848    /**
849     * Check if there is the target region size. If so, the normalize plan will be calculated based
850     * on the target region size.
851     * @return target region size after normalize done
852     */
853    @Override
854    public long getNormalizerTargetRegionSize() {
855      return getOrDefault(NORMALIZER_TARGET_REGION_SIZE_KEY, Long::valueOf, Long.valueOf(-1));
856    }
857
858    /**
859     * Setting the table normalization enable flag.
860     *
861     * @param isEnable True if enable normalization.
862     * @return the modifyable TD
863     */
864    public ModifyableTableDescriptor setNormalizationEnabled(final boolean isEnable) {
865      return setValue(NORMALIZATION_ENABLED_KEY, Boolean.toString(isEnable));
866    }
867
868    /**
869     * Setting the target region count of table normalization .
870     * @param regionCount the target region count.
871     * @return the modifyable TD
872     */
873    public ModifyableTableDescriptor setNormalizerTargetRegionCount(final int regionCount) {
874      return setValue(NORMALIZER_TARGET_REGION_COUNT_KEY, Integer.toString(regionCount));
875    }
876
877    /**
878     * Setting the target region size of table normalization.
879     * @param regionSize the target region size.
880     * @return the modifyable TD
881     */
882    public ModifyableTableDescriptor setNormalizerTargetRegionSize(final long regionSize) {
883      return setValue(NORMALIZER_TARGET_REGION_SIZE_KEY, Long.toString(regionSize));
884    }
885
886    /**
887     * Sets the {@link Durability} setting for the table. This defaults to
888     * Durability.USE_DEFAULT.
889     *
890     * @param durability enum value
891     * @return the modifyable TD
892     */
893    public ModifyableTableDescriptor setDurability(Durability durability) {
894      return setValue(DURABILITY_KEY, durability.name());
895    }
896
897    /**
898     * Returns the durability setting for the table.
899     *
900     * @return durability setting for the table.
901     */
902    @Override
903    public Durability getDurability() {
904      return getOrDefault(DURABILITY_KEY, Durability::valueOf, DEFAULT_DURABLITY);
905    }
906
907    /**
908     * Get the name of the table
909     *
910     * @return TableName
911     */
912    @Override
913    public TableName getTableName() {
914      return name;
915    }
916
917    /**
918     * This sets the class associated with the region split policy which
919     * determines when a region split should occur. The class used by default is
920     * defined in org.apache.hadoop.hbase.regionserver.RegionSplitPolicy
921     *
922     * @param clazz the class name
923     * @return the modifyable TD
924     */
925    public ModifyableTableDescriptor setRegionSplitPolicyClassName(String clazz) {
926      return setValue(SPLIT_POLICY_KEY, clazz);
927    }
928
929    /**
930     * This gets the class associated with the region split policy which
931     * determines when a region split should occur. The class used by default is
932     * defined in org.apache.hadoop.hbase.regionserver.RegionSplitPolicy
933     *
934     * @return the class name of the region split policy for this table. If this
935     * returns null, the default split policy is used.
936     */
937    @Override
938    public String getRegionSplitPolicyClassName() {
939      return getOrDefault(SPLIT_POLICY_KEY, Function.identity(), null);
940    }
941
942    /**
943     * Returns the maximum size upto which a region can grow to after which a
944     * region split is triggered. The region size is represented by the size of
945     * the biggest store file in that region.
946     *
947     * @return max hregion size for table, -1 if not set.
948     *
949     * @see #setMaxFileSize(long)
950     */
951    @Override
952    public long getMaxFileSize() {
953      return getOrDefault(MAX_FILESIZE_KEY, Long::valueOf, (long) -1);
954    }
955
956    /**
957     * Sets the maximum size upto which a region can grow to after which a
958     * region split is triggered. The region size is represented by the size of
959     * the biggest store file in that region, i.e. If the biggest store file
960     * grows beyond the maxFileSize, then the region split is triggered. This
961     * defaults to a value of 256 MB.
962     * <p>
963     * This is not an absolute value and might vary. Assume that a single row
964     * exceeds the maxFileSize then the storeFileSize will be greater than
965     * maxFileSize since a single row cannot be split across multiple regions
966     * </p>
967     *
968     * @param maxFileSize The maximum file size that a store file can grow to
969     * before a split is triggered.
970     * @return the modifyable TD
971     */
972    public ModifyableTableDescriptor setMaxFileSize(long maxFileSize) {
973      return setValue(MAX_FILESIZE_KEY, Long.toString(maxFileSize));
974    }
975
976    /**
977     * Returns the size of the memstore after which a flush to filesystem is
978     * triggered.
979     *
980     * @return memory cache flush size for each hregion, -1 if not set.
981     *
982     * @see #setMemStoreFlushSize(long)
983     */
984    @Override
985    public long getMemStoreFlushSize() {
986      return getOrDefault(MEMSTORE_FLUSHSIZE_KEY, Long::valueOf, (long) -1);
987    }
988
989    /**
990     * Represents the maximum size of the memstore after which the contents of
991     * the memstore are flushed to the filesystem. This defaults to a size of 64
992     * MB.
993     *
994     * @param memstoreFlushSize memory cache flush size for each hregion
995     * @return the modifyable TD
996     */
997    public ModifyableTableDescriptor setMemStoreFlushSize(long memstoreFlushSize) {
998      return setValue(MEMSTORE_FLUSHSIZE_KEY, Long.toString(memstoreFlushSize));
999    }
1000
1001    /**
1002     * This sets the class associated with the flush policy which determines
1003     * determines the stores need to be flushed when flushing a region. The
1004     * class used by default is defined in
1005     * org.apache.hadoop.hbase.regionserver.FlushPolicy.
1006     *
1007     * @param clazz the class name
1008     * @return the modifyable TD
1009     */
1010    public ModifyableTableDescriptor setFlushPolicyClassName(String clazz) {
1011      return setValue(FLUSH_POLICY_KEY, clazz);
1012    }
1013
1014    /**
1015     * This gets the class associated with the flush policy which determines the
1016     * stores need to be flushed when flushing a region. The class used by
1017     * default is defined in org.apache.hadoop.hbase.regionserver.FlushPolicy.
1018     *
1019     * @return the class name of the flush policy for this table. If this
1020     * returns null, the default flush policy is used.
1021     */
1022    @Override
1023    public String getFlushPolicyClassName() {
1024      return getOrDefault(FLUSH_POLICY_KEY, Function.identity(), null);
1025    }
1026
1027    /**
1028     * Adds a column family. For the updating purpose please use
1029     * {@link #modifyColumnFamily(ColumnFamilyDescriptor)} instead.
1030     *
1031     * @param family to add.
1032     * @return the modifyable TD
1033     */
1034    public ModifyableTableDescriptor setColumnFamily(final ColumnFamilyDescriptor family) {
1035      if (family.getName() == null || family.getName().length <= 0) {
1036        throw new IllegalArgumentException("Family name cannot be null or empty");
1037      }
1038      if (hasColumnFamily(family.getName())) {
1039        throw new IllegalArgumentException("Family '"
1040                + family.getNameAsString() + "' already exists so cannot be added");
1041      }
1042      return putColumnFamily(family);
1043    }
1044
1045    /**
1046     * Modifies the existing column family.
1047     *
1048     * @param family to update
1049     * @return this (for chained invocation)
1050     */
1051    public ModifyableTableDescriptor modifyColumnFamily(final ColumnFamilyDescriptor family) {
1052      if (family.getName() == null || family.getName().length <= 0) {
1053        throw new IllegalArgumentException("Family name cannot be null or empty");
1054      }
1055      if (!hasColumnFamily(family.getName())) {
1056        throw new IllegalArgumentException("Column family '" + family.getNameAsString()
1057                + "' does not exist");
1058      }
1059      return putColumnFamily(family);
1060    }
1061
1062    private ModifyableTableDescriptor putColumnFamily(ColumnFamilyDescriptor family) {
1063      families.put(family.getName(), family);
1064      return this;
1065    }
1066
1067    /**
1068     * Checks to see if this table contains the given column family
1069     *
1070     * @param familyName Family name or column name.
1071     * @return true if the table contains the specified family name
1072     */
1073    @Override
1074    public boolean hasColumnFamily(final byte[] familyName) {
1075      return families.containsKey(familyName);
1076    }
1077
1078    /**
1079     * @return Name of this table and then a map of all of the column family descriptors.
1080     */
1081    @Override
1082    public String toString() {
1083      StringBuilder s = new StringBuilder();
1084      s.append('\'').append(Bytes.toString(name.getName())).append('\'');
1085      s.append(getValues(true));
1086      families.values().forEach(f -> s.append(", ").append(f));
1087      return s.toString();
1088    }
1089
1090    /**
1091     * @return Name of this table and then a map of all of the column family
1092     * descriptors (with only the non-default column family attributes)
1093     */
1094    @Override
1095    public String toStringCustomizedValues() {
1096      StringBuilder s = new StringBuilder();
1097      s.append('\'').append(Bytes.toString(name.getName())).append('\'');
1098      s.append(getValues(false));
1099      families.values().forEach(hcd -> s.append(", ").append(hcd.toStringCustomizedValues()));
1100      return s.toString();
1101    }
1102
1103    /**
1104     * @return map of all table attributes formatted into string.
1105     */
1106    public String toStringTableAttributes() {
1107      return getValues(true).toString();
1108    }
1109
1110    private StringBuilder getValues(boolean printDefaults) {
1111      StringBuilder s = new StringBuilder();
1112
1113      // step 1: set partitioning and pruning
1114      Set<Bytes> reservedKeys = new TreeSet<>();
1115      Set<Bytes> userKeys = new TreeSet<>();
1116      for (Map.Entry<Bytes, Bytes> entry : values.entrySet()) {
1117        if (entry.getKey() == null || entry.getKey().get() == null) {
1118          continue;
1119        }
1120        String key = Bytes.toString(entry.getKey().get());
1121        // in this section, print out reserved keywords + coprocessor info
1122        if (!RESERVED_KEYWORDS.contains(entry.getKey()) && !key.startsWith("coprocessor$")) {
1123          userKeys.add(entry.getKey());
1124          continue;
1125        }
1126        // only print out IS_META if true
1127        String value = Bytes.toString(entry.getValue().get());
1128        if (key.equalsIgnoreCase(IS_META)) {
1129          if (Boolean.valueOf(value) == false) {
1130            continue;
1131          }
1132        }
1133        // see if a reserved key is a default value. may not want to print it out
1134        if (printDefaults
1135                || !DEFAULT_VALUES.containsKey(key)
1136                || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
1137          reservedKeys.add(entry.getKey());
1138        }
1139      }
1140
1141      // early exit optimization
1142      boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
1143      if (!hasAttributes) {
1144        return s;
1145      }
1146
1147      s.append(", {");
1148      // step 2: printing attributes
1149      if (hasAttributes) {
1150        s.append("TABLE_ATTRIBUTES => {");
1151
1152        // print all reserved keys first
1153        boolean printCommaForAttr = false;
1154        for (Bytes k : reservedKeys) {
1155          String key = Bytes.toString(k.get());
1156          String value = Bytes.toStringBinary(values.get(k).get());
1157          if (printCommaForAttr) {
1158            s.append(", ");
1159          }
1160          printCommaForAttr = true;
1161          s.append(key);
1162          s.append(" => ");
1163          s.append('\'').append(value).append('\'');
1164        }
1165
1166        if (!userKeys.isEmpty()) {
1167          // print all non-reserved as a separate subset
1168          if (printCommaForAttr) {
1169            s.append(", ");
1170          }
1171          s.append(HConstants.METADATA).append(" => ");
1172          s.append("{");
1173          boolean printCommaForCfg = false;
1174          for (Bytes k : userKeys) {
1175            String key = Bytes.toString(k.get());
1176            String value = Bytes.toStringBinary(values.get(k).get());
1177            if (printCommaForCfg) {
1178              s.append(", ");
1179            }
1180            printCommaForCfg = true;
1181            s.append('\'').append(key).append('\'');
1182            s.append(" => ");
1183            s.append('\'').append(value).append('\'');
1184          }
1185          s.append("}");
1186        }
1187
1188        s.append("}");
1189      }
1190
1191      s.append("}"); // end METHOD
1192      return s;
1193    }
1194
1195    /**
1196     * Compare the contents of the descriptor with another one passed as a
1197     * parameter. Checks if the obj passed is an instance of ModifyableTableDescriptor,
1198     * if yes then the contents of the descriptors are compared.
1199     *
1200     * @param obj The object to compare
1201     * @return true if the contents of the the two descriptors exactly match
1202     *
1203     * @see java.lang.Object#equals(java.lang.Object)
1204     */
1205    @Override
1206    public boolean equals(Object obj) {
1207      if (this == obj) {
1208        return true;
1209      }
1210      if (obj instanceof ModifyableTableDescriptor) {
1211        return TableDescriptor.COMPARATOR.compare(this, (ModifyableTableDescriptor) obj) == 0;
1212      }
1213      return false;
1214    }
1215
1216    /**
1217     * @return hash code
1218     */
1219    @Override
1220    public int hashCode() {
1221      int result = this.name.hashCode();
1222      if (this.families.size() > 0) {
1223        for (ColumnFamilyDescriptor e : this.families.values()) {
1224          result ^= e.hashCode();
1225        }
1226      }
1227      result ^= values.hashCode();
1228      return result;
1229    }
1230
1231    // Comparable
1232    /**
1233     * Compares the descriptor with another descriptor which is passed as a
1234     * parameter. This compares the content of the two descriptors and not the
1235     * reference.
1236     *
1237     * @param other The MTD to compare
1238     * @return 0 if the contents of the descriptors are exactly matching, 1 if
1239     * there is a mismatch in the contents
1240     */
1241    @Override
1242    public int compareTo(final ModifyableTableDescriptor other) {
1243      return TableDescriptor.COMPARATOR.compare(this, other);
1244    }
1245
1246    @Override
1247    public ColumnFamilyDescriptor[] getColumnFamilies() {
1248      return families.values().toArray(new ColumnFamilyDescriptor[families.size()]);
1249    }
1250
1251    /**
1252     * Returns the configured replicas per region
1253     */
1254    @Override
1255    public int getRegionReplication() {
1256      return getOrDefault(REGION_REPLICATION_KEY, Integer::valueOf, DEFAULT_REGION_REPLICATION);
1257    }
1258
1259    /**
1260     * Sets the number of replicas per region.
1261     *
1262     * @param regionReplication the replication factor per region
1263     * @return the modifyable TD
1264     */
1265    public ModifyableTableDescriptor setRegionReplication(int regionReplication) {
1266      return setValue(REGION_REPLICATION_KEY, Integer.toString(regionReplication));
1267    }
1268
1269    /**
1270     * @return true if the read-replicas memstore replication is enabled.
1271     */
1272    @Override
1273    public boolean hasRegionMemStoreReplication() {
1274      return getOrDefault(REGION_MEMSTORE_REPLICATION_KEY, Boolean::valueOf, DEFAULT_REGION_MEMSTORE_REPLICATION);
1275    }
1276
1277    /**
1278     * Enable or Disable the memstore replication from the primary region to the
1279     * replicas. The replication will be used only for meta operations (e.g.
1280     * flush, compaction, ...)
1281     *
1282     * @param memstoreReplication true if the new data written to the primary
1283     * region should be replicated. false if the secondaries can tollerate to
1284     * have new data only when the primary flushes the memstore.
1285     * @return the modifyable TD
1286     */
1287    public ModifyableTableDescriptor setRegionMemStoreReplication(boolean memstoreReplication) {
1288      setValue(REGION_MEMSTORE_REPLICATION_KEY, Boolean.toString(memstoreReplication));
1289      // If the memstore replication is setup, we do not have to wait for observing a flush event
1290      // from primary before starting to serve reads, because gaps from replication is not applicable
1291      return setValue(REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
1292              Boolean.toString(memstoreReplication));
1293    }
1294
1295    public ModifyableTableDescriptor setPriority(int priority) {
1296      return setValue(PRIORITY_KEY, Integer.toString(priority));
1297    }
1298
1299    @Override
1300    public int getPriority() {
1301      return getOrDefault(PRIORITY_KEY, Integer::valueOf, DEFAULT_PRIORITY);
1302    }
1303
1304    /**
1305     * Returns all the column family names of the current table. The map of
1306     * TableDescriptor contains mapping of family name to ColumnFamilyDescriptor.
1307     * This returns all the keys of the family map which represents the column
1308     * family names of the table.
1309     *
1310     * @return Immutable sorted set of the keys of the families.
1311     */
1312    @Override
1313    public Set<byte[]> getColumnFamilyNames() {
1314      return Collections.unmodifiableSet(this.families.keySet());
1315    }
1316
1317    /**
1318     * Returns the ColumnFamilyDescriptor for a specific column family with name as
1319     * specified by the parameter column.
1320     *
1321     * @param column Column family name
1322     * @return Column descriptor for the passed family name or the family on
1323     * passed in column.
1324     */
1325    @Override
1326    public ColumnFamilyDescriptor getColumnFamily(final byte[] column) {
1327      return this.families.get(column);
1328    }
1329
1330    /**
1331     * Removes the ColumnFamilyDescriptor with name specified by the parameter column
1332     * from the table descriptor
1333     *
1334     * @param column Name of the column family to be removed.
1335     * @return Column descriptor for the passed family name or the family on
1336     * passed in column.
1337     */
1338    public ColumnFamilyDescriptor removeColumnFamily(final byte[] column) {
1339      return this.families.remove(column);
1340    }
1341
1342    /**
1343     * Add a table coprocessor to this table. The coprocessor type must be
1344     * org.apache.hadoop.hbase.coprocessor.RegionObserver or Endpoint. It won't
1345     * check if the class can be loaded or not. Whether a coprocessor is
1346     * loadable or not will be determined when a region is opened.
1347     *
1348     * @param className Full class name.
1349     * @throws IOException
1350     * @return the modifyable TD
1351     */
1352    public ModifyableTableDescriptor setCoprocessor(String className) throws IOException {
1353      return setCoprocessor(
1354        CoprocessorDescriptorBuilder.newBuilder(className).setPriority(Coprocessor.PRIORITY_USER)
1355          .build());
1356    }
1357
1358    /**
1359     * Add a table coprocessor to this table. The coprocessor type must be
1360     * org.apache.hadoop.hbase.coprocessor.RegionObserver or Endpoint. It won't
1361     * check if the class can be loaded or not. Whether a coprocessor is
1362     * loadable or not will be determined when a region is opened.
1363     *
1364     * @throws IOException any illegal parameter key/value
1365     * @return the modifyable TD
1366     */
1367    public ModifyableTableDescriptor setCoprocessor(CoprocessorDescriptor cp)
1368            throws IOException {
1369      checkHasCoprocessor(cp.getClassName());
1370      if (cp.getPriority() < 0) {
1371        throw new IOException("Priority must be bigger than or equal with zero, current:"
1372          + cp.getPriority());
1373      }
1374      // Validate parameter kvs and then add key/values to kvString.
1375      StringBuilder kvString = new StringBuilder();
1376      for (Map.Entry<String, String> e : cp.getProperties().entrySet()) {
1377        if (!e.getKey().matches(CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1378          throw new IOException("Illegal parameter key = " + e.getKey());
1379        }
1380        if (!e.getValue().matches(CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1381          throw new IOException("Illegal parameter (" + e.getKey()
1382                  + ") value = " + e.getValue());
1383        }
1384        if (kvString.length() != 0) {
1385          kvString.append(',');
1386        }
1387        kvString.append(e.getKey());
1388        kvString.append('=');
1389        kvString.append(e.getValue());
1390      }
1391
1392      String value = cp.getJarPath().orElse("")
1393              + "|" + cp.getClassName() + "|" + Integer.toString(cp.getPriority()) + "|"
1394              + kvString.toString();
1395      return setCoprocessorToMap(value);
1396    }
1397
1398    /**
1399     * Add a table coprocessor to this table. The coprocessor type must be
1400     * org.apache.hadoop.hbase.coprocessor.RegionObserver or Endpoint. It won't
1401     * check if the class can be loaded or not. Whether a coprocessor is
1402     * loadable or not will be determined when a region is opened.
1403     *
1404     * @param specStr The Coprocessor specification all in in one String
1405     * @throws IOException
1406     * @return the modifyable TD
1407     * @deprecated used by HTableDescriptor and admin.rb.
1408     *                       As of release 2.0.0, this will be removed in HBase 3.0.0.
1409     */
1410    @Deprecated
1411    public ModifyableTableDescriptor setCoprocessorWithSpec(final String specStr)
1412      throws IOException {
1413      CoprocessorDescriptor cpDesc = toCoprocessorDescriptor(specStr).orElseThrow(
1414        () -> new IllegalArgumentException(
1415          "Format does not match " + CP_HTD_ATTR_VALUE_PATTERN + ": " + specStr));
1416      checkHasCoprocessor(cpDesc.getClassName());
1417      return setCoprocessorToMap(specStr);
1418    }
1419
1420    private void checkHasCoprocessor(final String className) throws IOException {
1421      if (hasCoprocessor(className)) {
1422        throw new IOException("Coprocessor " + className + " already exists.");
1423      }
1424    }
1425
1426    /**
1427     * Add coprocessor to values Map
1428     * @param specStr The Coprocessor specification all in in one String
1429     * @return Returns <code>this</code>
1430     */
1431    private ModifyableTableDescriptor setCoprocessorToMap(final String specStr) {
1432      if (specStr == null) {
1433        return this;
1434      }
1435      // generate a coprocessor key
1436      int maxCoprocessorNumber = 0;
1437      Matcher keyMatcher;
1438      for (Map.Entry<Bytes, Bytes> e : this.values.entrySet()) {
1439        keyMatcher = CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1440        if (!keyMatcher.matches()) {
1441          continue;
1442        }
1443        maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)), maxCoprocessorNumber);
1444      }
1445      maxCoprocessorNumber++;
1446      String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1447      return setValue(new Bytes(Bytes.toBytes(key)), new Bytes(Bytes.toBytes(specStr)));
1448    }
1449
1450    /**
1451     * Check if the table has an attached co-processor represented by the name
1452     * className
1453     *
1454     * @param classNameToMatch - Class name of the co-processor
1455     * @return true of the table has a co-processor className
1456     */
1457    @Override
1458    public boolean hasCoprocessor(String classNameToMatch) {
1459      return getCoprocessorDescriptors().stream().anyMatch(cp -> cp.getClassName()
1460        .equals(classNameToMatch));
1461    }
1462
1463    /**
1464     * Return the list of attached co-processor represented by their name
1465     * className
1466     *
1467     * @return The list of co-processors classNames
1468     */
1469    @Override
1470    public List<CoprocessorDescriptor> getCoprocessorDescriptors() {
1471      List<CoprocessorDescriptor> result = new ArrayList<>();
1472      for (Map.Entry<Bytes, Bytes> e: getValues().entrySet()) {
1473        String key = Bytes.toString(e.getKey().get()).trim();
1474        if (CP_HTD_ATTR_KEY_PATTERN.matcher(key).matches()) {
1475          toCoprocessorDescriptor(Bytes.toString(e.getValue().get()).trim())
1476            .ifPresent(result::add);
1477        }
1478      }
1479      return result;
1480    }
1481
1482    /**
1483     * Remove a coprocessor from those set on the table
1484     *
1485     * @param className Class name of the co-processor
1486     */
1487    public void removeCoprocessor(String className) {
1488      Bytes match = null;
1489      Matcher keyMatcher;
1490      Matcher valueMatcher;
1491      for (Map.Entry<Bytes, Bytes> e : this.values
1492              .entrySet()) {
1493        keyMatcher = CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1494                .getKey().get()));
1495        if (!keyMatcher.matches()) {
1496          continue;
1497        }
1498        valueMatcher = CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1499                .toString(e.getValue().get()));
1500        if (!valueMatcher.matches()) {
1501          continue;
1502        }
1503        // get className and compare
1504        String clazz = valueMatcher.group(2).trim(); // classname is the 2nd field
1505        // remove the CP if it is present
1506        if (clazz.equals(className.trim())) {
1507          match = e.getKey();
1508          break;
1509        }
1510      }
1511      // if we found a match, remove it
1512      if (match != null) {
1513        ModifyableTableDescriptor.this.removeValue(match);
1514      }
1515    }
1516
1517    /**
1518     * @deprecated since 2.0.0 and will be removed in 3.0.0.
1519     * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
1520     */
1521    @Deprecated
1522    public ModifyableTableDescriptor setOwner(User owner) {
1523      return setOwnerString(owner != null ? owner.getShortName() : null);
1524    }
1525
1526    /**
1527     * @deprecated since 2.0.0 and will be removed in 3.0.0.
1528     * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
1529     */
1530    // used by admin.rb:alter(table_name,*args) to update owner.
1531    @Deprecated
1532    public ModifyableTableDescriptor setOwnerString(String ownerString) {
1533      return setValue(OWNER_KEY, ownerString);
1534    }
1535
1536    /**
1537     * @deprecated since 2.0.0 and will be removed in 3.0.0.
1538     * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
1539     */
1540    @Override
1541    @Deprecated
1542    public String getOwnerString() {
1543      // Note that every table should have an owner (i.e. should have OWNER_KEY set).
1544      // hbase:meta should return system user as owner, not null (see
1545      // MasterFileSystem.java:bootstrap()).
1546      return getOrDefault(OWNER_KEY, Function.identity(), null);
1547    }
1548
1549    /**
1550     * @return the bytes in pb format
1551     */
1552    private byte[] toByteArray() {
1553      return ProtobufUtil.prependPBMagic(ProtobufUtil.toTableSchema(this).toByteArray());
1554    }
1555
1556    /**
1557     * @param bytes A pb serialized {@link ModifyableTableDescriptor} instance
1558     * with pb magic prefix
1559     * @return An instance of {@link ModifyableTableDescriptor} made from
1560     * <code>bytes</code>
1561     * @throws DeserializationException
1562     * @see #toByteArray()
1563     */
1564    private static TableDescriptor parseFrom(final byte[] bytes)
1565            throws DeserializationException {
1566      if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
1567        throw new DeserializationException("Expected PB encoded ModifyableTableDescriptor");
1568      }
1569      int pblen = ProtobufUtil.lengthOfPBMagic();
1570      HBaseProtos.TableSchema.Builder builder = HBaseProtos.TableSchema.newBuilder();
1571      try {
1572        ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
1573        return ProtobufUtil.toTableDescriptor(builder.build());
1574      } catch (IOException e) {
1575        throw new DeserializationException(e);
1576      }
1577    }
1578
1579    @Override
1580    public int getColumnFamilyCount() {
1581      return families.size();
1582    }
1583  }
1584
1585  private static Optional<CoprocessorDescriptor> toCoprocessorDescriptor(String spec) {
1586    Matcher matcher = CP_HTD_ATTR_VALUE_PATTERN.matcher(spec);
1587    if (matcher.matches()) {
1588      // jar file path can be empty if the cp class can be loaded
1589      // from class loader.
1590      String path = matcher.group(1).trim().isEmpty() ?
1591        null : matcher.group(1).trim();
1592      String className = matcher.group(2).trim();
1593      if (className.isEmpty()) {
1594        return Optional.empty();
1595      }
1596      String priorityStr = matcher.group(3).trim();
1597      int priority = priorityStr.isEmpty() ?
1598        Coprocessor.PRIORITY_USER : Integer.parseInt(priorityStr);
1599      String cfgSpec = null;
1600      try {
1601        cfgSpec = matcher.group(4);
1602      } catch (IndexOutOfBoundsException ex) {
1603        // ignore
1604      }
1605      Map<String, String> ourConf = new TreeMap<>();
1606      if (cfgSpec != null && !cfgSpec.trim().equals("|")) {
1607        cfgSpec = cfgSpec.substring(cfgSpec.indexOf('|') + 1);
1608        Matcher m = CP_HTD_ATTR_VALUE_PARAM_PATTERN.matcher(cfgSpec);
1609        while (m.find()) {
1610          ourConf.put(m.group(1), m.group(2));
1611        }
1612      }
1613      return Optional.of(CoprocessorDescriptorBuilder.newBuilder(className)
1614        .setJarPath(path)
1615        .setPriority(priority)
1616        .setProperties(ourConf)
1617        .build());
1618    }
1619    return Optional.empty();
1620  }
1621}