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