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