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.CellUtil;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.KeyValue;
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 cell
104    */
105   public CellModel(org.apache.hadoop.hbase.Cell cell) {
106     this(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil
107         .cloneValue(cell));
108   }
109 
110   /**
111    * Constructor
112    * @param column
113    * @param timestamp
114    * @param value
115    */
116   public CellModel(byte[] column, long timestamp, byte[] value) {
117     this.column = column;
118     this.timestamp = timestamp;
119     this.value = value;
120   }
121 
122   /**
123    * Constructor
124    * @param column
125    * @param qualifier
126    * @param timestamp
127    * @param value
128    */
129   public CellModel(byte[] column, byte[] qualifier, long timestamp,
130       byte[] value) {
131     this.column = KeyValue.makeColumn(column, qualifier);
132     this.timestamp = timestamp;
133     this.value = value;
134   }
135   
136   /**
137    * @return the column
138    */
139   public byte[] getColumn() {
140     return column;
141   }
142 
143   /**
144    * @param column the column to set
145    */
146   public void setColumn(byte[] column) {
147     this.column = column;
148   }
149 
150   /**
151    * @return true if the timestamp property has been specified by the
152    * user
153    */
154   public boolean hasUserTimestamp() {
155     return timestamp != HConstants.LATEST_TIMESTAMP;
156   }
157 
158   /**
159    * @return the timestamp
160    */
161   public long getTimestamp() {
162     return timestamp;
163   }
164 
165   /**
166    * @param timestamp the timestamp to set
167    */
168   public void setTimestamp(long timestamp) {
169     this.timestamp = timestamp;
170   }
171 
172   /**
173    * @return the value
174    */
175   public byte[] getValue() {
176     return value;
177   }
178 
179   /**
180    * @param value the value to set
181    */
182   public void setValue(byte[] value) {
183     this.value = value;
184   }
185 
186   @Override
187   public byte[] createProtobufOutput() {
188     Cell.Builder builder = Cell.newBuilder();
189     builder.setColumn(ByteStringer.wrap(getColumn()));
190     builder.setData(ByteStringer.wrap(getValue()));
191     if (hasUserTimestamp()) {
192       builder.setTimestamp(getTimestamp());
193     }
194     return builder.build().toByteArray();
195   }
196 
197   @Override
198   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
199       throws IOException {
200     Cell.Builder builder = Cell.newBuilder();
201     builder.mergeFrom(message);
202     setColumn(builder.getColumn().toByteArray());
203     setValue(builder.getData().toByteArray());
204     if (builder.hasTimestamp()) {
205       setTimestamp(builder.getTimestamp());
206     }
207     return this;
208   }
209 }