View Javadoc

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