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, null);
044  }
045
046  public SnapshotDescription(String name, TableName table) {
047    this(name, table, SnapshotType.DISABLED, null, -1, -1, null);
048  }
049
050  public SnapshotDescription(String name, TableName table, SnapshotType type) {
051    this(name, table, type, null, -1, -1, null);
052  }
053
054  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner) {
055    this(name, table, type, owner, -1, -1, null);
056  }
057
058  /**
059   * SnapshotDescription Parameterized Constructor
060   * @param name         Name of the snapshot
061   * @param table        TableName associated with the snapshot
062   * @param type         Type of the snapshot - enum SnapshotType
063   * @param owner        Snapshot Owner
064   * @param creationTime Creation time for Snapshot
065   * @param version      Snapshot Version
066   * @deprecated since 2.3.0 and will be removed in 4.0.0. Use
067   *             {@link #SnapshotDescription(String, TableName, SnapshotType, String, long, int, Map)}
068   */
069  @Deprecated
070  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
071    long creationTime, int version) {
072    this(name, table, type, owner, creationTime, version, null);
073  }
074
075  /**
076   * SnapshotDescription Parameterized Constructor
077   * @param name          Name of the snapshot
078   * @param table         TableName associated with the snapshot
079   * @param type          Type of the snapshot - enum SnapshotType
080   * @param owner         Snapshot Owner
081   * @param creationTime  Creation time for Snapshot
082   * @param version       Snapshot Version
083   * @param snapshotProps Additional properties for snapshot e.g. TTL
084   */
085  public SnapshotDescription(String name, TableName table, SnapshotType type, String owner,
086    long creationTime, int version, Map<String, Object> snapshotProps) {
087    this.name = name;
088    this.table = table;
089    this.snapShotType = type;
090    this.owner = owner;
091    this.creationTime = creationTime;
092    this.ttl = getLongFromSnapshotProps(snapshotProps, "TTL");
093    this.version = version;
094    this.maxFileSize = getLongFromSnapshotProps(snapshotProps, TableDescriptorBuilder.MAX_FILESIZE);
095  }
096
097  private long getLongFromSnapshotProps(Map<String, Object> snapshotProps, String property) {
098    return MapUtils.getLongValue(snapshotProps, property, -1);
099  }
100
101  /**
102   * SnapshotDescription Parameterized Constructor
103   * @param snapshotName  Name of the snapshot
104   * @param tableName     TableName associated with the snapshot
105   * @param type          Type of the snapshot - enum SnapshotType
106   * @param snapshotProps Additional properties for snapshot e.g. TTL
107   */
108  public SnapshotDescription(String snapshotName, TableName tableName, SnapshotType type,
109    Map<String, Object> snapshotProps) {
110    this(snapshotName, tableName, type, null, -1, -1, snapshotProps);
111  }
112
113  public String getName() {
114    return this.name;
115  }
116
117  public String getTableNameAsString() {
118    return this.table.getNameAsString();
119  }
120
121  public TableName getTableName() {
122    return this.table;
123  }
124
125  public SnapshotType getType() {
126    return this.snapShotType;
127  }
128
129  public String getOwner() {
130    return this.owner;
131  }
132
133  public long getCreationTime() {
134    return this.creationTime;
135  }
136
137  // get snapshot ttl in sec
138  public long getTtl() {
139    return ttl;
140  }
141
142  public int getVersion() {
143    return this.version;
144  }
145
146  public long getMaxFileSize() {
147    return maxFileSize;
148  }
149
150  @Override
151  public String toString() {
152    return new ToStringBuilder(this).append("name", name).append("table", table)
153      .append("snapShotType", snapShotType).append("owner", owner)
154      .append("creationTime", creationTime).append("ttl", ttl).append("version", version)
155      .append("maxFileSize", maxFileSize).toString();
156  }
157}