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 org.apache.hadoop.hbase.util.ByteStringer;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
37  import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
38  import org.codehaus.jackson.annotate.JsonProperty;
39  
40  /**
41   * Representation of a cell. A cell is a single value associated a column and
42   * optional qualifier, and either the timestamp when it was stored or the user-
43   * provided timestamp if one was explicitly supplied.
44   *
45   * <pre>
46   * &lt;complexType name="Cell"&gt;
47   *   &lt;sequence&gt;
48   *     &lt;element name="value" maxOccurs="1" minOccurs="1"&gt;
49   *       &lt;simpleType&gt;
50   *         &lt;restriction base="base64Binary"/&gt;
51   *       &lt;/simpleType&gt;
52   *     &lt;/element&gt;
53   *   &lt;/sequence&gt;
54   *   &lt;attribute name="column" type="base64Binary" /&gt;
55   *   &lt;attribute name="timestamp" type="int" /&gt;
56   * &lt;/complexType&gt;
57   * </pre>
58   */
59  @XmlRootElement(name="Cell")
60  @XmlAccessorType(XmlAccessType.FIELD)
61  @InterfaceAudience.Private
62  public class CellModel implements ProtobufMessageHandler, Serializable {
63    private static final long serialVersionUID = 1L;
64    
65    @JsonProperty("column")
66    @XmlAttribute
67    private byte[] column;
68  
69    @JsonProperty("timestamp")
70    @XmlAttribute
71    private long timestamp = HConstants.LATEST_TIMESTAMP;
72  
73    @JsonProperty("$")
74    @XmlValue
75    private byte[] value;
76  
77    /**
78     * Default constructor
79     */
80    public CellModel() {}
81  
82    /**
83     * Constructor
84     * @param column
85     * @param value
86     */
87    public CellModel(byte[] column, byte[] value) {
88      this(column, HConstants.LATEST_TIMESTAMP, value);
89    }
90  
91    /**
92     * Constructor
93     * @param column
94     * @param qualifier
95     * @param value
96     */
97    public CellModel(byte[] column, byte[] qualifier, byte[] value) {
98      this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
99    }
100 
101   /**
102    * Constructor from KeyValue
103    * @param kv
104    */
105   public CellModel(KeyValue kv) {
106     this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
107   }
108 
109   /**
110    * Constructor
111    * @param column
112    * @param timestamp
113    * @param value
114    */
115   public CellModel(byte[] column, long timestamp, byte[] value) {
116     this.column = column;
117     this.timestamp = timestamp;
118     this.value = value;
119   }
120 
121   /**
122    * Constructor
123    * @param column
124    * @param qualifier
125    * @param timestamp
126    * @param value
127    */
128   public CellModel(byte[] column, byte[] qualifier, long timestamp,
129       byte[] value) {
130     this.column = KeyValue.makeColumn(column, qualifier);
131     this.timestamp = timestamp;
132     this.value = value;
133   }
134   
135   /**
136    * @return the column
137    */
138   public byte[] getColumn() {
139     return column;
140   }
141 
142   /**
143    * @param column the column to set
144    */
145   public void setColumn(byte[] column) {
146     this.column = column;
147   }
148 
149   /**
150    * @return true if the timestamp property has been specified by the
151    * user
152    */
153   public boolean hasUserTimestamp() {
154     return timestamp != HConstants.LATEST_TIMESTAMP;
155   }
156 
157   /**
158    * @return the timestamp
159    */
160   public long getTimestamp() {
161     return timestamp;
162   }
163 
164   /**
165    * @param timestamp the timestamp to set
166    */
167   public void setTimestamp(long timestamp) {
168     this.timestamp = timestamp;
169   }
170 
171   /**
172    * @return the value
173    */
174   public byte[] getValue() {
175     return value;
176   }
177 
178   /**
179    * @param value the value to set
180    */
181   public void setValue(byte[] value) {
182     this.value = value;
183   }
184 
185   @Override
186   public byte[] createProtobufOutput() {
187     Cell.Builder builder = Cell.newBuilder();
188     builder.setColumn(ByteStringer.wrap(getColumn()));
189     builder.setData(ByteStringer.wrap(getValue()));
190     if (hasUserTimestamp()) {
191       builder.setTimestamp(getTimestamp());
192     }
193     return builder.build().toByteArray();
194   }
195 
196   @Override
197   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
198       throws IOException {
199     Cell.Builder builder = Cell.newBuilder();
200     ProtobufUtil.mergeFrom(builder, message);
201     setColumn(builder.getColumn().toByteArray());
202     setValue(builder.getData().toByteArray());
203     if (builder.hasTimestamp()) {
204       setTimestamp(builder.getTimestamp());
205     }
206     return this;
207   }
208 }