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.hbase.util.ByteStringer;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
36 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
37 import org.codehaus.jackson.annotate.JsonProperty;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @XmlRootElement(name="Cell")
59 @XmlAccessorType(XmlAccessType.FIELD)
60 @InterfaceAudience.Private
61 public class CellModel implements ProtobufMessageHandler, Serializable {
62 private static final long serialVersionUID = 1L;
63
64 @JsonProperty("column")
65 @XmlAttribute
66 private byte[] column;
67
68 @JsonProperty("timestamp")
69 @XmlAttribute
70 private long timestamp = HConstants.LATEST_TIMESTAMP;
71
72 @JsonProperty("$")
73 @XmlValue
74 private byte[] value;
75
76
77
78
79 public CellModel() {}
80
81
82
83
84
85
86 public CellModel(byte[] column, byte[] value) {
87 this(column, HConstants.LATEST_TIMESTAMP, value);
88 }
89
90
91
92
93
94
95
96 public CellModel(byte[] column, byte[] qualifier, byte[] value) {
97 this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
98 }
99
100
101
102
103
104 public CellModel(KeyValue kv) {
105 this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
106 }
107
108
109
110
111
112
113
114 public CellModel(byte[] column, long timestamp, byte[] value) {
115 this.column = column;
116 this.timestamp = timestamp;
117 this.value = value;
118 }
119
120
121
122
123
124
125
126
127 public CellModel(byte[] column, byte[] qualifier, long timestamp,
128 byte[] value) {
129 this.column = KeyValue.makeColumn(column, qualifier);
130 this.timestamp = timestamp;
131 this.value = value;
132 }
133
134
135
136
137 public byte[] getColumn() {
138 return column;
139 }
140
141
142
143
144 public void setColumn(byte[] column) {
145 this.column = column;
146 }
147
148
149
150
151
152 public boolean hasUserTimestamp() {
153 return timestamp != HConstants.LATEST_TIMESTAMP;
154 }
155
156
157
158
159 public long getTimestamp() {
160 return timestamp;
161 }
162
163
164
165
166 public void setTimestamp(long timestamp) {
167 this.timestamp = timestamp;
168 }
169
170
171
172
173 public byte[] getValue() {
174 return value;
175 }
176
177
178
179
180 public void setValue(byte[] value) {
181 this.value = value;
182 }
183
184 @Override
185 public byte[] createProtobufOutput() {
186 Cell.Builder builder = Cell.newBuilder();
187 builder.setColumn(ByteStringer.wrap(getColumn()));
188 builder.setData(ByteStringer.wrap(getValue()));
189 if (hasUserTimestamp()) {
190 builder.setTimestamp(getTimestamp());
191 }
192 return builder.build().toByteArray();
193 }
194
195 @Override
196 public ProtobufMessageHandler getObjectFromMessage(byte[] message)
197 throws IOException {
198 Cell.Builder builder = Cell.newBuilder();
199 builder.mergeFrom(message);
200 setColumn(builder.getColumn().toByteArray());
201 setValue(builder.getData().toByteArray());
202 if (builder.hasTimestamp()) {
203 setTimestamp(builder.getTimestamp());
204 }
205 return this;
206 }
207 }