View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlAttribute;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlValue;
30  
31  import com.google.protobuf.HBaseZeroCopyByteString;
32  import org.apache.hadoop.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
36  import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
37  import org.codehaus.jackson.annotate.JsonProperty;
38  
39  /**
40   * Representation of a cell. A cell is a single value associated a column and
41   * optional qualifier, and either the timestamp when it was stored or the user-
42   * provided timestamp if one was explicitly supplied.
43   *
44   * <pre>
45   * &lt;complexType name="Cell"&gt;
46   *   &lt;sequence&gt;
47   *     &lt;element name="value" maxOccurs="1" minOccurs="1"&gt;
48   *       &lt;simpleType&gt;
49   *         &lt;restriction base="base64Binary"/&gt;
50   *       &lt;/simpleType&gt;
51   *     &lt;/element&gt;
52   *   &lt;/sequence&gt;
53   *   &lt;attribute name="column" type="base64Binary" /&gt;
54   *   &lt;attribute name="timestamp" type="int" /&gt;
55   * &lt;/complexType&gt;
56   * </pre>
57   */
58  @XmlRootElement(name="Cell")
59  @XmlAccessorType(XmlAccessType.FIELD)
60  @InterfaceAudience.Private
61  public class CellModel implements ProtobufMessageHandler, Serializable {
62    private static final long serialVersionUID = 1L;
63    
64    @JsonProperty("column")
65    @XmlAttribute
66    private byte[] column;
67  
68    @JsonProperty("timestamp")
69    @XmlAttribute
70    private long timestamp = HConstants.LATEST_TIMESTAMP;
71  
72    @JsonProperty("$")
73    @XmlValue
74    private byte[] value;
75  
76    /**
77     * Default constructor
78     */
79    public CellModel() {}
80  
81    /**
82     * Constructor
83     * @param column
84     * @param value
85     */
86    public CellModel(byte[] column, byte[] value) {
87      this(column, HConstants.LATEST_TIMESTAMP, value);
88    }
89  
90    /**
91     * Constructor
92     * @param column
93     * @param qualifier
94     * @param value
95     */
96    public CellModel(byte[] column, byte[] qualifier, byte[] value) {
97      this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
98    }
99  
100   /**
101    * Constructor from KeyValue
102    * @param kv
103    */
104   public CellModel(KeyValue kv) {
105     this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
106   }
107 
108   /**
109    * Constructor
110    * @param column
111    * @param timestamp
112    * @param value
113    */
114   public CellModel(byte[] column, long timestamp, byte[] value) {
115     this.column = column;
116     this.timestamp = timestamp;
117     this.value = value;
118   }
119 
120   /**
121    * Constructor
122    * @param column
123    * @param qualifier
124    * @param timestamp
125    * @param value
126    */
127   public CellModel(byte[] column, byte[] qualifier, long timestamp,
128       byte[] value) {
129     this.column = KeyValue.makeColumn(column, qualifier);
130     this.timestamp = timestamp;
131     this.value = value;
132   }
133   
134   /**
135    * @return the column
136    */
137   public byte[] getColumn() {
138     return column;
139   }
140 
141   /**
142    * @param column the column to set
143    */
144   public void setColumn(byte[] column) {
145     this.column = column;
146   }
147 
148   /**
149    * @return true if the timestamp property has been specified by the
150    * user
151    */
152   public boolean hasUserTimestamp() {
153     return timestamp != HConstants.LATEST_TIMESTAMP;
154   }
155 
156   /**
157    * @return the timestamp
158    */
159   public long getTimestamp() {
160     return timestamp;
161   }
162 
163   /**
164    * @param timestamp the timestamp to set
165    */
166   public void setTimestamp(long timestamp) {
167     this.timestamp = timestamp;
168   }
169 
170   /**
171    * @return the value
172    */
173   public byte[] getValue() {
174     return value;
175   }
176 
177   /**
178    * @param value the value to set
179    */
180   public void setValue(byte[] value) {
181     this.value = value;
182   }
183 
184   @Override
185   public byte[] createProtobufOutput() {
186     Cell.Builder builder = Cell.newBuilder();
187     builder.setColumn(HBaseZeroCopyByteString.wrap(getColumn()));
188     builder.setData(HBaseZeroCopyByteString.wrap(getValue()));
189     if (hasUserTimestamp()) {
190       builder.setTimestamp(getTimestamp());
191     }
192     return builder.build().toByteArray();
193   }
194 
195   @Override
196   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
197       throws IOException {
198     Cell.Builder builder = Cell.newBuilder();
199     builder.mergeFrom(message);
200     setColumn(builder.getColumn().toByteArray());
201     setValue(builder.getData().toByteArray());
202     if (builder.hasTimestamp()) {
203       setTimestamp(builder.getTimestamp());
204     }
205     return this;
206   }
207 }