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.client;
22
23 import org.apache.hadoop.hbase.HConstants;
24 import org.apache.hadoop.hbase.KeyValue;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.apache.hadoop.io.Writable;
27
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.TreeMap;
35
36
37
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 public class Delete implements Writable, Row, Comparable<Row> {
69 private static final byte DELETE_VERSION = (byte)1;
70
71 private byte [] row = null;
72
73 private long ts;
74 private long lockId = -1L;
75 private final Map<byte [], List<KeyValue>> familyMap =
76 new TreeMap<byte [], List<KeyValue>>(Bytes.BYTES_COMPARATOR);
77
78
79 public Delete() {
80 this((byte [])null);
81 }
82
83
84
85
86
87
88
89
90
91 public Delete(byte [] row) {
92 this(row, HConstants.LATEST_TIMESTAMP, null);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public Delete(byte [] row, long timestamp, RowLock rowLock) {
110 this.row = row;
111 this.ts = timestamp;
112 if (rowLock != null) {
113 this.lockId = rowLock.getLockId();
114 }
115 }
116
117
118
119
120 public Delete(final Delete d) {
121 this.row = d.getRow();
122 this.ts = d.getTimeStamp();
123 this.lockId = d.getLockId();
124 this.familyMap.putAll(d.getFamilyMap());
125 }
126
127 public int compareTo(final Row d) {
128 return Bytes.compareTo(this.getRow(), d.getRow());
129 }
130
131
132
133
134
135 public boolean isEmpty() {
136 return familyMap.isEmpty();
137 }
138
139
140
141
142
143
144
145
146
147 public Delete deleteFamily(byte [] family) {
148 this.deleteFamily(family, HConstants.LATEST_TIMESTAMP);
149 return this;
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public Delete deleteFamily(byte [] family, long timestamp) {
163 List<KeyValue> list = familyMap.get(family);
164 if(list == null) {
165 list = new ArrayList<KeyValue>();
166 } else if(!list.isEmpty()) {
167 list.clear();
168 }
169 list.add(new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamily));
170 familyMap.put(family, list);
171 return this;
172 }
173
174
175
176
177
178
179
180 public Delete deleteColumns(byte [] family, byte [] qualifier) {
181 this.deleteColumns(family, qualifier, HConstants.LATEST_TIMESTAMP);
182 return this;
183 }
184
185
186
187
188
189
190
191
192
193 public Delete deleteColumns(byte [] family, byte [] qualifier, long timestamp) {
194 List<KeyValue> list = familyMap.get(family);
195 if (list == null) {
196 list = new ArrayList<KeyValue>();
197 }
198 list.add(new KeyValue(this.row, family, qualifier, timestamp,
199 KeyValue.Type.DeleteColumn));
200 familyMap.put(family, list);
201 return this;
202 }
203
204
205
206
207
208
209
210
211
212
213 public Delete deleteColumn(byte [] family, byte [] qualifier) {
214 this.deleteColumn(family, qualifier, HConstants.LATEST_TIMESTAMP);
215 return this;
216 }
217
218
219
220
221
222
223
224
225 public Delete deleteColumn(byte [] family, byte [] qualifier, long timestamp) {
226 List<KeyValue> list = familyMap.get(family);
227 if(list == null) {
228 list = new ArrayList<KeyValue>();
229 }
230 list.add(new KeyValue(
231 this.row, family, qualifier, timestamp, KeyValue.Type.Delete));
232 familyMap.put(family, list);
233 return this;
234 }
235
236
237
238
239
240 public Map<byte [], List<KeyValue>> getFamilyMap() {
241 return this.familyMap;
242 }
243
244
245
246
247
248 public byte [] getRow() {
249 return this.row;
250 }
251
252
253
254
255
256 public RowLock getRowLock() {
257 return new RowLock(this.row, this.lockId);
258 }
259
260
261
262
263
264
265 public long getLockId() {
266 return this.lockId;
267 }
268
269
270
271
272
273 public long getTimeStamp() {
274 return this.ts;
275 }
276
277
278
279
280
281
282 public void setTimestamp(long timestamp) {
283 this.ts = timestamp;
284 }
285
286
287
288
289 @Override
290 public String toString() {
291 StringBuilder sb = new StringBuilder();
292 sb.append("row=");
293 sb.append(Bytes.toStringBinary(this.row));
294 sb.append(", ts=");
295 sb.append(this.ts);
296 sb.append(", families={");
297 boolean moreThanOne = false;
298 for(Map.Entry<byte [], List<KeyValue>> entry : this.familyMap.entrySet()) {
299 if(moreThanOne) {
300 sb.append(", ");
301 } else {
302 moreThanOne = true;
303 }
304 sb.append("(family=");
305 sb.append(Bytes.toString(entry.getKey()));
306 sb.append(", keyvalues=(");
307 boolean moreThanOneB = false;
308 for(KeyValue kv : entry.getValue()) {
309 if(moreThanOneB) {
310 sb.append(", ");
311 } else {
312 moreThanOneB = true;
313 }
314 sb.append(kv.toString());
315 }
316 sb.append(")");
317 }
318 sb.append("}");
319 return sb.toString();
320 }
321
322
323 public void readFields(final DataInput in) throws IOException {
324 int version = in.readByte();
325 if (version > DELETE_VERSION) {
326 throw new IOException("version not supported");
327 }
328 this.row = Bytes.readByteArray(in);
329 this.ts = in.readLong();
330 this.lockId = in.readLong();
331 this.familyMap.clear();
332 int numFamilies = in.readInt();
333 for(int i=0;i<numFamilies;i++) {
334 byte [] family = Bytes.readByteArray(in);
335 int numColumns = in.readInt();
336 List<KeyValue> list = new ArrayList<KeyValue>(numColumns);
337 for(int j=0;j<numColumns;j++) {
338 KeyValue kv = new KeyValue();
339 kv.readFields(in);
340 list.add(kv);
341 }
342 this.familyMap.put(family, list);
343 }
344 }
345
346 public void write(final DataOutput out) throws IOException {
347 out.writeByte(DELETE_VERSION);
348 Bytes.writeByteArray(out, this.row);
349 out.writeLong(this.ts);
350 out.writeLong(this.lockId);
351 out.writeInt(familyMap.size());
352 for(Map.Entry<byte [], List<KeyValue>> entry : familyMap.entrySet()) {
353 Bytes.writeByteArray(out, entry.getKey());
354 List<KeyValue> list = entry.getValue();
355 out.writeInt(list.size());
356 for(KeyValue kv : list) {
357 kv.write(out);
358 }
359 }
360 }
361
362
363
364
365
366
367
368
369
370
371 public Delete deleteColumns(byte [] column, long timestamp) {
372 byte [][] parts = KeyValue.parseColumn(column);
373 this.deleteColumns(parts[0], parts[1], timestamp);
374 return this;
375 }
376
377
378
379
380
381
382
383
384 public Delete deleteColumn(byte [] column) {
385 byte [][] parts = KeyValue.parseColumn(column);
386 this.deleteColumn(parts[0], parts[1], HConstants.LATEST_TIMESTAMP);
387 return this;
388 }
389
390
391 }