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.nio.ByteBuffer;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NavigableMap;
28 import java.util.TreeMap;
29 import java.util.UUID;
30
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.classification.InterfaceStability;
33 import org.apache.hadoop.hbase.Cell;
34 import org.apache.hadoop.hbase.CellUtil;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.KeyValue;
37 import org.apache.hadoop.hbase.Tag;
38 import org.apache.hadoop.hbase.io.HeapSize;
39 import org.apache.hadoop.hbase.security.access.Permission;
40 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
41 import org.apache.hadoop.hbase.util.Bytes;
42
43
44
45
46
47
48
49
50 @InterfaceAudience.Public
51 @InterfaceStability.Stable
52 public class Put extends Mutation implements HeapSize, Comparable<Row> {
53
54
55
56
57 public Put(byte [] row) {
58 this(row, HConstants.LATEST_TIMESTAMP);
59 }
60
61
62
63
64
65
66
67 public Put(byte[] row, long ts) {
68 this(row, 0, row.length, ts);
69 }
70
71
72
73
74
75
76
77 public Put(byte [] rowArray, int rowOffset, int rowLength) {
78 this(rowArray, rowOffset, rowLength, HConstants.LATEST_TIMESTAMP);
79 }
80
81
82
83
84
85 public Put(ByteBuffer row, long ts) {
86 if (ts < 0) {
87 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
88 }
89 checkRow(row);
90 this.row = new byte[row.remaining()];
91 row.get(this.row);
92 this.ts = ts;
93 }
94
95
96
97
98 public Put(ByteBuffer row) {
99 this(row, HConstants.LATEST_TIMESTAMP);
100 }
101
102
103
104
105
106
107
108
109 public Put(byte [] rowArray, int rowOffset, int rowLength, long ts) {
110 checkRow(rowArray, rowOffset, rowLength);
111 this.row = Bytes.copy(rowArray, rowOffset, rowLength);
112 this.ts = ts;
113 if (ts < 0) {
114 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
115 }
116 }
117
118
119
120
121
122 public Put(Put putToCopy) {
123 this(putToCopy.getRow(), putToCopy.ts);
124 this.familyMap = new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
125 for(Map.Entry<byte [], List<Cell>> entry: putToCopy.getFamilyCellMap().entrySet()) {
126 this.familyMap.put(entry.getKey(), new ArrayList<Cell>(entry.getValue()));
127 }
128 this.durability = putToCopy.durability;
129 for (Map.Entry<String, byte[]> entry : putToCopy.getAttributesMap().entrySet()) {
130 this.setAttribute(entry.getKey(), entry.getValue());
131 }
132 }
133
134
135
136
137
138
139
140
141
142 @Deprecated
143 public Put add(byte [] family, byte [] qualifier, byte [] value) {
144 return addColumn(family, qualifier, value);
145 }
146
147
148
149
150
151
152
153
154 public Put addColumn(byte [] family, byte [] qualifier, byte [] value) {
155 return addColumn(family, qualifier, this.ts, value);
156 }
157
158
159
160
161
162
163 public Put addImmutable(byte [] family, byte [] qualifier, byte [] value) {
164 return addImmutable(family, qualifier, this.ts, value);
165 }
166
167
168
169
170
171
172
173 @InterfaceAudience.Private
174 public Put addImmutable(byte[] family, byte [] qualifier, byte [] value, Tag[] tag) {
175 return addImmutable(family, qualifier, this.ts, value, tag);
176 }
177
178
179
180
181
182
183
184
185
186
187
188 @Deprecated
189 public Put add(byte [] family, byte [] qualifier, long ts, byte [] value) {
190 return addColumn(family, qualifier, ts, value);
191 }
192
193
194
195
196
197
198
199
200
201
202 public Put addColumn(byte [] family, byte [] qualifier, long ts, byte [] value) {
203 if (ts < 0) {
204 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
205 }
206 List<Cell> list = getCellList(family);
207 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
208 list.add(kv);
209 familyMap.put(CellUtil.cloneFamily(kv), list);
210 return this;
211 }
212
213
214
215
216
217
218 public Put addImmutable(byte [] family, byte [] qualifier, long ts, byte [] value) {
219 if (ts < 0) {
220 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
221 }
222 List<Cell> list = getCellList(family);
223 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
224 list.add(kv);
225 familyMap.put(family, list);
226 return this;
227 }
228
229
230
231
232
233
234
235 @InterfaceAudience.Private
236 public Put addImmutable(byte[] family, byte[] qualifier, long ts, byte[] value, Tag[] tag) {
237 List<Cell> list = getCellList(family);
238 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, tag);
239 list.add(kv);
240 familyMap.put(family, list);
241 return this;
242 }
243
244
245
246
247
248
249
250 @InterfaceAudience.Private
251 public Put addImmutable(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value,
252 Tag[] tag) {
253 if (ts < 0) {
254 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
255 }
256 List<Cell> list = getCellList(family);
257 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, tag);
258 list.add(kv);
259 familyMap.put(family, list);
260 return this;
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274 @Deprecated
275 public Put add(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) {
276 return addColumn(family, qualifier, ts, value);
277 }
278
279
280
281
282
283
284
285
286
287
288 public Put addColumn(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) {
289 if (ts < 0) {
290 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
291 }
292 List<Cell> list = getCellList(family);
293 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, null);
294 list.add(kv);
295 familyMap.put(CellUtil.cloneFamily(kv), list);
296 return this;
297 }
298
299
300
301
302
303
304 public Put addImmutable(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) {
305 if (ts < 0) {
306 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
307 }
308 List<Cell> list = getCellList(family);
309 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, null);
310 list.add(kv);
311 familyMap.put(family, list);
312 return this;
313 }
314
315
316
317
318
319
320
321
322
323 public Put add(Cell kv) throws IOException{
324 byte [] family = CellUtil.cloneFamily(kv);
325 List<Cell> list = getCellList(family);
326
327 int res = Bytes.compareTo(this.row, 0, row.length,
328 kv.getRowArray(), kv.getRowOffset(), kv.getRowLength());
329 if (res != 0) {
330 throw new WrongRowIOException("The row in " + kv.toString() +
331 " doesn't match the original one " + Bytes.toStringBinary(this.row));
332 }
333 list.add(kv);
334 familyMap.put(family, list);
335 return this;
336 }
337
338
339
340
341
342
343
344
345
346
347
348 public boolean has(byte [] family, byte [] qualifier) {
349 return has(family, qualifier, this.ts, new byte[0], true, true);
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363 public boolean has(byte [] family, byte [] qualifier, long ts) {
364 return has(family, qualifier, ts, new byte[0], false, true);
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378 public boolean has(byte [] family, byte [] qualifier, byte [] value) {
379 return has(family, qualifier, this.ts, value, true, false);
380 }
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public boolean has(byte [] family, byte [] qualifier, long ts, byte [] value) {
395 return has(family, qualifier, ts, value, false, false);
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 private boolean has(byte[] family, byte[] qualifier, long ts, byte[] value,
413 boolean ignoreTS, boolean ignoreValue) {
414 List<Cell> list = getCellList(family);
415 if (list.size() == 0) {
416 return false;
417 }
418
419
420
421
422
423 if (!ignoreTS && !ignoreValue) {
424 for (Cell cell : list) {
425 if (CellUtil.matchingFamily(cell, family) &&
426 CellUtil.matchingQualifier(cell, qualifier) &&
427 CellUtil.matchingValue(cell, value) &&
428 cell.getTimestamp() == ts) {
429 return true;
430 }
431 }
432 } else if (ignoreValue && !ignoreTS) {
433 for (Cell cell : list) {
434 if (CellUtil.matchingFamily(cell, family) && CellUtil.matchingQualifier(cell, qualifier)
435 && cell.getTimestamp() == ts) {
436 return true;
437 }
438 }
439 } else if (!ignoreValue && ignoreTS) {
440 for (Cell cell : list) {
441 if (CellUtil.matchingFamily(cell, family) && CellUtil.matchingQualifier(cell, qualifier)
442 && CellUtil.matchingValue(cell, value)) {
443 return true;
444 }
445 }
446 } else {
447 for (Cell cell : list) {
448 if (CellUtil.matchingFamily(cell, family) &&
449 CellUtil.matchingQualifier(cell, qualifier)) {
450 return true;
451 }
452 }
453 }
454 return false;
455 }
456
457
458
459
460
461
462
463
464
465 public List<Cell> get(byte[] family, byte[] qualifier) {
466 List<Cell> filteredList = new ArrayList<Cell>();
467 for (Cell cell: getCellList(family)) {
468 if (CellUtil.matchingQualifier(cell, qualifier)) {
469 filteredList.add(cell);
470 }
471 }
472 return filteredList;
473 }
474
475 @Override
476 public Put setAttribute(String name, byte[] value) {
477 return (Put) super.setAttribute(name, value);
478 }
479
480 @Override
481 public Put setId(String id) {
482 return (Put) super.setId(id);
483 }
484
485 @Override
486 @Deprecated
487 public Put setWriteToWAL(boolean write) {
488 return (Put) super.setWriteToWAL(write);
489 }
490
491 @Override
492 public Put setDurability(Durability d) {
493 return (Put) super.setDurability(d);
494 }
495
496 @Override
497 public Put setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
498 return (Put) super.setFamilyCellMap(map);
499 }
500
501 @Override
502 @Deprecated
503 public Put setFamilyMap(NavigableMap<byte[], List<KeyValue>> map) {
504 return (Put) super.setFamilyMap(map);
505 }
506
507 @Override
508 public Put setClusterIds(List<UUID> clusterIds) {
509 return (Put) super.setClusterIds(clusterIds);
510 }
511
512 @Override
513 public Put setCellVisibility(CellVisibility expression) {
514 return (Put) super.setCellVisibility(expression);
515 }
516
517 @Override
518 public Put setACL(String user, Permission perms) {
519 return (Put) super.setACL(user, perms);
520 }
521
522 @Override
523 public Put setACL(Map<String, Permission> perms) {
524 return (Put) super.setACL(perms);
525 }
526
527 @Override
528 public Put setTTL(long ttl) {
529 return (Put) super.setTTL(ttl);
530 }
531 }