001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.util.Map;
021
022import org.apache.hadoop.hbase.TableName;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hbase.thirdparty.org.apache.commons.collections4.MapUtils;
026
027/**
028 * The POJO equivalent of HBaseProtos.SnapshotDescription
029 */
030@InterfaceAudience.Public
031public class SnapshotDescription {
032  private final String name;
033  private final TableName table;
034  private final SnapshotType snapShotType;
035  private final String owner;
036  private final long creationTime;
037  private final long ttl;
038  private final int version;
039
040  public SnapshotDescription(String name) {
041    this(name, (TableName)null);
042  }
043
044  public SnapshotDescription(String name, TableName table) {
045    this(name, table, SnapshotType.DISABLED, null, -1, -1, null);
046  }
047
048  public SnapshotDescription(String name, TableName table, SnapshotType type) {
049    this(name, table, type, null, -1, -1, null);
050  }
051
052  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner) {
053    this(name, table, type, owner, -1, -1, null);
054  }
055
056  /**
057   * SnapshotDescription Parameterized Constructor
058   *
059   * @param name          Name of the snapshot
060   * @param table         TableName associated with the snapshot
061   * @param type          Type of the snapshot - enum SnapshotType
062   * @param owner         Snapshot Owner
063   * @param creationTime  Creation time for Snapshot
064   * @param version       Snapshot Version
065   * @param snapshotProps Additional properties for snapshot e.g. TTL
066   */
067  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
068                             long creationTime, int version, Map<String, Object> snapshotProps) {
069    this.name = name;
070    this.table = table;
071    this.snapShotType = type;
072    this.owner = owner;
073    this.creationTime = creationTime;
074    this.ttl = getTtlFromSnapshotProps(snapshotProps);
075    this.version = version;
076  }
077
078  private long getTtlFromSnapshotProps(Map<String, Object> snapshotProps) {
079    return MapUtils.getLongValue(snapshotProps, "TTL", -1);
080  }
081
082  /**
083   * SnapshotDescription Parameterized Constructor
084   *
085   * @param snapshotName  Name of the snapshot
086   * @param tableName     TableName associated with the snapshot
087   * @param type          Type of the snapshot - enum SnapshotType
088   * @param snapshotProps Additional properties for snapshot e.g. TTL
089   */
090  public SnapshotDescription(String snapshotName, TableName tableName, SnapshotType type,
091                             Map<String, Object> snapshotProps) {
092    this(snapshotName, tableName, type, null, -1, -1, snapshotProps);
093  }
094
095  public String getName() {
096    return this.name;
097  }
098
099  public String getTableNameAsString() {
100    return this.table.getNameAsString();
101  }
102
103  public TableName getTableName() {
104    return this.table;
105  }
106
107  public SnapshotType getType() {
108    return this.snapShotType;
109  }
110
111  public String getOwner() {
112    return this.owner;
113  }
114
115  public long getCreationTime() {
116    return this.creationTime;
117  }
118
119  // get snapshot ttl in sec
120  public long getTtl() {
121    return ttl;
122  }
123
124  public int getVersion() {
125    return this.version;
126  }
127
128  @Override
129  public String toString() {
130    return new StringBuilder("SnapshotDescription: ")
131            .append("name = ")
132            .append(name)
133            .append("/table = ")
134            .append(table)
135            .append(" /owner = ")
136            .append(owner)
137            .append(creationTime != -1 ? ("/creationtime = " + creationTime) : "")
138            .append(ttl != -1 ? ("/ttl = " + ttl) : "")
139            .append(version != -1 ? ("/version = " + version) : "")
140            .toString();
141  }
142}