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.snapshot;
20  
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  /**
27   * Class to help with dealing with a snapshot description on the client side.
28   * There is a corresponding class on the server side.
29   */
30  @InterfaceAudience.Private
31  public final class ClientSnapshotDescriptionUtils {
32    private ClientSnapshotDescriptionUtils() {
33    }
34  
35    /**
36     * Check to make sure that the description of the snapshot requested is valid
37     * @param snapshot description of the snapshot
38     * @throws IllegalArgumentException if the name of the snapshot or the name of the table to
39     *           snapshot are not valid names
40     */
41    public static void assertSnapshotRequestIsValid(SnapshotDescription snapshot)
42        throws IllegalArgumentException {
43      // make sure the snapshot name is valid
44      TableName.isLegalTableQualifierName(Bytes.toBytes(snapshot.getName()), true);
45      if (snapshot.hasTable()) {
46        // make sure the table name is valid, this will implicitly check validity
47        TableName tableName = TableName.valueOf(snapshot.getTable());
48  
49        if (tableName.isSystemTable()) {
50          throw new IllegalArgumentException("System table snapshots are not allowed");
51        }
52      }
53    }
54  
55    /**
56     * Returns a single line (no \n) representation of snapshot metadata. Use this instead of
57     * {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription#toString()}.
58     * We don't replace
59     * {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription}'s
60     * {@code toString}, because it is auto-generated by protoc.
61     *
62     * @param snapshot description of the snapshot
63     * @return single line string with a summary of the snapshot parameters
64     */
65    public static String toString(SnapshotDescription snapshot) {
66      if (snapshot == null) {
67        return null;
68      }
69  
70      return new StringBuilder("{ ss=")
71              .append(snapshot.getName())
72              .append(" table=")
73              .append(snapshot.hasTable() ? TableName.valueOf(snapshot.getTable()) : "")
74              .append(" type=")
75              .append(snapshot.getType())
76              .append(" ttl=")
77              .append(snapshot.getTtl())
78              .append(" }")
79              .toString();
80    }
81  }