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