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.Cell;
30 import org.apache.hadoop.hbase.CellUtil;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.KeyValue;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.hbase.classification.InterfaceStability;
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 public Delete addFamily(final byte [] family) {
191 this.addFamily(family, this.ts);
192 return this;
193 }
194
195
196
197
198
199
200
201
202
203
204
205 public Delete addFamily(final byte [] family, final long timestamp) {
206 if (timestamp < 0) {
207 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
208 }
209 List<Cell> list = familyMap.get(family);
210 if(list == null) {
211 list = new ArrayList<Cell>();
212 } else if(!list.isEmpty()) {
213 list.clear();
214 }
215 KeyValue kv = new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamily);
216 list.add(kv);
217 familyMap.put(family, list);
218 return this;
219 }
220
221
222
223
224
225
226
227
228 public Delete addFamilyVersion(final byte [] family, final long timestamp) {
229 List<Cell> list = familyMap.get(family);
230 if(list == null) {
231 list = new ArrayList<Cell>();
232 }
233 list.add(new KeyValue(row, family, null, timestamp,
234 KeyValue.Type.DeleteFamilyVersion));
235 familyMap.put(family, list);
236 return this;
237 }
238
239
240
241
242
243
244
245 public Delete addColumns(final byte [] family, final byte [] qualifier) {
246 addColumns(family, qualifier, this.ts);
247 return this;
248 }
249
250
251
252
253
254
255
256
257
258 public Delete addColumns(final byte [] family, final byte [] qualifier, final long timestamp) {
259 if (timestamp < 0) {
260 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
261 }
262 List<Cell> list = familyMap.get(family);
263 if (list == null) {
264 list = new ArrayList<Cell>();
265 }
266 list.add(new KeyValue(this.row, family, qualifier, timestamp,
267 KeyValue.Type.DeleteColumn));
268 familyMap.put(family, list);
269 return this;
270 }
271
272
273
274
275
276
277
278
279
280
281 public Delete addColumn(final byte [] family, final byte [] qualifier) {
282 this.addColumn(family, qualifier, this.ts);
283 return this;
284 }
285
286
287
288
289
290
291
292
293 public Delete addColumn(byte [] family, byte [] qualifier, long timestamp) {
294 if (timestamp < 0) {
295 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
296 }
297 List<Cell> list = familyMap.get(family);
298 if(list == null) {
299 list = new ArrayList<Cell>();
300 }
301 KeyValue kv = new KeyValue(this.row, family, qualifier, timestamp, KeyValue.Type.Delete);
302 list.add(kv);
303 familyMap.put(family, list);
304 return this;
305 }
306
307
308
309
310
311
312 public Delete setTimestamp(long timestamp) {
313 if (timestamp < 0) {
314 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
315 }
316 this.ts = timestamp;
317 return this;
318 }
319
320 @Override
321 public Map<String, Object> toMap(int maxCols) {
322
323 Map<String, Object> map = super.toMap(maxCols);
324
325 map.put("ts", this.ts);
326 return map;
327 }
328
329 @Override
330 public Delete setAttribute(String name, byte[] value) {
331 return (Delete) super.setAttribute(name, value);
332 }
333
334 @Override
335 public Delete setId(String id) {
336 return (Delete) super.setId(id);
337 }
338
339 @Override
340 public Delete setDurability(Durability d) {
341 return (Delete) super.setDurability(d);
342 }
343
344 @Override
345 public Delete setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
346 return (Delete) super.setFamilyCellMap(map);
347 }
348
349 @Override
350 public Delete setClusterIds(List<UUID> clusterIds) {
351 return (Delete) super.setClusterIds(clusterIds);
352 }
353
354 @Override
355 public Delete setCellVisibility(CellVisibility expression) {
356 return (Delete) super.setCellVisibility(expression);
357 }
358
359 @Override
360 public Delete setACL(String user, Permission perms) {
361 return (Delete) super.setACL(user, perms);
362 }
363
364 @Override
365 public Delete setACL(Map<String, Permission> perms) {
366 return (Delete) super.setACL(perms);
367 }
368
369 @Override
370 public Delete setTTL(long ttl) {
371 throw new UnsupportedOperationException("Setting TTLs on Deletes is not supported");
372 }
373 }