View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.TreeMap;
34  import java.util.TreeSet;
35  import java.util.regex.Matcher;
36  
37  import org.apache.hadoop.hbase.util.ByteStringer;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.hadoop.hbase.classification.InterfaceAudience;
41  import org.apache.hadoop.hbase.classification.InterfaceStability;
42  import org.apache.hadoop.conf.Configuration;
43  import org.apache.hadoop.fs.Path;
44  import org.apache.hadoop.hbase.client.Durability;
45  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
46  import org.apache.hadoop.hbase.exceptions.DeserializationException;
47  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
48  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
49  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BytesBytesPair;
50  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ColumnFamilySchema;
51  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
52  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.TableSchema;
53  import org.apache.hadoop.hbase.regionserver.BloomType;
54  import org.apache.hadoop.hbase.security.User;
55  import org.apache.hadoop.hbase.util.Bytes;
56  import org.apache.hadoop.hbase.util.Writables;
57  import org.apache.hadoop.io.WritableComparable;
58  
59  /**
60   * HTableDescriptor contains the details about an HBase table  such as the descriptors of
61   * all the column families, is the table a catalog table, <code> -ROOT- </code> or
62   * <code> hbase:meta </code>, if the table is read only, the maximum size of the memstore,
63   * when the region split should occur, coprocessors associated with it etc...
64   */
65  @InterfaceAudience.Public
66  @InterfaceStability.Evolving
67  public class HTableDescriptor implements WritableComparable<HTableDescriptor> {
68  
69    private static final Log LOG = LogFactory.getLog(HTableDescriptor.class);
70  
71    /**
72     *  Changes prior to version 3 were not recorded here.
73     *  Version 3 adds metadata as a map where keys and values are byte[].
74     *  Version 4 adds indexes
75     *  Version 5 removed transactional pollution -- e.g. indexes
76     *  Version 6 changed metadata to BytesBytesPair in PB
77     *  Version 7 adds table-level configuration
78     */
79    private static final byte TABLE_DESCRIPTOR_VERSION = 7;
80  
81    private TableName name = null;
82  
83    /**
84     * A map which holds the metadata information of the table. This metadata
85     * includes values like IS_ROOT, IS_META, DEFERRED_LOG_FLUSH, SPLIT_POLICY,
86     * MAX_FILE_SIZE, READONLY, MEMSTORE_FLUSHSIZE etc...
87     */
88    private final Map<ImmutableBytesWritable, ImmutableBytesWritable> values =
89      new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
90  
91    /**
92     * A map which holds the configuration specific to the table.
93     * The keys of the map have the same names as config keys and override the defaults with
94     * table-specific settings. Example usage may be for compactions, etc.
95     */
96    private final Map<String, String> configuration = new HashMap<String, String>();
97  
98    public static final String SPLIT_POLICY = "SPLIT_POLICY";
99  
100   /**
101    * <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
102    * attribute which denotes the maximum size of the store file after which
103    * a region split occurs
104    *
105    * @see #getMaxFileSize()
106    */
107   public static final String MAX_FILESIZE = "MAX_FILESIZE";
108   private static final ImmutableBytesWritable MAX_FILESIZE_KEY =
109     new ImmutableBytesWritable(Bytes.toBytes(MAX_FILESIZE));
110 
111   public static final String OWNER = "OWNER";
112   public static final ImmutableBytesWritable OWNER_KEY =
113     new ImmutableBytesWritable(Bytes.toBytes(OWNER));
114 
115   /**
116    * <em>INTERNAL</em> Used by rest interface to access this metadata
117    * attribute which denotes if the table is Read Only
118    *
119    * @see #isReadOnly()
120    */
121   public static final String READONLY = "READONLY";
122   private static final ImmutableBytesWritable READONLY_KEY =
123     new ImmutableBytesWritable(Bytes.toBytes(READONLY));
124 
125   /**
126    * <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
127    * attribute which denotes if the table is compaction enabled
128    *
129    * @see #isCompactionEnabled()
130    */
131   public static final String COMPACTION_ENABLED = "COMPACTION_ENABLED";
132   private static final ImmutableBytesWritable COMPACTION_ENABLED_KEY =
133     new ImmutableBytesWritable(Bytes.toBytes(COMPACTION_ENABLED));
134 
135   /**
136    * <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
137    * attribute which represents the maximum size of the memstore after which
138    * its contents are flushed onto the disk
139    *
140    * @see #getMemStoreFlushSize()
141    */
142   public static final String MEMSTORE_FLUSHSIZE = "MEMSTORE_FLUSHSIZE";
143   private static final ImmutableBytesWritable MEMSTORE_FLUSHSIZE_KEY =
144     new ImmutableBytesWritable(Bytes.toBytes(MEMSTORE_FLUSHSIZE));
145 
146   public static final String FLUSH_POLICY = "FLUSH_POLICY";
147 
148   /**
149    * <em>INTERNAL</em> Used by rest interface to access this metadata
150    * attribute which denotes if the table is a -ROOT- region or not
151    *
152    * @see #isRootRegion()
153    */
154   public static final String IS_ROOT = "IS_ROOT";
155   private static final ImmutableBytesWritable IS_ROOT_KEY =
156     new ImmutableBytesWritable(Bytes.toBytes(IS_ROOT));
157 
158   /**
159    * <em>INTERNAL</em> Used by rest interface to access this metadata
160    * attribute which denotes if it is a catalog table, either
161    * <code> hbase:meta </code> or <code> -ROOT- </code>
162    *
163    * @see #isMetaRegion()
164    */
165   public static final String IS_META = "IS_META";
166   private static final ImmutableBytesWritable IS_META_KEY =
167     new ImmutableBytesWritable(Bytes.toBytes(IS_META));
168 
169   /**
170    * <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
171    * attribute which denotes if the deferred log flush option is enabled.
172    * @deprecated Use {@link #DURABILITY} instead.
173    */
174   @Deprecated
175   public static final String DEFERRED_LOG_FLUSH = "DEFERRED_LOG_FLUSH";
176   @Deprecated
177   private static final ImmutableBytesWritable DEFERRED_LOG_FLUSH_KEY =
178     new ImmutableBytesWritable(Bytes.toBytes(DEFERRED_LOG_FLUSH));
179 
180   /**
181    * <em>INTERNAL</em> {@link Durability} setting for the table.
182    */
183   public static final String DURABILITY = "DURABILITY";
184   private static final ImmutableBytesWritable DURABILITY_KEY =
185       new ImmutableBytesWritable(Bytes.toBytes("DURABILITY"));
186 
187   /**
188    * <em>INTERNAL</em> number of region replicas for the table.
189    */
190   public static final String REGION_REPLICATION = "REGION_REPLICATION";
191   private static final ImmutableBytesWritable REGION_REPLICATION_KEY =
192       new ImmutableBytesWritable(Bytes.toBytes(REGION_REPLICATION));
193 
194   /**
195    * <em>INTERNAL</em> flag to indicate whether or not the memstore should be replicated
196    * for read-replicas (CONSISTENCY =&gt; TIMELINE).
197    */
198   public static final String REGION_MEMSTORE_REPLICATION = "REGION_MEMSTORE_REPLICATION";
199   private static final ImmutableBytesWritable REGION_MEMSTORE_REPLICATION_KEY =
200       new ImmutableBytesWritable(Bytes.toBytes(REGION_MEMSTORE_REPLICATION));
201 
202   /**
203    * <em>INTERNAL</em> Used by shell/rest interface to access this metadata
204    * attribute which denotes if the table should be treated by region normalizer.
205    *
206    * @see #isNormalizationEnabled()
207    */
208   public static final String NORMALIZATION_ENABLED = "NORMALIZATION_ENABLED";
209   private static final ImmutableBytesWritable NORMALIZATION_ENABLED_KEY =
210     new ImmutableBytesWritable(Bytes.toBytes(NORMALIZATION_ENABLED));
211 
212   public static final String NORMALIZER_TARGET_REGION_COUNT =
213     "NORMALIZER_TARGET_REGION_COUNT";
214   private static final ImmutableBytesWritable NORMALIZER_TARGET_REGION_COUNT_KEY =
215     new ImmutableBytesWritable(Bytes.toBytes(NORMALIZER_TARGET_REGION_COUNT));
216 
217   public static final String NORMALIZER_TARGET_REGION_SIZE = "NORMALIZER_TARGET_REGION_SIZE";
218   private static final ImmutableBytesWritable NORMALIZER_TARGET_REGION_SIZE_KEY =
219     new ImmutableBytesWritable(Bytes.toBytes(NORMALIZER_TARGET_REGION_SIZE));
220 
221   /** Default durability for HTD is USE_DEFAULT, which defaults to HBase-global default value */
222   private static final Durability DEFAULT_DURABLITY = Durability.USE_DEFAULT;
223 
224   public static final String PRIORITY = "PRIORITY";
225   private static final ImmutableBytesWritable PRIORITY_KEY =
226     new ImmutableBytesWritable(Bytes.toBytes(PRIORITY));
227 
228   /** Relative priority of the table used for rpc scheduling */
229   private static final int DEFAULT_PRIORITY = HConstants.NORMAL_QOS;
230 
231   /*
232    *  The below are ugly but better than creating them each time till we
233    *  replace booleans being saved as Strings with plain booleans.  Need a
234    *  migration script to do this.  TODO.
235    */
236   private static final ImmutableBytesWritable FALSE =
237     new ImmutableBytesWritable(Bytes.toBytes(Boolean.FALSE.toString()));
238 
239   private static final ImmutableBytesWritable TRUE =
240     new ImmutableBytesWritable(Bytes.toBytes(Boolean.TRUE.toString()));
241 
242   private static final boolean DEFAULT_DEFERRED_LOG_FLUSH = false;
243 
244   /**
245    * Constant that denotes whether the table is READONLY by default and is false
246    */
247   public static final boolean DEFAULT_READONLY = false;
248 
249   /**
250    * Constant that denotes whether the table is compaction enabled by default
251    */
252   public static final boolean DEFAULT_COMPACTION_ENABLED = true;
253 
254   /**
255    * Constant that denotes whether the table is normalized by default.
256    */
257   public static final boolean DEFAULT_NORMALIZATION_ENABLED = false;
258 
259   /**
260    * Constant that denotes the maximum default size of the memstore after which
261    * the contents are flushed to the store files
262    */
263   public static final long DEFAULT_MEMSTORE_FLUSH_SIZE = 1024*1024*128L;
264 
265   public static final int DEFAULT_REGION_REPLICATION = 1;
266 
267   public static final boolean DEFAULT_REGION_MEMSTORE_REPLICATION = true;
268 
269   private final static Map<String, String> DEFAULT_VALUES
270     = new HashMap<String, String>();
271   private final static Set<ImmutableBytesWritable> RESERVED_KEYWORDS
272     = new HashSet<ImmutableBytesWritable>();
273   static {
274     DEFAULT_VALUES.put(MAX_FILESIZE,
275         String.valueOf(HConstants.DEFAULT_MAX_FILE_SIZE));
276     DEFAULT_VALUES.put(READONLY, String.valueOf(DEFAULT_READONLY));
277     DEFAULT_VALUES.put(MEMSTORE_FLUSHSIZE,
278         String.valueOf(DEFAULT_MEMSTORE_FLUSH_SIZE));
279     DEFAULT_VALUES.put(DEFERRED_LOG_FLUSH,
280         String.valueOf(DEFAULT_DEFERRED_LOG_FLUSH));
281     DEFAULT_VALUES.put(DURABILITY, DEFAULT_DURABLITY.name()); //use the enum name
282     DEFAULT_VALUES.put(REGION_REPLICATION, String.valueOf(DEFAULT_REGION_REPLICATION));
283     DEFAULT_VALUES.put(NORMALIZATION_ENABLED, String.valueOf(DEFAULT_NORMALIZATION_ENABLED));
284     DEFAULT_VALUES.put(PRIORITY, String.valueOf(DEFAULT_PRIORITY));
285     for (String s : DEFAULT_VALUES.keySet()) {
286       RESERVED_KEYWORDS.add(new ImmutableBytesWritable(Bytes.toBytes(s)));
287     }
288     RESERVED_KEYWORDS.add(IS_ROOT_KEY);
289     RESERVED_KEYWORDS.add(IS_META_KEY);
290   }
291 
292   /**
293    * Cache of whether this is a meta table or not.
294    */
295   private volatile Boolean meta = null;
296   /**
297    * Cache of whether this is root table or not.
298    */
299   private volatile Boolean root = null;
300 
301   /**
302    * Durability setting for the table
303    */
304   private Durability durability = null;
305 
306   /**
307    * Maps column family name to the respective HColumnDescriptors
308    */
309   private final Map<byte [], HColumnDescriptor> families =
310     new TreeMap<byte [], HColumnDescriptor>(Bytes.BYTES_RAWCOMPARATOR);
311 
312   /**
313    * <em> INTERNAL </em> Private constructor used internally creating table descriptors for
314    * catalog tables, <code>hbase:meta</code> and <code>-ROOT-</code>.
315    */
316   @InterfaceAudience.Private
317   protected HTableDescriptor(final TableName name, HColumnDescriptor[] families) {
318     setName(name);
319     for(HColumnDescriptor descriptor : families) {
320       this.families.put(descriptor.getName(), descriptor);
321     }
322   }
323 
324   /**
325    * <em> INTERNAL </em>Private constructor used internally creating table descriptors for
326    * catalog tables, <code>hbase:meta</code> and <code>-ROOT-</code>.
327    */
328   protected HTableDescriptor(final TableName name, HColumnDescriptor[] families,
329       Map<ImmutableBytesWritable,ImmutableBytesWritable> values) {
330     setName(name);
331     for(HColumnDescriptor descriptor : families) {
332       this.families.put(descriptor.getName(), descriptor);
333     }
334     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> entry:
335         values.entrySet()) {
336       setValue(entry.getKey(), entry.getValue());
337     }
338   }
339 
340   /**
341    * Default constructor which constructs an empty object.
342    * For deserializing an HTableDescriptor instance only.
343    * @deprecated As of release 0.96
344    *             (<a href="https://issues.apache.org/jira/browse/HBASE-5453">HBASE-5453</a>).
345    *             This will be removed in HBase 2.0.0.
346    *             Used by Writables and Writables are going away.
347    */
348   @Deprecated
349   public HTableDescriptor() {
350     super();
351   }
352 
353   /**
354    * Construct a table descriptor specifying a TableName object
355    * @param name Table name.
356    * @see <a href="HADOOP-1581">HADOOP-1581 HBASE: Un-openable tablename bug</a>
357    */
358   public HTableDescriptor(final TableName name) {
359     super();
360     setName(name);
361   }
362 
363   /**
364    * Construct a table descriptor specifying a byte array table name
365    * @param name Table name.
366    * @see <a href="HADOOP-1581">HADOOP-1581 HBASE: Un-openable tablename bug</a>
367    */
368   @Deprecated
369   public HTableDescriptor(final byte[] name) {
370     this(TableName.valueOf(name));
371   }
372 
373   /**
374    * Construct a table descriptor specifying a String table name
375    * @param name Table name.
376    * @see <a href="HADOOP-1581">HADOOP-1581 HBASE: Un-openable tablename bug</a>
377    */
378   @Deprecated
379   public HTableDescriptor(final String name) {
380     this(TableName.valueOf(name));
381   }
382 
383   /**
384    * Construct a table descriptor by cloning the descriptor passed as a parameter.
385    * <p>
386    * Makes a deep copy of the supplied descriptor.
387    * Can make a modifiable descriptor from an UnmodifyableHTableDescriptor.
388    * @param desc The descriptor.
389    */
390   public HTableDescriptor(final HTableDescriptor desc) {
391     this(desc.name, desc);
392   }
393 
394   /**
395    * Construct a table descriptor by cloning the descriptor passed as a parameter
396    * but using a different table name.
397    * <p>
398    * Makes a deep copy of the supplied descriptor.
399    * Can make a modifiable descriptor from an UnmodifyableHTableDescriptor.
400    * @param name Table name.
401    * @param desc The descriptor.
402    */
403   public HTableDescriptor(final TableName name, final HTableDescriptor desc) {
404     super();
405     setName(name);
406     setMetaFlags(this.name);
407     for (HColumnDescriptor c: desc.families.values()) {
408       this.families.put(c.getName(), new HColumnDescriptor(c));
409     }
410     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
411         desc.values.entrySet()) {
412       setValue(e.getKey(), e.getValue());
413     }
414     for (Map.Entry<String, String> e : desc.configuration.entrySet()) {
415       this.configuration.put(e.getKey(), e.getValue());
416     }
417   }
418 
419   /*
420    * Set meta flags on this table.
421    * IS_ROOT_KEY is set if its a -ROOT- table
422    * IS_META_KEY is set either if its a -ROOT- or a hbase:meta table
423    * Called by constructors.
424    * @param name
425    */
426   private void setMetaFlags(final TableName name) {
427     setMetaRegion(isRootRegion() ||
428       name.equals(TableName.META_TABLE_NAME));
429   }
430 
431   /**
432    * Check if the descriptor represents a <code> -ROOT- </code> region.
433    *
434    * @return true if this is a <code> -ROOT- </code> region
435    */
436   public boolean isRootRegion() {
437     if (this.root == null) {
438       this.root = isSomething(IS_ROOT_KEY, false)? Boolean.TRUE: Boolean.FALSE;
439     }
440     return this.root.booleanValue();
441   }
442 
443   /**
444    * <em> INTERNAL </em> Used to denote if the current table represents
445    * <code> -ROOT- </code> region. This is used internally by the
446    * HTableDescriptor constructors
447    *
448    * @param isRoot true if this is the <code> -ROOT- </code> region
449    */
450   protected void setRootRegion(boolean isRoot) {
451     // TODO: Make the value a boolean rather than String of boolean.
452     setValue(IS_ROOT_KEY, isRoot ? TRUE : FALSE);
453   }
454 
455   /**
456    * Checks if this table is <code> hbase:meta </code>
457    * region.
458    *
459    * @return true if this table is <code> hbase:meta </code>
460    * region
461    */
462   public boolean isMetaRegion() {
463     if (this.meta == null) {
464       this.meta = calculateIsMetaRegion();
465     }
466     return this.meta.booleanValue();
467   }
468 
469   private synchronized Boolean calculateIsMetaRegion() {
470     byte [] value = getValue(IS_META_KEY);
471     return (value != null)? Boolean.valueOf(Bytes.toString(value)): Boolean.FALSE;
472   }
473 
474   private boolean isSomething(final ImmutableBytesWritable key,
475       final boolean valueIfNull) {
476     byte [] value = getValue(key);
477     if (value != null) {
478       return Boolean.valueOf(Bytes.toString(value));
479     }
480     return valueIfNull;
481   }
482 
483   /**
484    * <em> INTERNAL </em> Used to denote if the current table represents
485    * <code> -ROOT- </code> or <code> hbase:meta </code> region. This is used
486    * internally by the HTableDescriptor constructors
487    *
488    * @param isMeta true if its either <code> -ROOT- </code> or
489    * <code> hbase:meta </code> region
490    */
491   protected void setMetaRegion(boolean isMeta) {
492     setValue(IS_META_KEY, isMeta? TRUE: FALSE);
493   }
494 
495   /**
496    * Checks if the table is a <code>hbase:meta</code> table
497    *
498    * @return true if table is <code> hbase:meta </code> region.
499    */
500   public boolean isMetaTable() {
501     return isMetaRegion() && !isRootRegion();
502   }
503 
504   /**
505    * Getter for accessing the metadata associated with the key
506    *
507    * @param key The key.
508    * @return The value.
509    * @see #values
510    */
511   public byte[] getValue(byte[] key) {
512     return getValue(new ImmutableBytesWritable(key));
513   }
514 
515   private byte[] getValue(final ImmutableBytesWritable key) {
516     ImmutableBytesWritable ibw = values.get(key);
517     if (ibw == null)
518       return null;
519     return ibw.get();
520   }
521 
522   /**
523    * Getter for accessing the metadata associated with the key
524    *
525    * @param key The key.
526    * @return The value.
527    * @see #values
528    */
529   public String getValue(String key) {
530     byte[] value = getValue(Bytes.toBytes(key));
531     if (value == null)
532       return null;
533     return Bytes.toString(value);
534   }
535 
536   /**
537    * Getter for fetching an unmodifiable {@link #values} map.
538    *
539    * @return unmodifiable map {@link #values}.
540    * @see #values
541    */
542   public Map<ImmutableBytesWritable,ImmutableBytesWritable> getValues() {
543     // shallow pointer copy
544     return Collections.unmodifiableMap(values);
545   }
546 
547   /**
548    * Setter for storing metadata as a (key, value) pair in {@link #values} map
549    *
550    * @param key The key.
551    * @param value The value.
552    * @see #values
553    */
554   public HTableDescriptor setValue(byte[] key, byte[] value) {
555     setValue(new ImmutableBytesWritable(key), new ImmutableBytesWritable(value));
556     return this;
557   }
558 
559   /**
560    * @param key The key.
561    * @param value The value.
562    */
563   private HTableDescriptor setValue(final ImmutableBytesWritable key,
564       final String value) {
565     setValue(key, new ImmutableBytesWritable(Bytes.toBytes(value)));
566     return this;
567   }
568 
569   /**
570    * Setter for storing metadata as a (key, value) pair in {@link #values} map
571    *
572    * @param key The key.
573    * @param value The value.
574    */
575   public HTableDescriptor setValue(final ImmutableBytesWritable key,
576       final ImmutableBytesWritable value) {
577     if (key.compareTo(DEFERRED_LOG_FLUSH_KEY) == 0) {
578       boolean isDeferredFlush = Boolean.valueOf(Bytes.toString(value.get()));
579       LOG.warn("HTableDescriptor property:" + DEFERRED_LOG_FLUSH + " is deprecated, " +
580           "use " + DURABILITY + " instead");
581       setDurability(isDeferredFlush ? Durability.ASYNC_WAL : DEFAULT_DURABLITY);
582       return this;
583     }
584     if (value == null || value.getLength() == 0) {
585       remove(key);
586     } else {
587       values.put(key, value);
588     }
589     return this;
590   }
591 
592   /**
593    * Setter for storing metadata as a (key, value) pair in {@link #values} map
594    *
595    * @param key The key.
596    * @param value The value.
597    * @see #values
598    */
599   public HTableDescriptor setValue(String key, String value) {
600     if (value == null || value.length() == 0) {
601       remove(key);
602     } else {
603       setValue(Bytes.toBytes(key), Bytes.toBytes(value));
604     }
605     return this;
606   }
607 
608   /**
609    * Remove metadata represented by the key from the {@link #values} map
610    *
611    * @param key Key whose key and value we're to remove from HTableDescriptor
612    * parameters.
613    */
614   public void remove(final String key) {
615     remove(new ImmutableBytesWritable(Bytes.toBytes(key)));
616   }
617 
618   /**
619    * Remove metadata represented by the key from the {@link #values} map
620    *
621    * @param key Key whose key and value we're to remove from HTableDescriptor
622    * parameters.
623    */
624   public void remove(ImmutableBytesWritable key) {
625     values.remove(key);
626   }
627 
628   /**
629    * Remove metadata represented by the key from the {@link #values} map
630    *
631    * @param key Key whose key and value we're to remove from HTableDescriptor
632    * parameters.
633    */
634   public void remove(final byte [] key) {
635     remove(new ImmutableBytesWritable(key));
636   }
637 
638   /**
639    * Check if the readOnly flag of the table is set. If the readOnly flag is
640    * set then the contents of the table can only be read from but not modified.
641    *
642    * @return true if all columns in the table should be read only
643    */
644   public boolean isReadOnly() {
645     return isSomething(READONLY_KEY, DEFAULT_READONLY);
646   }
647 
648   /**
649    * Setting the table as read only sets all the columns in the table as read
650    * only. By default all tables are modifiable, but if the readOnly flag is
651    * set to true then the contents of the table can only be read but not modified.
652    *
653    * @param readOnly True if all of the columns in the table should be read
654    * only.
655    */
656   public HTableDescriptor setReadOnly(final boolean readOnly) {
657     return setValue(READONLY_KEY, readOnly? TRUE: FALSE);
658   }
659 
660   /**
661    * Check if the compaction enable flag of the table is true. If flag is
662    * false then no minor/major compactions will be done in real.
663    *
664    * @return true if table compaction enabled
665    */
666   public boolean isCompactionEnabled() {
667     return isSomething(COMPACTION_ENABLED_KEY, DEFAULT_COMPACTION_ENABLED);
668   }
669 
670   /**
671    * Setting the table compaction enable flag.
672    *
673    * @param isEnable True if enable compaction.
674    */
675   public HTableDescriptor setCompactionEnabled(final boolean isEnable) {
676     setValue(COMPACTION_ENABLED_KEY, isEnable ? TRUE : FALSE);
677     return this;
678   }
679 
680   /**
681    * Check if normalization enable flag of the table is true. If flag is
682    * false then no region normalizer won't attempt to normalize this table.
683    *
684    * @return true if region normalization is enabled for this table
685    */
686   public boolean isNormalizationEnabled() {
687     return isSomething(NORMALIZATION_ENABLED_KEY, DEFAULT_NORMALIZATION_ENABLED);
688   }
689 
690   /**
691    * Setting the table normalization enable flag.
692    *
693    * @param isEnable True if enable normalization.
694    */
695   public HTableDescriptor setNormalizationEnabled(final boolean isEnable) {
696     setValue(NORMALIZATION_ENABLED_KEY, isEnable ? TRUE : FALSE);
697     return this;
698   }
699 
700   public HTableDescriptor setNormalizerTargetRegionCount(final int regionCount) {
701     setValue(NORMALIZER_TARGET_REGION_COUNT_KEY, Integer.toString(regionCount));
702     return this;
703   }
704 
705   public int getNormalizerTargetRegionCount() {
706     byte [] value = getValue(NORMALIZER_TARGET_REGION_COUNT_KEY);
707     if (value != null) {
708       return Integer.parseInt(Bytes.toString(value));
709     }
710     return -1;
711   }
712 
713   public HTableDescriptor setNormalizerTargetRegionSize(final long regionSize) {
714     setValue(NORMALIZER_TARGET_REGION_SIZE_KEY, Long.toString(regionSize));
715     return this;
716   }
717 
718   public long getNormalizerTargetRegionSize() {
719     byte [] value = getValue(NORMALIZER_TARGET_REGION_SIZE_KEY);
720     if (value != null) {
721       return Long.parseLong(Bytes.toString(value));
722     }
723     return -1;
724   }
725 
726 
727   /**
728    * Sets the {@link Durability} setting for the table. This defaults to Durability.USE_DEFAULT.
729    * @param durability enum value
730    */
731   public HTableDescriptor setDurability(Durability durability) {
732     this.durability = durability;
733     setValue(DURABILITY_KEY, durability.name());
734     return this;
735   }
736 
737   /**
738    * Returns the durability setting for the table.
739    * @return durability setting for the table.
740    */
741   public Durability getDurability() {
742     if (this.durability == null) {
743       byte[] durabilityValue = getValue(DURABILITY_KEY);
744       if (durabilityValue == null) {
745         this.durability = DEFAULT_DURABLITY;
746       } else {
747         try {
748           this.durability = Durability.valueOf(Bytes.toString(durabilityValue));
749         } catch (IllegalArgumentException ex) {
750           LOG.warn("Received " + ex + " because Durability value for HTableDescriptor"
751             + " is not known. Durability:" + Bytes.toString(durabilityValue));
752           this.durability = DEFAULT_DURABLITY;
753         }
754       }
755     }
756     return this.durability;
757   }
758 
759   /**
760    * Get the name of the table
761    *
762    * @return TableName
763    */
764   public TableName getTableName() {
765     return name;
766   }
767 
768   /**
769    * Get the name of the table as a byte array.
770    *
771    * @return name of table
772    * @deprecated Use {@link #getTableName()} instead
773    */
774   @Deprecated
775   public byte[] getName() {
776     return name.getName();
777   }
778 
779   /**
780    * Get the name of the table as a String
781    *
782    * @return name of table as a String
783    */
784   public String getNameAsString() {
785     return name.getNameAsString();
786   }
787 
788   /**
789    * This sets the class associated with the region split policy which
790    * determines when a region split should occur.  The class used by
791    * default is defined in {@link org.apache.hadoop.hbase.regionserver.RegionSplitPolicy}
792    * @param clazz the class name
793    */
794   public HTableDescriptor setRegionSplitPolicyClassName(String clazz) {
795     setValue(SPLIT_POLICY, clazz);
796     return this;
797   }
798 
799   /**
800    * This gets the class associated with the region split policy which
801    * determines when a region split should occur.  The class used by
802    * default is defined in {@link org.apache.hadoop.hbase.regionserver.RegionSplitPolicy}
803    *
804    * @return the class name of the region split policy for this table.
805    * If this returns null, the default split policy is used.
806    */
807    public String getRegionSplitPolicyClassName() {
808     return getValue(SPLIT_POLICY);
809   }
810 
811   /**
812    * Set the name of the table.
813    *
814    * @param name name of table
815    */
816   @Deprecated
817   public HTableDescriptor setName(byte[] name) {
818     setName(TableName.valueOf(name));
819     return this;
820   }
821 
822   @Deprecated
823   public HTableDescriptor setName(TableName name) {
824     this.name = name;
825     setMetaFlags(this.name);
826     return this;
827   }
828 
829   /**
830    * Returns the maximum size upto which a region can grow to after which a region
831    * split is triggered. The region size is represented by the size of the biggest
832    * store file in that region.
833    *
834    * @return max hregion size for table, -1 if not set.
835    *
836    * @see #setMaxFileSize(long)
837    */
838   public long getMaxFileSize() {
839     byte [] value = getValue(MAX_FILESIZE_KEY);
840     if (value != null) {
841       return Long.parseLong(Bytes.toString(value));
842     }
843     return -1;
844   }
845 
846   /**
847    * Sets the maximum size upto which a region can grow to after which a region
848    * split is triggered. The region size is represented by the size of the biggest
849    * store file in that region, i.e. If the biggest store file grows beyond the
850    * maxFileSize, then the region split is triggered. This defaults to a value of
851    * 256 MB.
852    * <p>
853    * This is not an absolute value and might vary. Assume that a single row exceeds
854    * the maxFileSize then the storeFileSize will be greater than maxFileSize since
855    * a single row cannot be split across multiple regions
856    * </p>
857    *
858    * @param maxFileSize The maximum file size that a store file can grow to
859    * before a split is triggered.
860    */
861   public HTableDescriptor setMaxFileSize(long maxFileSize) {
862     setValue(MAX_FILESIZE_KEY, Long.toString(maxFileSize));
863     return this;
864   }
865 
866   /**
867    * Returns the size of the memstore after which a flush to filesystem is triggered.
868    *
869    * @return memory cache flush size for each hregion, -1 if not set.
870    *
871    * @see #setMemStoreFlushSize(long)
872    */
873   public long getMemStoreFlushSize() {
874     byte [] value = getValue(MEMSTORE_FLUSHSIZE_KEY);
875     if (value != null) {
876       return Long.parseLong(Bytes.toString(value));
877     }
878     return -1;
879   }
880 
881   /**
882    * Represents the maximum size of the memstore after which the contents of the
883    * memstore are flushed to the filesystem. This defaults to a size of 64 MB.
884    *
885    * @param memstoreFlushSize memory cache flush size for each hregion
886    */
887   public HTableDescriptor setMemStoreFlushSize(long memstoreFlushSize) {
888     setValue(MEMSTORE_FLUSHSIZE_KEY, Long.toString(memstoreFlushSize));
889     return this;
890   }
891 
892   /**
893    * This sets the class associated with the flush policy which determines determines the stores
894    * need to be flushed when flushing a region. The class used by default is defined in
895    * {@link org.apache.hadoop.hbase.regionserver.FlushPolicy}
896    * @param clazz the class name
897    */
898   public HTableDescriptor setFlushPolicyClassName(String clazz) {
899     setValue(FLUSH_POLICY, clazz);
900     return this;
901   }
902 
903   /**
904    * This gets the class associated with the flush policy which determines the stores need to be
905    * flushed when flushing a region. The class used by default is defined in
906    * {@link org.apache.hadoop.hbase.regionserver.FlushPolicy}
907    * @return the class name of the flush policy for this table. If this returns null, the default
908    *         flush policy is used.
909    */
910   public String getFlushPolicyClassName() {
911     return getValue(FLUSH_POLICY);
912   }
913 
914   /**
915    * Adds a column family.
916    * For the updating purpose please use {@link #modifyFamily(HColumnDescriptor)} instead.
917    * @param family HColumnDescriptor of family to add.
918    */
919   public HTableDescriptor addFamily(final HColumnDescriptor family) {
920     if (family.getName() == null || family.getName().length <= 0) {
921       throw new IllegalArgumentException("Family name cannot be null or empty");
922     }
923     if (hasFamily(family.getName())) {
924       throw new IllegalArgumentException("Family '" +
925         family.getNameAsString() + "' already exists so cannot be added");
926     }
927     this.families.put(family.getName(), family);
928     return this;
929   }
930 
931   /**
932    * Modifies the existing column family.
933    * @param family HColumnDescriptor of family to update
934    * @return this (for chained invocation)
935    */
936   public HTableDescriptor modifyFamily(final HColumnDescriptor family) {
937     if (family.getName() == null || family.getName().length <= 0) {
938       throw new IllegalArgumentException("Family name cannot be null or empty");
939     }
940     if (!hasFamily(family.getName())) {
941       throw new IllegalArgumentException("Column family '" + family.getNameAsString()
942         + "' does not exist");
943     }
944     this.families.put(family.getName(), family);
945     return this;
946   }
947 
948   /**
949    * Checks to see if this table contains the given column family
950    * @param familyName Family name or column name.
951    * @return true if the table contains the specified family name
952    */
953   public boolean hasFamily(final byte [] familyName) {
954     return families.containsKey(familyName);
955   }
956 
957   /**
958    * @return Name of this table and then a map of all of the column family
959    * descriptors.
960    * @see #getNameAsString()
961    */
962   @Override
963   public String toString() {
964     StringBuilder s = new StringBuilder();
965     s.append('\'').append(Bytes.toString(name.getName())).append('\'');
966     s.append(getValues(true));
967     for (HColumnDescriptor f : families.values()) {
968       s.append(", ").append(f);
969     }
970     return s.toString();
971   }
972 
973   /**
974    * @return Name of this table and then a map of all of the column family
975    * descriptors (with only the non-default column family attributes)
976    */
977   public String toStringCustomizedValues() {
978     StringBuilder s = new StringBuilder();
979     s.append('\'').append(Bytes.toString(name.getName())).append('\'');
980     s.append(getValues(false));
981     for(HColumnDescriptor hcd : families.values()) {
982       s.append(", ").append(hcd.toStringCustomizedValues());
983     }
984     return s.toString();
985   }
986 
987   /**
988    * @return map of all table attributes formatted into string.
989    */
990   public String toStringTableAttributes() {
991    return getValues(true).toString();
992   }
993 
994   private StringBuilder getValues(boolean printDefaults) {
995     StringBuilder s = new StringBuilder();
996 
997     // step 1: set partitioning and pruning
998     Set<ImmutableBytesWritable> reservedKeys = new TreeSet<ImmutableBytesWritable>();
999     Set<ImmutableBytesWritable> userKeys = new TreeSet<ImmutableBytesWritable>();
1000     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> entry : values.entrySet()) {
1001       ImmutableBytesWritable k = entry.getKey();
1002       if (k == null || k.get() == null) continue;
1003       String key = Bytes.toString(k.get());
1004       // in this section, print out reserved keywords + coprocessor info
1005       if (!RESERVED_KEYWORDS.contains(k) && !key.startsWith("coprocessor$")) {
1006         userKeys.add(k);
1007         continue;
1008       }
1009       // only print out IS_ROOT/IS_META if true
1010       String value = Bytes.toString(entry.getValue().get());
1011       if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
1012         if (Boolean.valueOf(value) == false) continue;
1013       }
1014       // see if a reserved key is a default value. may not want to print it out
1015       if (printDefaults
1016           || !DEFAULT_VALUES.containsKey(key)
1017           || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
1018         reservedKeys.add(k);
1019       }
1020     }
1021 
1022     // early exit optimization
1023     boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
1024     if (!hasAttributes && configuration.isEmpty()) return s;
1025 
1026     s.append(", {");
1027     // step 2: printing attributes
1028     if (hasAttributes) {
1029       s.append("TABLE_ATTRIBUTES => {");
1030 
1031       // print all reserved keys first
1032       boolean printCommaForAttr = false;
1033       for (ImmutableBytesWritable k : reservedKeys) {
1034         String key = Bytes.toString(k.get());
1035         String value = Bytes.toStringBinary(values.get(k).get());
1036         if (printCommaForAttr) s.append(", ");
1037         printCommaForAttr = true;
1038         s.append(key);
1039         s.append(" => ");
1040         s.append('\'').append(value).append('\'');
1041       }
1042 
1043       if (!userKeys.isEmpty()) {
1044         // print all non-reserved, advanced config keys as a separate subset
1045         if (printCommaForAttr) s.append(", ");
1046         printCommaForAttr = true;
1047         s.append(HConstants.METADATA).append(" => ");
1048         s.append("{");
1049         boolean printCommaForCfg = false;
1050         for (ImmutableBytesWritable k : userKeys) {
1051           String key = Bytes.toString(k.get());
1052           String value = Bytes.toStringBinary(values.get(k).get());
1053           if (printCommaForCfg) s.append(", ");
1054           printCommaForCfg = true;
1055           s.append('\'').append(key).append('\'');
1056           s.append(" => ");
1057           s.append('\'').append(value).append('\'');
1058         }
1059         s.append("}");
1060       }
1061     }
1062 
1063     // step 3: printing all configuration:
1064     if (!configuration.isEmpty()) {
1065       if (hasAttributes) {
1066         s.append(", ");
1067       }
1068       s.append(HConstants.CONFIGURATION).append(" => ");
1069       s.append('{');
1070       boolean printCommaForConfig = false;
1071       for (Map.Entry<String, String> e : configuration.entrySet()) {
1072         if (printCommaForConfig) s.append(", ");
1073         printCommaForConfig = true;
1074         s.append('\'').append(e.getKey()).append('\'');
1075         s.append(" => ");
1076         s.append('\'').append(e.getValue()).append('\'');
1077       }
1078       s.append("}");
1079     }
1080     s.append("}"); // end METHOD
1081     return s;
1082   }
1083 
1084   /**
1085    * Compare the contents of the descriptor with another one passed as a parameter.
1086    * Checks if the obj passed is an instance of HTableDescriptor, if yes then the
1087    * contents of the descriptors are compared.
1088    *
1089    * @return true if the contents of the the two descriptors exactly match
1090    *
1091    * @see java.lang.Object#equals(java.lang.Object)
1092    */
1093   @Override
1094   public boolean equals(Object obj) {
1095     if (this == obj) {
1096       return true;
1097     }
1098     if (obj == null) {
1099       return false;
1100     }
1101     if (!(obj instanceof HTableDescriptor)) {
1102       return false;
1103     }
1104     return compareTo((HTableDescriptor)obj) == 0;
1105   }
1106 
1107 
1108   /**
1109    * @see java.lang.Object#hashCode()
1110    */
1111   @Override
1112   public int hashCode() {
1113     int result = this.name.hashCode();
1114     result ^= Byte.valueOf(TABLE_DESCRIPTOR_VERSION).hashCode();
1115     if (this.families != null && this.families.size() > 0) {
1116       for (HColumnDescriptor e: this.families.values()) {
1117         result ^= e.hashCode();
1118       }
1119     }
1120     result ^= values.hashCode();
1121     result ^= configuration.hashCode();
1122     return result;
1123   }
1124 
1125   /**
1126    * <em> INTERNAL </em> This method is a part of {@link WritableComparable} interface
1127    * and is used for de-serialization of the HTableDescriptor over RPC
1128    * @deprecated Writables are going away.  Use pb {@link #parseFrom(byte[])} instead.
1129    */
1130   @Deprecated
1131   @Override
1132   public void readFields(DataInput in) throws IOException {
1133     int version = in.readInt();
1134     if (version < 3)
1135       throw new IOException("versions < 3 are not supported (and never existed!?)");
1136     // version 3+
1137     name = TableName.valueOf(Bytes.readByteArray(in));
1138     setRootRegion(in.readBoolean());
1139     setMetaRegion(in.readBoolean());
1140     values.clear();
1141     configuration.clear();
1142     int numVals = in.readInt();
1143     for (int i = 0; i < numVals; i++) {
1144       ImmutableBytesWritable key = new ImmutableBytesWritable();
1145       ImmutableBytesWritable value = new ImmutableBytesWritable();
1146       key.readFields(in);
1147       value.readFields(in);
1148       setValue(key, value);
1149     }
1150     families.clear();
1151     int numFamilies = in.readInt();
1152     for (int i = 0; i < numFamilies; i++) {
1153       HColumnDescriptor c = new HColumnDescriptor();
1154       c.readFields(in);
1155       families.put(c.getName(), c);
1156     }
1157     if (version >= 7) {
1158       int numConfigs = in.readInt();
1159       for (int i = 0; i < numConfigs; i++) {
1160         ImmutableBytesWritable key = new ImmutableBytesWritable();
1161         ImmutableBytesWritable value = new ImmutableBytesWritable();
1162         key.readFields(in);
1163         value.readFields(in);
1164         configuration.put(
1165           Bytes.toString(key.get(), key.getOffset(), key.getLength()),
1166           Bytes.toString(value.get(), value.getOffset(), value.getLength()));
1167       }
1168     }
1169   }
1170 
1171   /**
1172    * <em> INTERNAL </em> This method is a part of {@link WritableComparable} interface
1173    * and is used for serialization of the HTableDescriptor over RPC
1174    * @deprecated Writables are going away.
1175    * Use {@link com.google.protobuf.MessageLite#toByteArray} instead.
1176    */
1177   @Deprecated
1178   @Override
1179   public void write(DataOutput out) throws IOException {
1180     out.writeInt(TABLE_DESCRIPTOR_VERSION);
1181     Bytes.writeByteArray(out, name.toBytes());
1182     out.writeBoolean(isRootRegion());
1183     out.writeBoolean(isMetaRegion());
1184     out.writeInt(values.size());
1185     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1186         values.entrySet()) {
1187       e.getKey().write(out);
1188       e.getValue().write(out);
1189     }
1190     out.writeInt(families.size());
1191     for(Iterator<HColumnDescriptor> it = families.values().iterator();
1192         it.hasNext(); ) {
1193       HColumnDescriptor family = it.next();
1194       family.write(out);
1195     }
1196     out.writeInt(configuration.size());
1197     for (Map.Entry<String, String> e : configuration.entrySet()) {
1198       new ImmutableBytesWritable(Bytes.toBytes(e.getKey())).write(out);
1199       new ImmutableBytesWritable(Bytes.toBytes(e.getValue())).write(out);
1200     }
1201   }
1202 
1203   // Comparable
1204 
1205   /**
1206    * Compares the descriptor with another descriptor which is passed as a parameter.
1207    * This compares the content of the two descriptors and not the reference.
1208    *
1209    * @return 0 if the contents of the descriptors are exactly matching,
1210    *         1 if there is a mismatch in the contents
1211    */
1212   @Override
1213   public int compareTo(final HTableDescriptor other) {
1214     int result = this.name.compareTo(other.name);
1215     if (result == 0) {
1216       result = families.size() - other.families.size();
1217     }
1218     if (result == 0 && families.size() != other.families.size()) {
1219       result = Integer.compare(families.size(), other.families.size());
1220     }
1221     if (result == 0) {
1222       for (Iterator<HColumnDescriptor> it = families.values().iterator(),
1223           it2 = other.families.values().iterator(); it.hasNext(); ) {
1224         result = it.next().compareTo(it2.next());
1225         if (result != 0) {
1226           break;
1227         }
1228       }
1229     }
1230     if (result == 0) {
1231       // punt on comparison for ordering, just calculate difference
1232       result = this.values.hashCode() - other.values.hashCode();
1233       if (result < 0)
1234         result = -1;
1235       else if (result > 0)
1236         result = 1;
1237     }
1238     if (result == 0) {
1239       result = this.configuration.hashCode() - other.configuration.hashCode();
1240       if (result < 0)
1241         result = -1;
1242       else if (result > 0)
1243         result = 1;
1244     }
1245     return result;
1246   }
1247 
1248   /**
1249    * Returns an unmodifiable collection of all the {@link HColumnDescriptor}
1250    * of all the column families of the table.
1251    *
1252    * @return Immutable collection of {@link HColumnDescriptor} of all the
1253    * column families.
1254    */
1255   public Collection<HColumnDescriptor> getFamilies() {
1256     return Collections.unmodifiableCollection(this.families.values());
1257   }
1258 
1259   /**
1260    * Returns the configured replicas per region
1261    */
1262   public int getRegionReplication() {
1263     return getIntValue(REGION_REPLICATION_KEY, DEFAULT_REGION_REPLICATION);
1264   }
1265 
1266   private int getIntValue(ImmutableBytesWritable key, int defaultVal) {
1267     byte[] val = getValue(key);
1268     if (val == null || val.length == 0) {
1269       return defaultVal;
1270     }
1271     return Integer.parseInt(Bytes.toString(val));
1272   }
1273 
1274   /**
1275    * Sets the number of replicas per region.
1276    * @param regionReplication the replication factor per region
1277    */
1278   public HTableDescriptor setRegionReplication(int regionReplication) {
1279     setValue(REGION_REPLICATION_KEY,
1280         new ImmutableBytesWritable(Bytes.toBytes(Integer.toString(regionReplication))));
1281     return this;
1282   }
1283 
1284   /**
1285    * @return true if the read-replicas memstore replication is enabled.
1286    */
1287   public boolean hasRegionMemstoreReplication() {
1288     return isSomething(REGION_MEMSTORE_REPLICATION_KEY, DEFAULT_REGION_MEMSTORE_REPLICATION);
1289   }
1290 
1291   /**
1292    * Enable or Disable the memstore replication from the primary region to the replicas.
1293    * The replication will be used only for meta operations (e.g. flush, compaction, ...)
1294    *
1295    * @param memstoreReplication true if the new data written to the primary region
1296    *                                 should be replicated.
1297    *                            false if the secondaries can tollerate to have new
1298    *                                  data only when the primary flushes the memstore.
1299    */
1300   public HTableDescriptor setRegionMemstoreReplication(boolean memstoreReplication) {
1301     setValue(REGION_MEMSTORE_REPLICATION_KEY, memstoreReplication ? TRUE : FALSE);
1302     // If the memstore replication is setup, we do not have to wait for observing a flush event
1303     // from primary before starting to serve reads, because gaps from replication is not applicable
1304     setConfiguration(RegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
1305       Boolean.toString(memstoreReplication));
1306     return this;
1307   }
1308 
1309   public HTableDescriptor setPriority(int priority) {
1310     setValue(PRIORITY_KEY, Integer.toString(priority));
1311     return this;
1312   }
1313 
1314   public int getPriority() {
1315     return getIntValue(PRIORITY_KEY, DEFAULT_PRIORITY);
1316   }
1317 
1318   /**
1319    * Returns all the column family names of the current table. The map of
1320    * HTableDescriptor contains mapping of family name to HColumnDescriptors.
1321    * This returns all the keys of the family map which represents the column
1322    * family names of the table.
1323    *
1324    * @return Immutable sorted set of the keys of the families.
1325    */
1326   public Set<byte[]> getFamiliesKeys() {
1327     return Collections.unmodifiableSet(this.families.keySet());
1328   }
1329 
1330   /**
1331    * Returns an array all the {@link HColumnDescriptor} of the column families
1332    * of the table.
1333    *
1334    * @return Array of all the HColumnDescriptors of the current table
1335    *
1336    * @see #getFamilies()
1337    */
1338   public HColumnDescriptor[] getColumnFamilies() {
1339     Collection<HColumnDescriptor> hColumnDescriptors = getFamilies();
1340     return hColumnDescriptors.toArray(new HColumnDescriptor[hColumnDescriptors.size()]);
1341   }
1342 
1343 
1344   /**
1345    * Returns the HColumnDescriptor for a specific column family with name as
1346    * specified by the parameter column.
1347    *
1348    * @param column Column family name
1349    * @return Column descriptor for the passed family name or the family on
1350    * passed in column.
1351    */
1352   public HColumnDescriptor getFamily(final byte [] column) {
1353     return this.families.get(column);
1354   }
1355 
1356 
1357   /**
1358    * Removes the HColumnDescriptor with name specified by the parameter column
1359    * from the table descriptor
1360    *
1361    * @param column Name of the column family to be removed.
1362    * @return Column descriptor for the passed family name or the family on
1363    * passed in column.
1364    */
1365   public HColumnDescriptor removeFamily(final byte [] column) {
1366     return this.families.remove(column);
1367   }
1368 
1369   /**
1370    * Add a table coprocessor to this table. The coprocessor
1371    * type must be {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
1372    * or Endpoint.
1373    * It won't check if the class can be loaded or not.
1374    * Whether a coprocessor is loadable or not will be determined when
1375    * a region is opened.
1376    * @param className Full class name.
1377    * @throws IOException
1378    */
1379   public HTableDescriptor addCoprocessor(String className) throws IOException {
1380     addCoprocessor(className, null, Coprocessor.PRIORITY_USER, null);
1381     return this;
1382   }
1383 
1384   /**
1385    * Add a table coprocessor to this table. The coprocessor
1386    * type must be {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
1387    * or Endpoint.
1388    * It won't check if the class can be loaded or not.
1389    * Whether a coprocessor is loadable or not will be determined when
1390    * a region is opened.
1391    * @param jarFilePath Path of the jar file. If it's null, the class will be
1392    * loaded from default classloader.
1393    * @param className Full class name.
1394    * @param priority Priority
1395    * @param kvs Arbitrary key-value parameter pairs passed into the coprocessor.
1396    * @throws IOException
1397    */
1398   public HTableDescriptor addCoprocessor(String className, Path jarFilePath,
1399                              int priority, final Map<String, String> kvs)
1400   throws IOException {
1401     checkHasCoprocessor(className);
1402 
1403     // Validate parameter kvs and then add key/values to kvString.
1404     StringBuilder kvString = new StringBuilder();
1405     if (kvs != null) {
1406       for (Map.Entry<String, String> e: kvs.entrySet()) {
1407         if (!e.getKey().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1408           throw new IOException("Illegal parameter key = " + e.getKey());
1409         }
1410         if (!e.getValue().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1411           throw new IOException("Illegal parameter (" + e.getKey() +
1412               ") value = " + e.getValue());
1413         }
1414         if (kvString.length() != 0) {
1415           kvString.append(',');
1416         }
1417         kvString.append(e.getKey());
1418         kvString.append('=');
1419         kvString.append(e.getValue());
1420       }
1421     }
1422 
1423     String value = ((jarFilePath == null)? "" : jarFilePath.toString()) +
1424         "|" + className + "|" + Integer.toString(priority) + "|" +
1425         kvString.toString();
1426     return addCoprocessorToMap(value);
1427   }
1428 
1429   /**
1430    * Add a table coprocessor to this table. The coprocessor
1431    * type must be {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
1432    * or Endpoint.
1433    * It won't check if the class can be loaded or not.
1434    * Whether a coprocessor is loadable or not will be determined when
1435    * a region is opened.
1436    * @param specStr The Coprocessor specification all in in one String formatted so matches
1437    * {@link HConstants#CP_HTD_ATTR_VALUE_PATTERN}
1438    * @throws IOException
1439    */
1440   public HTableDescriptor addCoprocessorWithSpec(final String specStr) throws IOException {
1441     String className = getCoprocessorClassNameFromSpecStr(specStr);
1442     if (className == null) {
1443       throw new IllegalArgumentException("Format does not match " +
1444         HConstants.CP_HTD_ATTR_VALUE_PATTERN + ": " + specStr);
1445     }
1446     checkHasCoprocessor(className);
1447     return addCoprocessorToMap(specStr);
1448   }
1449 
1450   private void checkHasCoprocessor(final String className) throws IOException {
1451     if (hasCoprocessor(className)) {
1452       throw new IOException("Coprocessor " + className + " already exists.");
1453     }
1454   }
1455 
1456   /**
1457    * Add coprocessor to values Map
1458    * @param specStr The Coprocessor specification all in in one String formatted so matches
1459    * {@link HConstants#CP_HTD_ATTR_VALUE_PATTERN}
1460    * @return Returns <code>this</code>
1461    */
1462   private HTableDescriptor addCoprocessorToMap(final String specStr) {
1463     if (specStr == null) return this;
1464     // generate a coprocessor key
1465     int maxCoprocessorNumber = 0;
1466     Matcher keyMatcher;
1467     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1468         this.values.entrySet()) {
1469       keyMatcher =
1470           HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1471               Bytes.toString(e.getKey().get()));
1472       if (!keyMatcher.matches()) {
1473         continue;
1474       }
1475       maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)), maxCoprocessorNumber);
1476     }
1477     maxCoprocessorNumber++;
1478     String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1479     this.values.put(new ImmutableBytesWritable(Bytes.toBytes(key)),
1480       new ImmutableBytesWritable(Bytes.toBytes(specStr)));
1481     return this;
1482   }
1483 
1484   /**
1485    * Check if the table has an attached co-processor represented by the name className
1486    *
1487    * @param classNameToMatch - Class name of the co-processor
1488    * @return true of the table has a co-processor className
1489    */
1490   public boolean hasCoprocessor(String classNameToMatch) {
1491     Matcher keyMatcher;
1492     Matcher valueMatcher;
1493     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1494         this.values.entrySet()) {
1495       keyMatcher =
1496           HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1497               Bytes.toString(e.getKey().get()));
1498       if (!keyMatcher.matches()) {
1499         continue;
1500       }
1501       String className = getCoprocessorClassNameFromSpecStr(Bytes.toString(e.getValue().get()));
1502       if (className == null) continue;
1503       if (className.equals(classNameToMatch.trim())) {
1504         return true;
1505       }
1506     }
1507     return false;
1508   }
1509 
1510   /**
1511    * Return the list of attached co-processor represented by their name className
1512    *
1513    * @return The list of co-processors classNames
1514    */
1515   public List<String> getCoprocessors() {
1516     List<String> result = new ArrayList<String>();
1517     Matcher keyMatcher;
1518     Matcher valueMatcher;
1519     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values.entrySet()) {
1520       keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1521       if (!keyMatcher.matches()) {
1522         continue;
1523       }
1524       String className = getCoprocessorClassNameFromSpecStr(Bytes.toString(e.getValue().get()));
1525       if (className == null) continue;
1526       result.add(className); // classname is the 2nd field
1527     }
1528     return result;
1529   }
1530 
1531   /**
1532    * @param spec String formatted as per {@link HConstants#CP_HTD_ATTR_VALUE_PATTERN}
1533    * @return Class parsed from passed in <code>spec</code> or null if no match or classpath found
1534    */
1535   private static String getCoprocessorClassNameFromSpecStr(final String spec) {
1536     Matcher matcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(spec);
1537     // Classname is the 2nd field
1538     return matcher != null && matcher.matches()? matcher.group(2).trim(): null;
1539   }
1540 
1541   /**
1542    * Remove a coprocessor from those set on the table
1543    * @param className Class name of the co-processor
1544    */
1545   public void removeCoprocessor(String className) {
1546     ImmutableBytesWritable match = null;
1547     Matcher keyMatcher;
1548     Matcher valueMatcher;
1549     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values
1550         .entrySet()) {
1551       keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1552           .getKey().get()));
1553       if (!keyMatcher.matches()) {
1554         continue;
1555       }
1556       valueMatcher = HConstants.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       remove(match);
1572   }
1573 
1574   /**
1575    * Returns the {@link Path} object representing the table directory under
1576    * path rootdir
1577    *
1578    * Deprecated use FSUtils.getTableDir() instead.
1579    *
1580    * @param rootdir qualified path of HBase root directory
1581    * @param tableName name of table
1582    * @return {@link Path} for table
1583    */
1584   @Deprecated
1585   public static Path getTableDir(Path rootdir, final byte [] tableName) {
1586     //This is bad I had to mirror code from FSUTils.getTableDir since
1587     //there is no module dependency between hbase-client and hbase-server
1588     TableName name = TableName.valueOf(tableName);
1589     return new Path(rootdir, new Path(HConstants.BASE_NAMESPACE_DIR,
1590               new Path(name.getNamespaceAsString(), new Path(name.getQualifierAsString()))));
1591   }
1592 
1593   /**
1594    * Table descriptor for <code>hbase:meta</code> catalog table
1595    * @deprecated Use TableDescriptors#get(TableName.META_TABLE_NAME) or
1596    * HBaseAdmin#getTableDescriptor(TableName.META_TABLE_NAME) instead.
1597    */
1598   @Deprecated
1599   public static final HTableDescriptor META_TABLEDESC = new HTableDescriptor(
1600       TableName.META_TABLE_NAME,
1601       new HColumnDescriptor[] {
1602           new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1603               // Ten is arbitrary number.  Keep versions to help debugging.
1604               .setMaxVersions(HConstants.DEFAULT_HBASE_META_VERSIONS)
1605               .setInMemory(true)
1606               .setBlocksize(HConstants.DEFAULT_HBASE_META_BLOCK_SIZE)
1607               .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1608               // Disable blooms for meta.  Needs work.  Seems to mess w/ getClosestOrBefore.
1609               .setBloomFilterType(BloomType.NONE)
1610               // Enable cache of data blocks in L1 if more than one caching tier deployed:
1611               // e.g. if using CombinedBlockCache (BucketCache).
1612               .setCacheDataInL1(true)
1613       });
1614 
1615   static {
1616     try {
1617       META_TABLEDESC.addCoprocessor(
1618           "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1619           null, Coprocessor.PRIORITY_SYSTEM, null);
1620     } catch (IOException ex) {
1621       //LOG.warn("exception in loading coprocessor for the hbase:meta table");
1622       throw new RuntimeException(ex);
1623     }
1624   }
1625 
1626   public final static String NAMESPACE_FAMILY_INFO = "info";
1627   public final static byte[] NAMESPACE_FAMILY_INFO_BYTES = Bytes.toBytes(NAMESPACE_FAMILY_INFO);
1628   public final static byte[] NAMESPACE_COL_DESC_BYTES = Bytes.toBytes("d");
1629 
1630   /** Table descriptor for namespace table */
1631   public static final HTableDescriptor NAMESPACE_TABLEDESC = new HTableDescriptor(
1632       TableName.NAMESPACE_TABLE_NAME,
1633       new HColumnDescriptor[] {
1634           new HColumnDescriptor(NAMESPACE_FAMILY_INFO)
1635               // Ten is arbitrary number.  Keep versions to help debugging.
1636               .setMaxVersions(10)
1637               .setInMemory(true)
1638               .setBlocksize(8 * 1024)
1639               .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1640               // Enable cache of data blocks in L1 if more than one caching tier deployed:
1641               // e.g. if using CombinedBlockCache (BucketCache).
1642               .setCacheDataInL1(true)
1643       });
1644 
1645   /**
1646    * @deprecated since 0.94.1
1647    * @see <a href="https://issues.apache.org/jira/browse/HBASE-6188">HBASE-6188</a>
1648    */
1649   @Deprecated
1650   public HTableDescriptor setOwner(User owner) {
1651     return setOwnerString(owner != null ? owner.getShortName() : null);
1652   }
1653 
1654   /**
1655    * @deprecated since 0.94.1
1656    * @see <a href="https://issues.apache.org/jira/browse/HBASE-6188">HBASE-6188</a>
1657    */
1658   // used by admin.rb:alter(table_name,*args) to update owner.
1659   @Deprecated
1660   public HTableDescriptor setOwnerString(String ownerString) {
1661     if (ownerString != null) {
1662       setValue(OWNER_KEY, ownerString);
1663     } else {
1664       remove(OWNER_KEY);
1665     }
1666     return this;
1667   }
1668 
1669   /**
1670    * @deprecated since 0.94.1
1671    * @see <a href="https://issues.apache.org/jira/browse/HBASE-6188">HBASE-6188</a>
1672    */
1673   @Deprecated
1674   public String getOwnerString() {
1675     if (getValue(OWNER_KEY) != null) {
1676       return Bytes.toString(getValue(OWNER_KEY));
1677     }
1678     // Note that every table should have an owner (i.e. should have OWNER_KEY set).
1679     // hbase:meta and -ROOT- should return system user as owner, not null (see
1680     // MasterFileSystem.java:bootstrap()).
1681     return null;
1682   }
1683 
1684   /**
1685    * @return This instance serialized with pb with pb magic prefix
1686    * @see #parseFrom(byte[])
1687    */
1688   public byte [] toByteArray() {
1689     return ProtobufUtil.prependPBMagic(convert().toByteArray());
1690   }
1691 
1692   /**
1693    * @param bytes A pb serialized {@link HTableDescriptor} instance with pb magic prefix
1694    * @return An instance of {@link HTableDescriptor} made from <code>bytes</code>
1695    * @throws DeserializationException
1696    * @throws IOException
1697    * @see #toByteArray()
1698    */
1699   public static HTableDescriptor parseFrom(final byte [] bytes)
1700   throws DeserializationException, IOException {
1701     if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
1702       return (HTableDescriptor)Writables.getWritable(bytes, new HTableDescriptor());
1703     }
1704     int pblen = ProtobufUtil.lengthOfPBMagic();
1705     TableSchema.Builder builder = TableSchema.newBuilder();
1706     TableSchema ts;
1707     try {
1708       ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
1709       ts = builder.build();
1710       return convert(ts);
1711     } catch (IOException | IllegalArgumentException e) {
1712       // Deserialization may not fail but can return garbage that fails eventual validations and
1713       // hence IAE.
1714       throw new DeserializationException(e);
1715     }
1716   }
1717 
1718 
1719 
1720   /**
1721    * @return Convert the current {@link HTableDescriptor} into a pb TableSchema instance.
1722    */
1723   @Deprecated
1724   public TableSchema convert() {
1725     TableSchema.Builder builder = TableSchema.newBuilder();
1726     builder.setTableName(ProtobufUtil.toProtoTableName(getTableName()));
1727     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e: this.values.entrySet()) {
1728       BytesBytesPair.Builder aBuilder = BytesBytesPair.newBuilder();
1729       aBuilder.setFirst(ByteStringer.wrap(e.getKey().get()));
1730       aBuilder.setSecond(ByteStringer.wrap(e.getValue().get()));
1731       builder.addAttributes(aBuilder.build());
1732     }
1733     for (HColumnDescriptor hcd: getColumnFamilies()) {
1734       builder.addColumnFamilies(hcd.convert());
1735     }
1736     for (Map.Entry<String, String> e : this.configuration.entrySet()) {
1737       NameStringPair.Builder aBuilder = NameStringPair.newBuilder();
1738       aBuilder.setName(e.getKey());
1739       aBuilder.setValue(e.getValue());
1740       builder.addConfiguration(aBuilder.build());
1741     }
1742     return builder.build();
1743   }
1744 
1745   /**
1746    * @param ts A pb TableSchema instance.
1747    * @return An {@link HTableDescriptor} made from the passed in pb <code>ts</code>.
1748    */
1749   @Deprecated
1750   public static HTableDescriptor convert(final TableSchema ts) {
1751     List<ColumnFamilySchema> list = ts.getColumnFamiliesList();
1752     HColumnDescriptor [] hcds = new HColumnDescriptor[list.size()];
1753     int index = 0;
1754     for (ColumnFamilySchema cfs: list) {
1755       hcds[index++] = HColumnDescriptor.convert(cfs);
1756     }
1757     HTableDescriptor htd = new HTableDescriptor(
1758         ProtobufUtil.toTableName(ts.getTableName()),
1759         hcds);
1760     for (BytesBytesPair a: ts.getAttributesList()) {
1761       htd.setValue(a.getFirst().toByteArray(), a.getSecond().toByteArray());
1762     }
1763     for (NameStringPair a: ts.getConfigurationList()) {
1764       htd.setConfiguration(a.getName(), a.getValue());
1765     }
1766     return htd;
1767   }
1768 
1769   /**
1770    * Getter for accessing the configuration value by key
1771    */
1772   public String getConfigurationValue(String key) {
1773     return configuration.get(key);
1774   }
1775 
1776   /**
1777    * Getter for fetching an unmodifiable {@link #configuration} map.
1778    */
1779   public Map<String, String> getConfiguration() {
1780     // shallow pointer copy
1781     return Collections.unmodifiableMap(configuration);
1782   }
1783 
1784   /**
1785    * Setter for storing a configuration setting in {@link #configuration} map.
1786    * @param key Config key. Same as XML config key e.g. hbase.something.or.other.
1787    * @param value String value. If null, removes the setting.
1788    */
1789   public HTableDescriptor setConfiguration(String key, String value) {
1790     if (value == null || value.length() == 0) {
1791       removeConfiguration(key);
1792     } else {
1793       configuration.put(key, value);
1794     }
1795     return this;
1796   }
1797 
1798   /**
1799    * Remove a config setting represented by the key from the {@link #configuration} map
1800    */
1801   public void removeConfiguration(final String key) {
1802     configuration.remove(key);
1803   }
1804 
1805   public static HTableDescriptor metaTableDescriptor(final Configuration conf)
1806       throws IOException {
1807     HTableDescriptor metaDescriptor = new HTableDescriptor(
1808       TableName.META_TABLE_NAME,
1809       new HColumnDescriptor[] {
1810         new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1811           .setMaxVersions(conf.getInt(HConstants.HBASE_META_VERSIONS,
1812             HConstants.DEFAULT_HBASE_META_VERSIONS))
1813           .setInMemory(true)
1814           .setBlocksize(conf.getInt(HConstants.HBASE_META_BLOCK_SIZE,
1815             HConstants.DEFAULT_HBASE_META_BLOCK_SIZE))
1816           .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1817           // Disable blooms for meta.  Needs work.  Seems to mess w/ getClosestOrBefore.
1818           .setBloomFilterType(BloomType.NONE)
1819           .setCacheDataInL1(true)
1820          });
1821     metaDescriptor.addCoprocessor(
1822       "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1823       null, Coprocessor.PRIORITY_SYSTEM, null);
1824     return metaDescriptor;
1825   }
1826 
1827 }