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