View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.model;
22  
23  import java.io.IOException;
24  import java.io.Serializable;
25  
26  import javax.xml.bind.annotation.XmlAttribute;
27  import javax.xml.bind.annotation.XmlRootElement;
28  import javax.xml.bind.annotation.XmlValue;
29  
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  public class CellModel implements ProtobufMessageHandler, Serializable {
58    private static final long serialVersionUID = 1L;
59    
60    private long timestamp = HConstants.LATEST_TIMESTAMP;
61    private byte[] column;
62    private byte[] value;
63  
64    /**
65     * Default constructor
66     */
67    public CellModel() {}
68  
69    /**
70     * Constructor
71     * @param column
72     * @param value
73     */
74    public CellModel(byte[] column, byte[] value) {
75      this(column, HConstants.LATEST_TIMESTAMP, value);
76    }
77  
78    /**
79     * Constructor
80     * @param column
81     * @param qualifier
82     * @param value
83     */
84    public CellModel(byte[] column, byte[] qualifier, byte[] value) {
85      this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
86    }
87  
88    /**
89     * Constructor from KeyValue
90     * @param kv
91     */
92    public CellModel(KeyValue kv) {
93      this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
94    }
95  
96    /**
97     * Constructor
98     * @param column
99     * @param timestamp
100    * @param value
101    */
102   public CellModel(byte[] column, long timestamp, byte[] value) {
103     this.column = column;
104     this.timestamp = timestamp;
105     this.value = value;
106   }
107 
108   /**
109    * Constructor
110    * @param column
111    * @param qualifier
112    * @param timestamp
113    * @param value
114    */
115   public CellModel(byte[] column, byte[] qualifier, long timestamp,
116       byte[] value) {
117     this.column = KeyValue.makeColumn(column, qualifier);
118     this.timestamp = timestamp;
119     this.value = value;
120   }
121   
122   /**
123    * @return the column
124    */
125   @XmlAttribute
126   public byte[] getColumn() {
127     return column;
128   }
129 
130   /**
131    * @param column the column to set
132    */
133   public void setColumn(byte[] column) {
134     this.column = column;
135   }
136 
137   /**
138    * @return true if the timestamp property has been specified by the
139    * user
140    */
141   public boolean hasUserTimestamp() {
142     return timestamp != HConstants.LATEST_TIMESTAMP;
143   }
144 
145   /**
146    * @return the timestamp
147    */
148   @XmlAttribute
149   public long getTimestamp() {
150     return timestamp;
151   }
152 
153   /**
154    * @param timestamp the timestamp to set
155    */
156   public void setTimestamp(long timestamp) {
157     this.timestamp = timestamp;
158   }
159 
160   /**
161    * @return the value
162    */
163   @XmlValue
164   public byte[] getValue() {
165     return value;
166   }
167 
168   /**
169    * @param value the value to set
170    */
171   public void setValue(byte[] value) {
172     this.value = value;
173   }
174 
175   @Override
176   public byte[] createProtobufOutput() {
177     Cell.Builder builder = Cell.newBuilder();
178     builder.setColumn(ByteString.copyFrom(getColumn()));
179     builder.setData(ByteString.copyFrom(getValue()));
180     if (hasUserTimestamp()) {
181       builder.setTimestamp(getTimestamp());
182     }
183     return builder.build().toByteArray();
184   }
185 
186   @Override
187   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
188       throws IOException {
189     Cell.Builder builder = Cell.newBuilder();
190     builder.mergeFrom(message);
191     setColumn(builder.getColumn().toByteArray());
192     setValue(builder.getData().toByteArray());
193     if (builder.hasTimestamp()) {
194       setTimestamp(builder.getTimestamp());
195     }
196     return this;
197   }
198 }