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