1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.KeyValue;
34 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
35 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
36 import org.codehaus.jackson.annotate.JsonProperty;
37
38 import com.google.protobuf.ZeroCopyLiteralByteString;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
79
80 public CellModel() {}
81
82
83
84
85
86
87 public CellModel(byte[] column, byte[] value) {
88 this(column, HConstants.LATEST_TIMESTAMP, value);
89 }
90
91
92
93
94
95
96
97 public CellModel(byte[] column, byte[] qualifier, byte[] value) {
98 this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
99 }
100
101
102
103
104
105 public CellModel(KeyValue kv) {
106 this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
107 }
108
109
110
111
112
113
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
123
124
125
126
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
137
138 public byte[] getColumn() {
139 return column;
140 }
141
142
143
144
145 public void setColumn(byte[] column) {
146 this.column = column;
147 }
148
149
150
151
152
153 public boolean hasUserTimestamp() {
154 return timestamp != HConstants.LATEST_TIMESTAMP;
155 }
156
157
158
159
160 public long getTimestamp() {
161 return timestamp;
162 }
163
164
165
166
167 public void setTimestamp(long timestamp) {
168 this.timestamp = timestamp;
169 }
170
171
172
173
174 public byte[] getValue() {
175 return value;
176 }
177
178
179
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(ZeroCopyLiteralByteString.wrap(getColumn()));
189 builder.setData(ZeroCopyLiteralByteString.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 builder.mergeFrom(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 }