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