View Javadoc

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