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.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
67
68 public CellModel() {}
69
70
71
72
73
74
75 public CellModel(byte[] column, byte[] value) {
76 this(column, HConstants.LATEST_TIMESTAMP, value);
77 }
78
79
80
81
82
83
84
85 public CellModel(byte[] column, byte[] qualifier, byte[] value) {
86 this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
87 }
88
89
90
91
92
93 public CellModel(KeyValue kv) {
94 this(kv.getFamily(), kv.getQualifier(), kv.getTimestamp(), kv.getValue());
95 }
96
97
98
99
100
101
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
111
112
113
114
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
125
126 @XmlAttribute
127 public byte[] getColumn() {
128 return column;
129 }
130
131
132
133
134 public void setColumn(byte[] column) {
135 this.column = column;
136 }
137
138
139
140
141
142 public boolean hasUserTimestamp() {
143 return timestamp != HConstants.LATEST_TIMESTAMP;
144 }
145
146
147
148
149 @XmlAttribute
150 public long getTimestamp() {
151 return timestamp;
152 }
153
154
155
156
157 public void setTimestamp(long timestamp) {
158 this.timestamp = timestamp;
159 }
160
161
162
163
164 @XmlValue
165 public byte[] getValue() {
166 return value;
167 }
168
169
170
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 }