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