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.commons.lang3.builder.ToStringBuilder;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.org.apache.commons.collections4.MapUtils;
027
028/**
029 * The POJO equivalent of HBaseProtos.SnapshotDescription
030 */
031@InterfaceAudience.Public
032public class SnapshotDescription {
033  private final String name;
034  private final TableName table;
035  private final SnapshotType snapShotType;
036  private final String owner;
037  private final long creationTime;
038  private final long ttl;
039  private final int version;
040
041  public SnapshotDescription(String name) {
042    this(name, (TableName) null);
043  }
044
045  /**
046   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
047   *   instance instead.
048   * @see #SnapshotDescription(String, TableName)
049   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
050   */
051  @Deprecated
052  public SnapshotDescription(String name, String table) {
053    this(name, TableName.valueOf(table));
054  }
055
056  public SnapshotDescription(String name, TableName table) {
057    this(name, table, SnapshotType.DISABLED, null, -1, -1, null);
058  }
059
060  /**
061   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
062   *   instance instead.
063   * @see #SnapshotDescription(String, TableName, SnapshotType)
064   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
065   */
066  @Deprecated
067  public SnapshotDescription(String name, String table, SnapshotType type) {
068    this(name, TableName.valueOf(table), type);
069  }
070
071  public SnapshotDescription(String name, TableName table, SnapshotType type) {
072    this(name, table, type, null, -1, -1, null);
073  }
074
075  /**
076   * @see #SnapshotDescription(String, TableName, SnapshotType, String)
077   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
078   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
079   *   instance instead.
080   */
081  @Deprecated
082  public SnapshotDescription(String name, String table, SnapshotType type, String owner) {
083    this(name, TableName.valueOf(table), type, owner);
084  }
085
086  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner) {
087    this(name, table, type, owner, -1, -1, null);
088  }
089
090  /**
091   * @see #SnapshotDescription(String, TableName, SnapshotType, String, long, int, Map)
092   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
093   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
094   *   instance instead.
095   */
096  @Deprecated
097  public SnapshotDescription(String name, String table, SnapshotType type, String owner,
098      long creationTime, int version) {
099    this(name, TableName.valueOf(table), type, owner, creationTime, version, null);
100  }
101
102  /**
103   * SnapshotDescription Parameterized Constructor
104   *
105   * @param name Name of the snapshot
106   * @param table TableName associated with the snapshot
107   * @param type Type of the snapshot - enum SnapshotType
108   * @param owner Snapshot Owner
109   * @param creationTime Creation time for Snapshot
110   * @param version Snapshot Version
111   * @deprecated since 2.3.0 and will be removed in 4.0.0. Use
112   *   {@link #SnapshotDescription(String, TableName, SnapshotType, String, long, int, Map)}
113   */
114  @Deprecated
115  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
116      long creationTime, int version) {
117    this(name, table, type, owner, creationTime, version, null);
118  }
119
120  /**
121   * SnapshotDescription Parameterized Constructor
122   *
123   * @param name          Name of the snapshot
124   * @param table         TableName associated with the snapshot
125   * @param type          Type of the snapshot - enum SnapshotType
126   * @param owner         Snapshot Owner
127   * @param creationTime  Creation time for Snapshot
128   * @param version       Snapshot Version
129   * @param snapshotProps Additional properties for snapshot e.g. TTL
130   */
131  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
132      long creationTime, int version, Map<String, Object> snapshotProps) {
133    this.name = name;
134    this.table = table;
135    this.snapShotType = type;
136    this.owner = owner;
137    this.creationTime = creationTime;
138    this.ttl = getTtlFromSnapshotProps(snapshotProps);
139    this.version = version;
140  }
141
142  private long getTtlFromSnapshotProps(Map<String, Object> snapshotProps) {
143    return MapUtils.getLongValue(snapshotProps, "TTL", -1);
144  }
145
146  /**
147   * SnapshotDescription Parameterized Constructor
148   *
149   * @param snapshotName  Name of the snapshot
150   * @param tableName     TableName associated with the snapshot
151   * @param type          Type of the snapshot - enum SnapshotType
152   * @param snapshotProps Additional properties for snapshot e.g. TTL
153   */
154  public SnapshotDescription(String snapshotName, TableName tableName, SnapshotType type,
155                             Map<String, Object> snapshotProps) {
156    this(snapshotName, tableName, type, null, -1, -1, snapshotProps);
157  }
158
159  public String getName() {
160    return this.name;
161  }
162
163  /**
164   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use {@link #getTableName()} or
165   *   {@link #getTableNameAsString()} instead.
166   * @see #getTableName()
167   * @see #getTableNameAsString()
168   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
169   */
170  @Deprecated
171  public String getTable() {
172    return getTableNameAsString();
173  }
174
175  public String getTableNameAsString() {
176    return this.table.getNameAsString();
177  }
178
179  public TableName getTableName() {
180    return this.table;
181  }
182
183  public SnapshotType getType() {
184    return this.snapShotType;
185  }
186
187  public String getOwner() {
188    return this.owner;
189  }
190
191  public long getCreationTime() {
192    return this.creationTime;
193  }
194
195  // get snapshot ttl in sec
196  public long getTtl() {
197    return ttl;
198  }
199
200  public int getVersion() {
201    return this.version;
202  }
203
204  @Override
205  public String toString() {
206    return new ToStringBuilder(this)
207      .append("name", name)
208      .append("table", table)
209      .append("snapShotType", snapShotType)
210      .append("owner", owner)
211      .append("creationTime", creationTime)
212      .append("ttl", ttl)
213      .append("version", version)
214      .toString();
215  }
216}