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