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.client;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.apache.hadoop.classification.InterfaceAudience;
30 import org.apache.hadoop.classification.InterfaceStability;
31 import org.apache.hadoop.hbase.Cell;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.KeyValue;
34 import org.apache.hadoop.hbase.KeyValueUtil;
35 import org.apache.hadoop.hbase.io.HeapSize;
36 import org.apache.hadoop.hbase.util.Bytes;
37
38
39
40
41
42
43
44
45 @InterfaceAudience.Public
46 @InterfaceStability.Stable
47 public class Put extends Mutation implements HeapSize, Comparable<Row> {
48
49
50
51
52 public Put(byte [] row) {
53 this(row, HConstants.LATEST_TIMESTAMP);
54 }
55
56
57
58
59
60
61
62 public Put(byte[] row, long ts) {
63 this(row, 0, row.length, ts);
64 }
65
66
67
68
69
70
71
72 public Put(byte [] rowArray, int rowOffset, int rowLength) {
73 this(rowArray, rowOffset, rowLength, HConstants.LATEST_TIMESTAMP);
74 }
75
76
77
78
79
80
81
82
83 public Put(byte [] rowArray, int rowOffset, int rowLength, long ts) {
84 checkRow(rowArray, rowOffset, rowLength);
85 this.row = Bytes.copy(rowArray, rowOffset, rowLength);
86 this.ts = ts;
87 }
88
89
90
91
92
93 public Put(Put putToCopy) {
94 this(putToCopy.getRow(), putToCopy.ts);
95 this.familyMap = new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
96 for(Map.Entry<byte [], List<Cell>> entry: putToCopy.getFamilyCellMap().entrySet()) {
97 this.familyMap.put(entry.getKey(), entry.getValue());
98 }
99 this.durability = putToCopy.durability;
100 }
101
102
103
104
105
106
107
108
109 public Put add(byte [] family, byte [] qualifier, byte [] value) {
110 return add(family, qualifier, this.ts, value);
111 }
112
113
114
115
116
117
118
119
120
121
122 @SuppressWarnings("unchecked")
123 public Put add(byte [] family, byte [] qualifier, long ts, byte [] value) {
124 List<Cell> list = getCellList(family);
125 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
126 list.add(kv);
127 familyMap.put(kv.getFamily(), list);
128 return this;
129 }
130
131
132
133
134
135
136
137
138
139 @SuppressWarnings("unchecked")
140 public Put add(KeyValue kv) throws IOException{
141 byte [] family = kv.getFamily();
142 List<Cell> list = getCellList(family);
143
144 int res = Bytes.compareTo(this.row, 0, row.length,
145 kv.getBuffer(), kv.getRowOffset(), kv.getRowLength());
146 if (res != 0) {
147 throw new WrongRowIOException("The row in " + kv.toString() +
148 " doesn't match the original one " + Bytes.toStringBinary(this.row));
149 }
150 list.add(kv);
151 familyMap.put(family, list);
152 return this;
153 }
154
155
156
157
158
159
160
161
162
163
164
165 public boolean has(byte [] family, byte [] qualifier) {
166 return has(family, qualifier, this.ts, new byte[0], true, true);
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 public boolean has(byte [] family, byte [] qualifier, long ts) {
181 return has(family, qualifier, ts, new byte[0], false, true);
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195 public boolean has(byte [] family, byte [] qualifier, byte [] value) {
196 return has(family, qualifier, this.ts, value, true, false);
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public boolean has(byte [] family, byte [] qualifier, long ts, byte [] value) {
212 return has(family, qualifier, ts, value, false, false);
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 private boolean has(byte[] family, byte[] qualifier, long ts, byte[] value,
230 boolean ignoreTS, boolean ignoreValue) {
231 List<Cell> list = getCellList(family);
232 if (list.size() == 0) {
233 return false;
234 }
235
236
237
238
239
240 if (!ignoreTS && !ignoreValue) {
241 for (Cell cell : list) {
242 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
243 if (Arrays.equals(kv.getFamily(), family) &&
244 Arrays.equals(kv.getQualifier(), qualifier) &&
245 Arrays.equals(kv.getValue(), value) &&
246 kv.getTimestamp() == ts) {
247 return true;
248 }
249 }
250 } else if (ignoreValue && !ignoreTS) {
251 for (Cell cell : list) {
252 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
253 if (Arrays.equals(kv.getFamily(), family) && Arrays.equals(kv.getQualifier(), qualifier)
254 && kv.getTimestamp() == ts) {
255 return true;
256 }
257 }
258 } else if (!ignoreValue && ignoreTS) {
259 for (Cell cell : list) {
260 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
261 if (Arrays.equals(kv.getFamily(), family) && Arrays.equals(kv.getQualifier(), qualifier)
262 && Arrays.equals(kv.getValue(), value)) {
263 return true;
264 }
265 }
266 } else {
267 for (Cell cell : list) {
268 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
269 if (Arrays.equals(kv.getFamily(), family) &&
270 Arrays.equals(kv.getQualifier(), qualifier)) {
271 return true;
272 }
273 }
274 }
275 return false;
276 }
277
278
279
280
281
282
283
284
285
286 public List<KeyValue> get(byte[] family, byte[] qualifier) {
287 List<KeyValue> filteredList = new ArrayList<KeyValue>();
288 for (Cell cell: getCellList(family)) {
289 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
290 if (Arrays.equals(kv.getQualifier(), qualifier)) {
291 filteredList.add(kv);
292 }
293 }
294 return filteredList;
295 }
296 }