View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.snapshot;
19  
20  import java.io.IOException;
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  
24  import org.apache.hadoop.io.Writable;
25  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  /**
29   * Writable version of the SnapshotDescription used by the rpc
30   */
31  public class HSnapshotDescription implements Writable {
32    private SnapshotDescription proto;
33  
34    public HSnapshotDescription() {
35    }
36  
37    public HSnapshotDescription(final SnapshotDescription proto) {
38      assert proto != null : "proto must be non-null";
39      this.proto = proto;
40    }
41  
42    public String getName() {
43      return this.proto.getName();
44    }
45  
46    public SnapshotDescription getProto() {
47      return this.proto;
48    }
49  
50    public SnapshotDescription.Type getType() {
51      return this.proto.getType();
52    }
53  
54    public String getTable() {
55      return this.proto.getTable();
56    }
57  
58    public boolean hasTable() {
59      return this.proto.hasTable();
60    }
61  
62    public long getCreationTime() {
63      return this.proto.getCreationTime();
64    }
65  
66    public int getVersion() {
67      return this.proto.getVersion();
68    }
69  
70    public String toString() {
71      if (this.proto != null) {
72        return this.proto.toString();
73      }
74      return "(no snapshot)";
75    }
76  
77    public boolean equals(Object obj) {
78      if (this == obj) {
79        return true;
80      }
81      if (obj == null) {
82        return false;
83      }
84      if (!(obj instanceof HSnapshotDescription)) {
85        return false;
86      }
87      SnapshotDescription oproto = ((HSnapshotDescription)obj).getProto();
88      if (this.proto == oproto) {
89        return true;
90      }
91      if (this.proto == null && oproto != null) {
92        return false;
93      }
94      return this.proto.equals(oproto);
95    }
96  
97    // Writable
98    /**
99     * <em> INTERNAL </em> This method is a part of {@link Writable} interface
100    * and is used for de-serialization of the HTableDescriptor over RPC
101    */
102   @Override
103   public void readFields(DataInput in) throws IOException {
104     byte[] data = Bytes.readByteArray(in);
105     if (data.length > 0) {
106       this.proto = SnapshotDescription.parseFrom(data);
107     } else {
108       this.proto = null;
109     }
110   }
111 
112   /**
113    * <em> INTERNAL </em> This method is a part of {@link Writable} interface
114    * and is used for serialization of the HTableDescriptor over RPC
115    */
116   @Override
117   public void write(DataOutput out) throws IOException {
118     if (this.proto != null) {
119       Bytes.writeByteArray(out, this.proto.toByteArray());
120     } else {
121       Bytes.writeByteArray(out, new byte[0]);
122     }
123   }
124 }