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   /**
839    * @return map of all table attributes formatted into string.
840    */
841   public String toStringTableAttributes() {
842    return getValues(true).toString();
843   }
844 
845   private StringBuilder getValues(boolean printDefaults) {
846     StringBuilder s = new StringBuilder();
847 
848     // step 1: set partitioning and pruning
849     Set<ImmutableBytesWritable> reservedKeys = new TreeSet<ImmutableBytesWritable>();
850     Set<ImmutableBytesWritable> userKeys = new TreeSet<ImmutableBytesWritable>();
851     for (ImmutableBytesWritable k : values.keySet()) {
852       if (k == null || k.get() == null) continue;
853       String key = Bytes.toString(k.get());
854       // in this section, print out reserved keywords + coprocessor info
855       if (!RESERVED_KEYWORDS.contains(k) && !key.startsWith("coprocessor$")) {
856         userKeys.add(k);
857         continue;
858       }
859       // only print out IS_ROOT/IS_META if true
860       String value = Bytes.toString(values.get(k).get());
861       if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
862         if (Boolean.valueOf(value) == false) continue;
863       }
864       // see if a reserved key is a default value. may not want to print it out
865       if (printDefaults
866           || !DEFAULT_VALUES.containsKey(key)
867           || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
868         reservedKeys.add(k);
869       }
870     }
871 
872     // early exit optimization
873     boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
874     if (!hasAttributes && configuration.isEmpty()) return s;
875 
876     s.append(", {");
877     // step 2: printing attributes
878     if (hasAttributes) {
879       s.append("TABLE_ATTRIBUTES => {");
880 
881       // print all reserved keys first
882       boolean printCommaForAttr = false;
883       for (ImmutableBytesWritable k : reservedKeys) {
884         String key = Bytes.toString(k.get());
885         String value = Bytes.toStringBinary(values.get(k).get());
886         if (printCommaForAttr) s.append(", ");
887         printCommaForAttr = true;
888         s.append(key);
889         s.append(" => ");
890         s.append('\'').append(value).append('\'');
891       }
892 
893       if (!userKeys.isEmpty()) {
894         // print all non-reserved, advanced config keys as a separate subset
895         if (printCommaForAttr) s.append(", ");
896         printCommaForAttr = true;
897         s.append(HConstants.METADATA).append(" => ");
898         s.append("{");
899         boolean printCommaForCfg = false;
900         for (ImmutableBytesWritable k : userKeys) {
901           String key = Bytes.toString(k.get());
902           String value = Bytes.toStringBinary(values.get(k).get());
903           if (printCommaForCfg) s.append(", ");
904           printCommaForCfg = true;
905           s.append('\'').append(key).append('\'');
906           s.append(" => ");
907           s.append('\'').append(value).append('\'');
908         }
909         s.append("}");
910       }
911     }
912 
913     // step 3: printing all configuration:
914     if (!configuration.isEmpty()) {
915       if (hasAttributes) {
916         s.append(", ");
917       }
918       s.append(HConstants.CONFIGURATION).append(" => ");
919       s.append('{');
920       boolean printCommaForConfig = false;
921       for (Map.Entry<String, String> e : configuration.entrySet()) {
922         if (printCommaForConfig) s.append(", ");
923         printCommaForConfig = true;
924         s.append('\'').append(e.getKey()).append('\'');
925         s.append(" => ");
926         s.append('\'').append(e.getValue()).append('\'');
927       }
928       s.append("}");
929     }
930     s.append("}"); // end METHOD
931     return s;
932   }
933 
934   /**
935    * Compare the contents of the descriptor with another one passed as a parameter.
936    * Checks if the obj passed is an instance of HTableDescriptor, if yes then the
937    * contents of the descriptors are compared.
938    *
939    * @return true if the contents of the the two descriptors exactly match
940    *
941    * @see java.lang.Object#equals(java.lang.Object)
942    */
943   @Override
944   public boolean equals(Object obj) {
945     if (this == obj) {
946       return true;
947     }
948     if (obj == null) {
949       return false;
950     }
951     if (!(obj instanceof HTableDescriptor)) {
952       return false;
953     }
954     return compareTo((HTableDescriptor)obj) == 0;
955   }
956 
957   /**
958    * @see java.lang.Object#hashCode()
959    */
960   @Override
961   public int hashCode() {
962     int result = this.name.hashCode();
963     result ^= Byte.valueOf(TABLE_DESCRIPTOR_VERSION).hashCode();
964     if (this.families != null && this.families.size() > 0) {
965       for (HColumnDescriptor e: this.families.values()) {
966         result ^= e.hashCode();
967       }
968     }
969     result ^= values.hashCode();
970     result ^= configuration.hashCode();
971     return result;
972   }
973 
974   /**
975    * <em> INTERNAL </em> This method is a part of {@link WritableComparable} interface
976    * and is used for de-serialization of the HTableDescriptor over RPC
977    * @deprecated Writables are going away.  Use pb {@link #parseFrom(byte[])} instead.
978    */
979   @Deprecated
980   @Override
981   public void readFields(DataInput in) throws IOException {
982     int version = in.readInt();
983     if (version < 3)
984       throw new IOException("versions < 3 are not supported (and never existed!?)");
985     // version 3+
986     name = TableName.valueOf(Bytes.readByteArray(in));
987     setRootRegion(in.readBoolean());
988     setMetaRegion(in.readBoolean());
989     values.clear();
990     configuration.clear();
991     int numVals = in.readInt();
992     for (int i = 0; i < numVals; i++) {
993       ImmutableBytesWritable key = new ImmutableBytesWritable();
994       ImmutableBytesWritable value = new ImmutableBytesWritable();
995       key.readFields(in);
996       value.readFields(in);
997       setValue(key, value);
998     }
999     families.clear();
1000     int numFamilies = in.readInt();
1001     for (int i = 0; i < numFamilies; i++) {
1002       HColumnDescriptor c = new HColumnDescriptor();
1003       c.readFields(in);
1004       families.put(c.getName(), c);
1005     }
1006     if (version >= 7) {
1007       int numConfigs = in.readInt();
1008       for (int i = 0; i < numConfigs; i++) {
1009         ImmutableBytesWritable key = new ImmutableBytesWritable();
1010         ImmutableBytesWritable value = new ImmutableBytesWritable();
1011         key.readFields(in);
1012         value.readFields(in);
1013         configuration.put(
1014           Bytes.toString(key.get(), key.getOffset(), key.getLength()),
1015           Bytes.toString(value.get(), value.getOffset(), value.getLength()));
1016       }
1017     }
1018   }
1019 
1020   /**
1021    * <em> INTERNAL </em> This method is a part of {@link WritableComparable} interface
1022    * and is used for serialization of the HTableDescriptor over RPC
1023    * @deprecated Writables are going away.
1024    * Use {@link com.google.protobuf.MessageLite#toByteArray} instead.
1025    */
1026   @Deprecated
1027   @Override
1028   public void write(DataOutput out) throws IOException {
1029 	  out.writeInt(TABLE_DESCRIPTOR_VERSION);
1030     Bytes.writeByteArray(out, name.toBytes());
1031     out.writeBoolean(isRootRegion());
1032     out.writeBoolean(isMetaRegion());
1033     out.writeInt(values.size());
1034     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1035         values.entrySet()) {
1036       e.getKey().write(out);
1037       e.getValue().write(out);
1038     }
1039     out.writeInt(families.size());
1040     for(Iterator<HColumnDescriptor> it = families.values().iterator();
1041         it.hasNext(); ) {
1042       HColumnDescriptor family = it.next();
1043       family.write(out);
1044     }
1045     out.writeInt(configuration.size());
1046     for (Map.Entry<String, String> e : configuration.entrySet()) {
1047       new ImmutableBytesWritable(Bytes.toBytes(e.getKey())).write(out);
1048       new ImmutableBytesWritable(Bytes.toBytes(e.getValue())).write(out);
1049     }
1050   }
1051 
1052   // Comparable
1053 
1054   /**
1055    * Compares the descriptor with another descriptor which is passed as a parameter.
1056    * This compares the content of the two descriptors and not the reference.
1057    *
1058    * @return 0 if the contents of the descriptors are exactly matching,
1059    * 		 1 if there is a mismatch in the contents
1060    */
1061   @Override
1062   public int compareTo(final HTableDescriptor other) {
1063     int result = this.name.compareTo(other.name);
1064     if (result == 0) {
1065       result = families.size() - other.families.size();
1066     }
1067     if (result == 0 && families.size() != other.families.size()) {
1068       result = Integer.valueOf(families.size()).compareTo(
1069           Integer.valueOf(other.families.size()));
1070     }
1071     if (result == 0) {
1072       for (Iterator<HColumnDescriptor> it = families.values().iterator(),
1073           it2 = other.families.values().iterator(); it.hasNext(); ) {
1074         result = it.next().compareTo(it2.next());
1075         if (result != 0) {
1076           break;
1077         }
1078       }
1079     }
1080     if (result == 0) {
1081       // punt on comparison for ordering, just calculate difference
1082       result = this.values.hashCode() - other.values.hashCode();
1083       if (result < 0)
1084         result = -1;
1085       else if (result > 0)
1086         result = 1;
1087     }
1088     if (result == 0) {
1089       result = this.configuration.hashCode() - other.configuration.hashCode();
1090       if (result < 0)
1091         result = -1;
1092       else if (result > 0)
1093         result = 1;
1094     }
1095     return result;
1096   }
1097 
1098   /**
1099    * Returns an unmodifiable collection of all the {@link HColumnDescriptor}
1100    * of all the column families of the table.
1101    *
1102    * @return Immutable collection of {@link HColumnDescriptor} of all the
1103    * column families.
1104    */
1105   public Collection<HColumnDescriptor> getFamilies() {
1106     return Collections.unmodifiableCollection(this.families.values());
1107   }
1108 
1109   /**
1110    * Returns all the column family names of the current table. The map of
1111    * HTableDescriptor contains mapping of family name to HColumnDescriptors.
1112    * This returns all the keys of the family map which represents the column
1113    * family names of the table.
1114    *
1115    * @return Immutable sorted set of the keys of the families.
1116    */
1117   public Set<byte[]> getFamiliesKeys() {
1118     return Collections.unmodifiableSet(this.families.keySet());
1119   }
1120 
1121   /**
1122    * Returns an array all the {@link HColumnDescriptor} of the column families
1123    * of the table.
1124    *
1125    * @return Array of all the HColumnDescriptors of the current table
1126    *
1127    * @see #getFamilies()
1128    */
1129   public HColumnDescriptor[] getColumnFamilies() {
1130     Collection<HColumnDescriptor> hColumnDescriptors = getFamilies();
1131     return hColumnDescriptors.toArray(new HColumnDescriptor[hColumnDescriptors.size()]);
1132   }
1133 
1134 
1135   /**
1136    * Returns the HColumnDescriptor for a specific column family with name as
1137    * specified by the parameter column.
1138    *
1139    * @param column Column family name
1140    * @return Column descriptor for the passed family name or the family on
1141    * passed in column.
1142    */
1143   public HColumnDescriptor getFamily(final byte [] column) {
1144     return this.families.get(column);
1145   }
1146 
1147 
1148   /**
1149    * Removes the HColumnDescriptor with name specified by the parameter column
1150    * from the table descriptor
1151    *
1152    * @param column Name of the column family to be removed.
1153    * @return Column descriptor for the passed family name or the family on
1154    * passed in column.
1155    */
1156   public HColumnDescriptor removeFamily(final byte [] column) {
1157     return this.families.remove(column);
1158   }
1159 
1160 
1161   /**
1162    * Add a table coprocessor to this table. The coprocessor
1163    * type must be {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
1164    * or Endpoint.
1165    * It won't check if the class can be loaded or not.
1166    * Whether a coprocessor is loadable or not will be determined when
1167    * a region is opened.
1168    * @param className Full class name.
1169    * @throws IOException
1170    */
1171   public void addCoprocessor(String className) throws IOException {
1172     addCoprocessor(className, null, Coprocessor.PRIORITY_USER, null);
1173   }
1174 
1175 
1176   /**
1177    * Add a table coprocessor to this table. The coprocessor
1178    * type must be {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
1179    * or Endpoint.
1180    * It won't check if the class can be loaded or not.
1181    * Whether a coprocessor is loadable or not will be determined when
1182    * a region is opened.
1183    * @param jarFilePath Path of the jar file. If it's null, the class will be
1184    * loaded from default classloader.
1185    * @param className Full class name.
1186    * @param priority Priority
1187    * @param kvs Arbitrary key-value parameter pairs passed into the coprocessor.
1188    * @throws IOException
1189    */
1190   public void addCoprocessor(String className, Path jarFilePath,
1191                              int priority, final Map<String, String> kvs)
1192   throws IOException {
1193     if (hasCoprocessor(className)) {
1194       throw new IOException("Coprocessor " + className + " already exists.");
1195     }
1196     // validate parameter kvs
1197     StringBuilder kvString = new StringBuilder();
1198     if (kvs != null) {
1199       for (Map.Entry<String, String> e: kvs.entrySet()) {
1200         if (!e.getKey().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1201           throw new IOException("Illegal parameter key = " + e.getKey());
1202         }
1203         if (!e.getValue().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1204           throw new IOException("Illegal parameter (" + e.getKey() +
1205               ") value = " + e.getValue());
1206         }
1207         if (kvString.length() != 0) {
1208           kvString.append(',');
1209         }
1210         kvString.append(e.getKey());
1211         kvString.append('=');
1212         kvString.append(e.getValue());
1213       }
1214     }
1215 
1216     // generate a coprocessor key
1217     int maxCoprocessorNumber = 0;
1218     Matcher keyMatcher;
1219     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1220         this.values.entrySet()) {
1221       keyMatcher =
1222           HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1223               Bytes.toString(e.getKey().get()));
1224       if (!keyMatcher.matches()) {
1225         continue;
1226       }
1227       maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)),
1228           maxCoprocessorNumber);
1229     }
1230     maxCoprocessorNumber++;
1231 
1232     String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1233     String value = ((jarFilePath == null)? "" : jarFilePath.toString()) +
1234         "|" + className + "|" + Integer.toString(priority) + "|" +
1235         kvString.toString();
1236     setValue(key, value);
1237   }
1238 
1239 
1240   /**
1241    * Check if the table has an attached co-processor represented by the name className
1242    *
1243    * @param className - Class name of the co-processor
1244    * @return true of the table has a co-processor className
1245    */
1246   public boolean hasCoprocessor(String className) {
1247     Matcher keyMatcher;
1248     Matcher valueMatcher;
1249     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1250         this.values.entrySet()) {
1251       keyMatcher =
1252           HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1253               Bytes.toString(e.getKey().get()));
1254       if (!keyMatcher.matches()) {
1255         continue;
1256       }
1257       valueMatcher =
1258         HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(
1259             Bytes.toString(e.getValue().get()));
1260       if (!valueMatcher.matches()) {
1261         continue;
1262       }
1263       // get className and compare
1264       String clazz = valueMatcher.group(2).trim(); // classname is the 2nd field
1265       if (clazz.equals(className.trim())) {
1266         return true;
1267       }
1268     }
1269     return false;
1270   }
1271 
1272   /**
1273    * Return the list of attached co-processor represented by their name className
1274    *
1275    * @return The list of co-processors classNames
1276    */
1277   public List<String> getCoprocessors() {
1278     List<String> result = new ArrayList<String>();
1279     Matcher keyMatcher;
1280     Matcher valueMatcher;
1281     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values.entrySet()) {
1282       keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1283       if (!keyMatcher.matches()) {
1284         continue;
1285       }
1286       valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1287           .toString(e.getValue().get()));
1288       if (!valueMatcher.matches()) {
1289         continue;
1290       }
1291       result.add(valueMatcher.group(2).trim()); // classname is the 2nd field
1292     }
1293     return result;
1294   }
1295 
1296   /**
1297    * Remove a coprocessor from those set on the table
1298    * @param className Class name of the co-processor
1299    */
1300   public void removeCoprocessor(String className) {
1301     ImmutableBytesWritable match = null;
1302     Matcher keyMatcher;
1303     Matcher valueMatcher;
1304     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values
1305         .entrySet()) {
1306       keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1307           .getKey().get()));
1308       if (!keyMatcher.matches()) {
1309         continue;
1310       }
1311       valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1312           .toString(e.getValue().get()));
1313       if (!valueMatcher.matches()) {
1314         continue;
1315       }
1316       // get className and compare
1317       String clazz = valueMatcher.group(2).trim(); // classname is the 2nd field
1318       // remove the CP if it is present
1319       if (clazz.equals(className.trim())) {
1320         match = e.getKey();
1321         break;
1322       }
1323     }
1324     // if we found a match, remove it
1325     if (match != null)
1326       remove(match);
1327   }
1328 
1329   /**
1330    * Returns the {@link Path} object representing the table directory under
1331    * path rootdir
1332    *
1333    * Deprecated use FSUtils.getTableDir() instead.
1334    *
1335    * @param rootdir qualified path of HBase root directory
1336    * @param tableName name of table
1337    * @return {@link Path} for table
1338    */
1339   @Deprecated
1340   public static Path getTableDir(Path rootdir, final byte [] tableName) {
1341     //This is bad I had to mirror code from FSUTils.getTableDir since
1342     //there is no module dependency between hbase-client and hbase-server
1343     TableName name = TableName.valueOf(tableName);
1344     return new Path(rootdir, new Path(HConstants.BASE_NAMESPACE_DIR,
1345               new Path(name.getNamespaceAsString(), new Path(name.getQualifierAsString()))));
1346   }
1347 
1348   /**
1349    * Table descriptor for <code>hbase:meta</code> catalog table
1350    * @deprecated Use TableDescriptors#get(TableName.META_TABLE_NAME) or
1351    * HBaseAdmin#getTableDescriptor(TableName.META_TABLE_NAME) instead.
1352    */
1353   @Deprecated
1354   public static final HTableDescriptor META_TABLEDESC = new HTableDescriptor(
1355       TableName.META_TABLE_NAME,
1356       new HColumnDescriptor[] {
1357           new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1358               // Ten is arbitrary number.  Keep versions to help debugging.
1359               .setMaxVersions(HConstants.DEFAULT_HBASE_META_VERSIONS)
1360               .setInMemory(true)
1361               .setBlocksize(HConstants.DEFAULT_HBASE_META_BLOCK_SIZE)
1362               .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1363               // Disable blooms for meta.  Needs work.  Seems to mess w/ getClosestOrBefore.
1364               .setBloomFilterType(BloomType.NONE)
1365       });
1366 
1367   static {
1368     try {
1369       META_TABLEDESC.addCoprocessor(
1370           "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1371           null, Coprocessor.PRIORITY_SYSTEM, null);
1372     } catch (IOException ex) {
1373       //LOG.warn("exception in loading coprocessor for the hbase:meta table");
1374       throw new RuntimeException(ex);
1375     }
1376   }
1377 
1378   public final static String NAMESPACE_FAMILY_INFO = "info";
1379   public final static byte[] NAMESPACE_FAMILY_INFO_BYTES = Bytes.toBytes(NAMESPACE_FAMILY_INFO);
1380   public final static byte[] NAMESPACE_COL_DESC_BYTES = Bytes.toBytes("d");
1381 
1382   /** Table descriptor for namespace table */
1383   public static final HTableDescriptor NAMESPACE_TABLEDESC = new HTableDescriptor(
1384       TableName.NAMESPACE_TABLE_NAME,
1385       new HColumnDescriptor[] {
1386           new HColumnDescriptor(NAMESPACE_FAMILY_INFO)
1387               // Ten is arbitrary number.  Keep versions to help debugging.
1388               .setMaxVersions(10)
1389               .setInMemory(true)
1390               .setBlocksize(8 * 1024)
1391               .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1392       });
1393 
1394   @Deprecated
1395   public void setOwner(User owner) {
1396     setOwnerString(owner != null ? owner.getShortName() : null);
1397   }
1398 
1399   // used by admin.rb:alter(table_name,*args) to update owner.
1400   @Deprecated
1401   public void setOwnerString(String ownerString) {
1402     if (ownerString != null) {
1403       setValue(OWNER_KEY, ownerString);
1404     } else {
1405       remove(OWNER_KEY);
1406     }
1407   }
1408 
1409   @Deprecated
1410   public String getOwnerString() {
1411     if (getValue(OWNER_KEY) != null) {
1412       return Bytes.toString(getValue(OWNER_KEY));
1413     }
1414     // Note that every table should have an owner (i.e. should have OWNER_KEY set).
1415     // hbase:meta and -ROOT- should return system user as owner, not null (see
1416     // MasterFileSystem.java:bootstrap()).
1417     return null;
1418   }
1419 
1420   /**
1421    * @return This instance serialized with pb with pb magic prefix
1422    * @see #parseFrom(byte[])
1423    */
1424   public byte [] toByteArray() {
1425     return ProtobufUtil.prependPBMagic(convert().toByteArray());
1426   }
1427 
1428   /**
1429    * @param bytes A pb serialized {@link HTableDescriptor} instance with pb magic prefix
1430    * @return An instance of {@link HTableDescriptor} made from <code>bytes</code>
1431    * @throws DeserializationException
1432    * @throws IOException
1433    * @see #toByteArray()
1434    */
1435   public static HTableDescriptor parseFrom(final byte [] bytes)
1436   throws DeserializationException, IOException {
1437     if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
1438       return (HTableDescriptor)Writables.getWritable(bytes, new HTableDescriptor());
1439     }
1440     int pblen = ProtobufUtil.lengthOfPBMagic();
1441     TableSchema.Builder builder = TableSchema.newBuilder();
1442     TableSchema ts;
1443     try {
1444       ts = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
1445     } catch (InvalidProtocolBufferException e) {
1446       throw new DeserializationException(e);
1447     }
1448     return convert(ts);
1449   }
1450 
1451   /**
1452    * @return Convert the current {@link HTableDescriptor} into a pb TableSchema instance.
1453    */
1454   public TableSchema convert() {
1455     TableSchema.Builder builder = TableSchema.newBuilder();
1456     builder.setTableName(ProtobufUtil.toProtoTableName(getTableName()));
1457     for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e: this.values.entrySet()) {
1458       BytesBytesPair.Builder aBuilder = BytesBytesPair.newBuilder();
1459       aBuilder.setFirst(ByteStringer.wrap(e.getKey().get()));
1460       aBuilder.setSecond(ByteStringer.wrap(e.getValue().get()));
1461       builder.addAttributes(aBuilder.build());
1462     }
1463     for (HColumnDescriptor hcd: getColumnFamilies()) {
1464       builder.addColumnFamilies(hcd.convert());
1465     }
1466     for (Map.Entry<String, String> e : this.configuration.entrySet()) {
1467       NameStringPair.Builder aBuilder = NameStringPair.newBuilder();
1468       aBuilder.setName(e.getKey());
1469       aBuilder.setValue(e.getValue());
1470       builder.addConfiguration(aBuilder.build());
1471     }
1472     return builder.build();
1473   }
1474 
1475   /**
1476    * @param ts A pb TableSchema instance.
1477    * @return An {@link HTableDescriptor} made from the passed in pb <code>ts</code>.
1478    */
1479   public static HTableDescriptor convert(final TableSchema ts) {
1480     List<ColumnFamilySchema> list = ts.getColumnFamiliesList();
1481     HColumnDescriptor [] hcds = new HColumnDescriptor[list.size()];
1482     int index = 0;
1483     for (ColumnFamilySchema cfs: list) {
1484       hcds[index++] = HColumnDescriptor.convert(cfs);
1485     }
1486     HTableDescriptor htd = new HTableDescriptor(
1487         ProtobufUtil.toTableName(ts.getTableName()),
1488         hcds);
1489     for (BytesBytesPair a: ts.getAttributesList()) {
1490       htd.setValue(a.getFirst().toByteArray(), a.getSecond().toByteArray());
1491     }
1492     for (NameStringPair a: ts.getConfigurationList()) {
1493       htd.setConfiguration(a.getName(), a.getValue());
1494     }
1495     return htd;
1496   }
1497 
1498   /**
1499    * Getter for accessing the configuration value by key
1500    */
1501   public String getConfigurationValue(String key) {
1502     return configuration.get(key);
1503   }
1504 
1505   /**
1506    * Getter for fetching an unmodifiable {@link #configuration} map.
1507    */
1508   public Map<String, String> getConfiguration() {
1509     // shallow pointer copy
1510     return Collections.unmodifiableMap(configuration);
1511   }
1512 
1513   /**
1514    * Setter for storing a configuration setting in {@link #configuration} map.
1515    * @param key Config key. Same as XML config key e.g. hbase.something.or.other.
1516    * @param value String value. If null, removes the setting.
1517    */
1518   public void setConfiguration(String key, String value) {
1519     if (value == null) {
1520       removeConfiguration(key);
1521     } else {
1522       configuration.put(key, value);
1523     }
1524   }
1525 
1526   /**
1527    * Remove a config setting represented by the key from the {@link #configuration} map
1528    */
1529   public void removeConfiguration(final String key) {
1530     configuration.remove(key);
1531   }
1532 
1533   public static HTableDescriptor metaTableDescriptor(final Configuration conf)
1534       throws IOException {
1535     HTableDescriptor metaDescriptor = new HTableDescriptor(
1536       TableName.META_TABLE_NAME,
1537       new HColumnDescriptor[] {
1538         new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1539           .setMaxVersions(conf.getInt(HConstants.HBASE_META_VERSIONS,
1540             HConstants.DEFAULT_HBASE_META_VERSIONS))
1541           .setInMemory(true)
1542           .setBlocksize(conf.getInt(HConstants.HBASE_META_BLOCK_SIZE,
1543             HConstants.DEFAULT_HBASE_META_BLOCK_SIZE))
1544           .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1545           // Disable blooms for meta.  Needs work.  Seems to mess w/ getClosestOrBefore.
1546           .setBloomFilterType(BloomType.NONE)
1547          });
1548     metaDescriptor.addCoprocessor(
1549       "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1550       null, Coprocessor.PRIORITY_SYSTEM, null);
1551     return metaDescriptor;
1552   }
1553 
1554 }