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;
021import org.apache.commons.lang3.builder.ToStringBuilder;
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  private final long maxFileSize;
041
042  public SnapshotDescription(String name) {
043    this(name, (TableName) null);
044  }
045
046  /**
047   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
048   *             instance instead.
049   * @see #SnapshotDescription(String, TableName)
050   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
051   */
052  @Deprecated
053  public SnapshotDescription(String name, String table) {
054    this(name, TableName.valueOf(table));
055  }
056
057  public SnapshotDescription(String name, TableName table) {
058    this(name, table, SnapshotType.DISABLED, null, -1, -1, null);
059  }
060
061  /**
062   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
063   *             instance instead.
064   * @see #SnapshotDescription(String, TableName, SnapshotType)
065   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
066   */
067  @Deprecated
068  public SnapshotDescription(String name, String table, SnapshotType type) {
069    this(name, TableName.valueOf(table), type);
070  }
071
072  public SnapshotDescription(String name, TableName table, SnapshotType type) {
073    this(name, table, type, null, -1, -1, null);
074  }
075
076  /**
077   * @see #SnapshotDescription(String, TableName, SnapshotType, String)
078   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
079   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
080   *             instance instead.
081   */
082  @Deprecated
083  public SnapshotDescription(String name, String table, SnapshotType type, String owner) {
084    this(name, TableName.valueOf(table), type, owner);
085  }
086
087  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner) {
088    this(name, table, type, owner, -1, -1, null);
089  }
090
091  /**
092   * @see #SnapshotDescription(String, TableName, SnapshotType, String, long, int, Map)
093   * @see <a href="https://issues.apache.org/jira/browse/HBASE-16892">HBASE-16892</a>
094   * @deprecated since 2.0.0 and will be removed in 3.0.0. Use the version with the TableName
095   *             instance instead.
096   */
097  @Deprecated
098  public SnapshotDescription(String name, String table, SnapshotType type, String owner,
099    long creationTime, int version) {
100    this(name, TableName.valueOf(table), type, owner, creationTime, version, null);
101  }
102
103  /**
104   * SnapshotDescription Parameterized Constructor
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   * @param name          Name of the snapshot
123   * @param table         TableName associated with the snapshot
124   * @param type          Type of the snapshot - enum SnapshotType
125   * @param owner         Snapshot Owner
126   * @param creationTime  Creation time for Snapshot
127   * @param version       Snapshot Version
128   * @param snapshotProps Additional properties for snapshot e.g. TTL
129   */
130  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
131    long creationTime, int version, Map<String, Object> snapshotProps) {
132    this.name = name;
133    this.table = table;
134    this.snapShotType = type;
135    this.owner = owner;
136    this.creationTime = creationTime;
137    this.ttl = getLongFromSnapshotProps(snapshotProps, "TTL");
138    this.version = version;
139    this.maxFileSize = getLongFromSnapshotProps(snapshotProps, TableDescriptorBuilder.MAX_FILESIZE);
140  }
141
142  private long getLongFromSnapshotProps(Map<String, Object> snapshotProps, String property) {
143    return MapUtils.getLongValue(snapshotProps, property, -1);
144  }
145
146  /**
147   * SnapshotDescription Parameterized Constructor
148   * @param snapshotName  Name of the snapshot
149   * @param tableName     TableName associated with the snapshot
150   * @param type          Type of the snapshot - enum SnapshotType
151   * @param snapshotProps Additional properties for snapshot e.g. TTL
152   */
153  public SnapshotDescription(String snapshotName, TableName tableName, SnapshotType type,
154    Map<String, Object> snapshotProps) {
155    this(snapshotName, tableName, type, null, -1, -1, snapshotProps);
156  }
157
158  public String getName() {
159    return this.name;
160  }
161
162  /**
163   * Get the table name as a string.
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  public long getMaxFileSize() {
205    return maxFileSize;
206  }
207
208  @Override
209  public String toString() {
210    return new ToStringBuilder(this).append("name", name).append("table", table)
211      .append("snapShotType", snapShotType).append("owner", owner)
212      .append("creationTime", creationTime).append("ttl", ttl).append("version", version)
213      .append("maxFileSize", maxFileSize).toString();
214  }
215}