View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.apache.hadoop.hbase.KeyValue.KVComparator;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  import java.nio.ByteBuffer;
27  import java.util.Arrays;
28  import java.util.Set;
29  import java.util.concurrent.CopyOnWriteArraySet;
30  
31  /**
32   * Immutable POJO class for representing a table name.
33   * Which is of the form:
34   * <table namespace>:<table qualifier>
35   *
36   * Two special namespaces:
37   *
38   * 1. hbase - system namespace, used to contain hbase internal tables
39   * 2. default - tables with no explicit specified namespace will
40   * automatically fall into this namespace.
41   *
42   * ie
43   *
44   * a) foo:bar, means namespace=foo and qualifier=bar
45   * b) bar, means namespace=default and qualifier=bar
46   * c) default:bar, means namespace=default and qualifier=bar
47   *
48   *  <p>
49   * Internally, in this class, we cache the instances to limit the number of objects and
50   *  make the "equals" faster. We try to minimize the number of objects created of
51   *  the number of array copy to check if we already have an instance of this TableName. The code
52   *  is not optimize for a new instance creation but is optimized to check for existence.
53   * </p>
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Evolving
57  public final class TableName implements Comparable<TableName> {
58  
59    /** See {@link #createTableNameIfNecessary(ByteBuffer, ByteBuffer)} */
60    private static final Set<TableName> tableCache = new CopyOnWriteArraySet<TableName>();
61  
62    /** Namespace delimiter */
63    //this should always be only 1 byte long
64    public final static char NAMESPACE_DELIM = ':';
65  
66    // A non-capture group so that this can be embedded.
67    // regex is a bit more complicated to support nuance of tables
68    // in default namespace
69    //Allows only letters, digits and '_'
70    public static final String VALID_NAMESPACE_REGEX =
71        "(?:[a-zA-Z_0-9]+)";
72    //Allows only letters, digits, '_', '-' and '.'
73    public static final String VALID_TABLE_QUALIFIER_REGEX =
74        "(?:[a-zA-Z_0-9][a-zA-Z_0-9-.]*)";
75    //Concatenation of NAMESPACE_REGEX and TABLE_QUALIFIER_REGEX,
76    //with NAMESPACE_DELIM as delimiter
77    public static final String VALID_USER_TABLE_REGEX =
78        "(?:(?:(?:"+VALID_NAMESPACE_REGEX+"\\"+NAMESPACE_DELIM+")?)" +
79           "(?:"+VALID_TABLE_QUALIFIER_REGEX+"))";
80  
81    /** The hbase:meta table's name. */
82    public static final TableName META_TABLE_NAME =
83        valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "meta");
84  
85    /** The Namespace table's name. */
86    public static final TableName NAMESPACE_TABLE_NAME =
87        valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "namespace");
88  
89    public static final String OLD_META_STR = ".META.";
90    public static final String OLD_ROOT_STR = "-ROOT-";
91  
92  
93  
94    /**
95     * TableName for old -ROOT- table. It is used to read/process old WALs which have
96     * ROOT edits.
97     */
98    public static final TableName OLD_ROOT_TABLE_NAME = getADummyTableName(OLD_ROOT_STR);
99    /**
100    * TableName for old .META. table. Used in testing.
101    */
102   public static final TableName OLD_META_TABLE_NAME = getADummyTableName(OLD_META_STR);
103 
104   private final byte[] name;
105   private final String nameAsString;
106   private final byte[] namespace;
107   private final String namespaceAsString;
108   private final byte[] qualifier;
109   private final String qualifierAsString;
110   private final boolean systemTable;
111   private final int hashCode;
112 
113   /**
114    * Check passed byte array, "tableName", is legal user-space table name.
115    * @return Returns passed <code>tableName</code> param
116    * @throws IllegalArgumentException if passed a tableName is null or
117    * is made of other than 'word' characters or underscores: i.e.
118    * <code>[a-zA-Z_0-9.-:]</code>. The ':' is used to delimit the namespace
119    * from the table name and can be used for nothing else.
120    *
121    * Namespace names can only contain 'word' characters
122    * <code>[a-zA-Z_0-9]</code> or '_'
123    *
124    * Qualifier names can only contain 'word' characters
125    * <code>[a-zA-Z_0-9]</code> or '_', '.' or '-'.
126    * The name may not start with '.' or '-'.
127    *
128    * Valid fully qualified table names:
129    * foo:bar, namespace=>foo, table=>bar
130    * org:foo.bar, namespace=org, table=>foo.bar
131    */
132   public static byte [] isLegalFullyQualifiedTableName(final byte[] tableName) {
133     if (tableName == null || tableName.length <= 0) {
134       throw new IllegalArgumentException("Name is null or empty");
135     }
136 
137     int namespaceDelimIndex = com.google.common.primitives.Bytes.lastIndexOf(tableName,
138         (byte) NAMESPACE_DELIM);
139     if (namespaceDelimIndex == 0 || namespaceDelimIndex == -1){
140       isLegalTableQualifierName(tableName);
141     } else {
142       isLegalNamespaceName(tableName, 0, namespaceDelimIndex);
143       isLegalTableQualifierName(tableName, namespaceDelimIndex + 1, tableName.length);
144     }
145     return tableName;
146   }
147 
148   public static byte [] isLegalTableQualifierName(final byte[] qualifierName){
149     isLegalTableQualifierName(qualifierName, 0, qualifierName.length);
150     return qualifierName;
151   }
152 
153   /**
154    * Qualifier names can only contain 'word' characters
155    * <code>[a-zA-Z_0-9]</code> or '_', '.' or '-'.
156    * The name may not start with '.' or '-'.
157    *
158    * @param qualifierName byte array containing the qualifier name
159    * @param start start index
160    * @param end end index (exclusive)
161    */
162   public static void isLegalTableQualifierName(final byte[] qualifierName,
163                                                 int start,
164                                                 int end){
165     if(end - start < 1) {
166       throw new IllegalArgumentException("Table qualifier must not be empty");
167     }
168 
169     if (qualifierName[start] == '.' || qualifierName[start] == '-') {
170       throw new IllegalArgumentException("Illegal first character <" + qualifierName[0] +
171           "> at 0. Namespaces can only start with alphanumeric " +
172           "characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(qualifierName));
173     }
174     for (int i = start; i < end; i++) {
175       if (Character.isLetterOrDigit(qualifierName[i]) ||
176           qualifierName[i] == '_' ||
177           qualifierName[i] == '-' ||
178           qualifierName[i] == '.') {
179         continue;
180       }
181       throw new IllegalArgumentException("Illegal character code:" + qualifierName[i] +
182         ", <" + (char) qualifierName[i] + "> at " + i +
183           ". User-space table qualifiers can only contain " +
184         "'alphanumeric characters': i.e. [a-zA-Z_0-9-.]: " +
185           Bytes.toString(qualifierName, start, end));
186     }
187   }
188 
189   public static void isLegalNamespaceName(byte[] namespaceName) {
190     isLegalNamespaceName(namespaceName, 0, namespaceName.length);
191   }
192 
193   /**
194    * Valid namespace characters are [a-zA-Z_0-9]
195    */
196   public static void isLegalNamespaceName(byte[] namespaceName, int offset, int length) {
197     for (int i = offset; i < length; i++) {
198       if (Character.isLetterOrDigit(namespaceName[i])|| namespaceName[i] == '_') {
199         continue;
200       }
201       throw new IllegalArgumentException("Illegal character <" + namespaceName[i] +
202         "> at " + i + ". Namespaces can only contain " +
203         "'alphanumeric characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(namespaceName,
204           offset, length));
205     }
206   }
207 
208   public byte[] getName() {
209     return name;
210   }
211 
212   public String getNameAsString() {
213     return nameAsString;
214   }
215 
216   public byte[] getNamespace() {
217     return namespace;
218   }
219 
220   public String getNamespaceAsString() {
221     return namespaceAsString;
222   }
223 
224   public byte[] getQualifier() {
225     return qualifier;
226   }
227 
228   public String getQualifierAsString() {
229     return qualifierAsString;
230   }
231 
232   public byte[] toBytes() {
233     return name;
234   }
235 
236   public boolean isSystemTable() {
237     return systemTable;
238   }
239 
240   @Override
241   public String toString() {
242     return nameAsString;
243   }
244 
245   /**
246    *
247    * @throws IllegalArgumentException See {@link #valueOf(byte[])}
248    */
249   private TableName(ByteBuffer namespace, ByteBuffer qualifier) throws IllegalArgumentException {
250     this.qualifier = new byte[qualifier.remaining()];
251     qualifier.duplicate().get(this.qualifier);
252     this.qualifierAsString = Bytes.toString(this.qualifier);
253 
254     if (qualifierAsString.equals(OLD_ROOT_STR)) {
255       throw new IllegalArgumentException(OLD_ROOT_STR + " has been deprecated.");
256     }
257     if (qualifierAsString.equals(OLD_META_STR)) {
258       throw new IllegalArgumentException(OLD_META_STR + " no longer exists. The table has been " +
259           "renamed to " + META_TABLE_NAME);
260     }
261 
262     if (Bytes.equals(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME, namespace)) {
263       // Using the same objects: this will make the comparison faster later
264       this.namespace = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME;
265       this.namespaceAsString = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR;
266       this.systemTable = false;
267 
268       // The name does not include the namespace when it's the default one.
269       this.nameAsString = qualifierAsString;
270       this.name = this.qualifier;
271     } else {
272       if (Bytes.equals(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME, namespace)) {
273         this.namespace = NamespaceDescriptor.SYSTEM_NAMESPACE_NAME;
274         this.namespaceAsString = NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR;
275         this.systemTable = true;
276       } else {
277         this.namespace = new byte[namespace.remaining()];
278         namespace.duplicate().get(this.namespace);
279         this.namespaceAsString = Bytes.toString(this.namespace);
280         this.systemTable = false;
281       }
282       this.nameAsString = namespaceAsString + NAMESPACE_DELIM + qualifierAsString;
283       this.name = Bytes.toBytes(nameAsString);
284     }
285 
286     this.hashCode = nameAsString.hashCode();
287 
288     isLegalNamespaceName(this.namespace);
289     isLegalTableQualifierName(this.qualifier);
290   }
291 
292   /**
293    * This is only for the old and meta tables.
294    */
295   private TableName(String qualifier) {
296     this.qualifier = Bytes.toBytes(qualifier);
297     this.qualifierAsString = qualifier;
298 
299     this.namespace = NamespaceDescriptor.SYSTEM_NAMESPACE_NAME;
300     this.namespaceAsString = NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR;
301     this.systemTable = true;
302 
303     // WARNING: nameAsString is different than name for old meta & root!
304     // This is by design.
305     this.nameAsString = namespaceAsString + NAMESPACE_DELIM + qualifierAsString;
306     this.name = this.qualifier;
307 
308     this.hashCode = nameAsString.hashCode();
309   }
310 
311 
312   /**
313    * Check that the object does not exist already. There are two reasons for creating the objects
314    * only once:
315    * 1) With 100K regions, the table names take ~20MB.
316    * 2) Equals becomes much faster as it's resolved with a reference and an int comparison.
317    */
318   private static TableName createTableNameIfNecessary(ByteBuffer bns, ByteBuffer qns) {
319     for (TableName tn : tableCache) {
320       if (Bytes.equals(tn.getQualifier(), qns) && Bytes.equals(tn.getNamespace(), bns)) {
321         return tn;
322       }
323     }
324 
325     TableName newTable = new TableName(bns, qns);
326     if (tableCache.add(newTable)) {  // Adds the specified element if it is not already present
327       return newTable;
328     }
329 
330     // Someone else added it. Let's find it.
331     for (TableName tn : tableCache) {
332       if (Bytes.equals(tn.getQualifier(), qns) && Bytes.equals(tn.getNamespace(), bns)) {
333         return tn;
334       }
335     }
336     // this should never happen.
337     throw new IllegalStateException(newTable + " was supposed to be in the cache");
338   }
339 
340 
341   /**
342    * It is used to create table names for old META, and ROOT table.
343    * These tables are not really legal tables. They are not added into the cache.
344    * @return a dummy TableName instance (with no validation) for the passed qualifier
345    */
346   private static TableName getADummyTableName(String qualifier) {
347     return new TableName(qualifier);
348   }
349 
350 
351   public static TableName valueOf(String namespaceAsString, String qualifierAsString) {
352     if (namespaceAsString == null || namespaceAsString.length() < 1) {
353       namespaceAsString = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR;
354     }
355 
356     for (TableName tn : tableCache) {
357       if (qualifierAsString.equals(tn.getQualifierAsString()) &&
358           namespaceAsString.equals(tn.getNameAsString())) {
359         return tn;
360       }
361     }
362 
363     return createTableNameIfNecessary(
364         ByteBuffer.wrap(Bytes.toBytes(namespaceAsString)),
365         ByteBuffer.wrap(Bytes.toBytes(qualifierAsString)));
366   }
367 
368 
369   /**
370    * @throws IllegalArgumentException if fullName equals old root or old meta. Some code
371    *  depends on this. The test is buried in the table creation to save on array comparison
372    *  when we're creating a standard table object that will be in the cache.
373    */
374   public static TableName valueOf(byte[] fullName) throws IllegalArgumentException{
375     for (TableName tn : tableCache) {
376       if (Arrays.equals(tn.getName(), fullName)) {
377         return tn;
378       }
379     }
380 
381     int namespaceDelimIndex = com.google.common.primitives.Bytes.lastIndexOf(fullName,
382         (byte) NAMESPACE_DELIM);
383 
384     if (namespaceDelimIndex < 0) {
385       return createTableNameIfNecessary(
386           ByteBuffer.wrap(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME),
387           ByteBuffer.wrap(fullName));
388     } else {
389       return createTableNameIfNecessary(
390           ByteBuffer.wrap(fullName, 0, namespaceDelimIndex),
391           ByteBuffer.wrap(fullName, namespaceDelimIndex + 1,
392               fullName.length - (namespaceDelimIndex + 1)));
393     }
394   }
395 
396 
397   /**
398    * @throws IllegalArgumentException if fullName equals old root or old meta. Some code
399    *  depends on this.
400    */
401   public static TableName valueOf(String name) {
402     for (TableName tn : tableCache) {
403       if (name.equals(tn.getNameAsString())) {
404         return tn;
405       }
406     }
407 
408     int namespaceDelimIndex = name.indexOf(NAMESPACE_DELIM);
409     byte[] nameB = Bytes.toBytes(name);
410 
411     if (namespaceDelimIndex < 0) {
412       return createTableNameIfNecessary(
413           ByteBuffer.wrap(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME),
414           ByteBuffer.wrap(nameB));
415     } else {
416       return createTableNameIfNecessary(
417           ByteBuffer.wrap(nameB, 0, namespaceDelimIndex),
418           ByteBuffer.wrap(nameB, namespaceDelimIndex + 1,
419               nameB.length - (namespaceDelimIndex + 1)));
420     }
421   }
422 
423 
424   public static TableName valueOf(byte[] namespace, byte[] qualifier) {
425     if (namespace == null || namespace.length < 1) {
426       namespace = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME;
427     }
428 
429     for (TableName tn : tableCache) {
430       if (Arrays.equals(tn.getQualifier(), namespace) &&
431           Arrays.equals(tn.getNamespace(), namespace)) {
432         return tn;
433       }
434     }
435 
436     return createTableNameIfNecessary(
437         ByteBuffer.wrap(namespace), ByteBuffer.wrap(qualifier));
438   }
439 
440   public static TableName valueOf(ByteBuffer namespace, ByteBuffer qualifier) {
441     if (namespace == null || namespace.remaining() < 1) {
442       return createTableNameIfNecessary(
443           ByteBuffer.wrap(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME), qualifier);
444     }
445 
446     return createTableNameIfNecessary(namespace, qualifier);
447   }
448 
449   @Override
450   public boolean equals(Object o) {
451     if (this == o) return true;
452     if (o == null || getClass() != o.getClass()) return false;
453 
454     TableName tableName = (TableName) o;
455 
456     return o.hashCode() == hashCode && nameAsString.equals(tableName.nameAsString);
457   }
458 
459   @Override
460   public int hashCode() {
461     return hashCode;
462   }
463 
464   /**
465    * For performance reasons, the ordering is not lexicographic.
466    */
467   @Override
468   public int compareTo(TableName tableName) {
469     if (this == tableName) return 0;
470     if (this.hashCode < tableName.hashCode()) {
471       return -1;
472     }
473     if (this.hashCode > tableName.hashCode()) {
474       return 1;
475     }
476     return this.nameAsString.compareTo(tableName.getNameAsString());
477   }
478 
479   /**
480    * Get the appropriate row comparator for this table.
481    *
482    * @return The comparator.
483    */
484   public KVComparator getRowComparator() {
485      if(TableName.META_TABLE_NAME.equals(this)) {
486       return KeyValue.META_COMPARATOR;
487     }
488     return KeyValue.COMPARATOR;
489   }
490 }