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