001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.snapshot; 020 021import org.apache.hadoop.hbase.TableName; 022import org.apache.hadoop.hbase.util.Bytes; 023import org.apache.yetus.audience.InterfaceAudience; 024 025import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos; 026 027/** 028 * Class to help with dealing with a snapshot description on the client side. 029 * There is a corresponding class on the server side. 030 */ 031@InterfaceAudience.Private 032public final class ClientSnapshotDescriptionUtils { 033 private ClientSnapshotDescriptionUtils() { 034 } 035 036 /** 037 * Check to make sure that the description of the snapshot requested is valid 038 * @param snapshot description of the snapshot 039 * @throws IllegalArgumentException if the name of the snapshot or the name of the table to 040 * snapshot are not valid names 041 */ 042 public static void assertSnapshotRequestIsValid(SnapshotProtos.SnapshotDescription snapshot) 043 throws IllegalArgumentException { 044 // make sure the snapshot name is valid 045 TableName.isLegalTableQualifierName(Bytes.toBytes(snapshot.getName()), true); 046 if (snapshot.hasTable()) { 047 // make sure the table name is valid, this will implicitly check validity 048 TableName tableName = TableName.valueOf(snapshot.getTable()); 049 050 if (tableName.isSystemTable()) { 051 throw new IllegalArgumentException("System table snapshots are not allowed"); 052 } 053 } 054 } 055 056 /** 057 * Returns a single line (no \n) representation of snapshot metadata. Use this instead of 058 * {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription#toString()}. 059 * We don't replace 060 * {@link org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription}'s 061 * {@code toString}, because it is auto-generated by protoc. 062 * 063 * @param snapshot description of the snapshot 064 * @return single line string with a summary of the snapshot parameters 065 */ 066 public static String toString(SnapshotProtos.SnapshotDescription snapshot) { 067 if (snapshot == null) { 068 return null; 069 } 070 071 return new StringBuilder("{ ss=") 072 .append(snapshot.getName()) 073 .append(" table=") 074 .append(snapshot.hasTable() ? TableName.valueOf(snapshot.getTable()) : "") 075 .append(" type=") 076 .append(snapshot.getType()) 077 .append(" ttl=") 078 .append(snapshot.getTtl()) 079 .append(" }") 080 .toString(); 081 } 082}