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 @Override
281 public String toString() {
282 StringBuilder sb = new StringBuilder();
283 sb.append("row=");
284 sb.append(Bytes.toString(this.row));
285 sb.append(", ts=");
286 sb.append(this.ts);
287 sb.append(", families={");
288 boolean moreThanOne = false;
289 for(Map.Entry<byte [], List<KeyValue>> entry : this.familyMap.entrySet()) {
290 if(moreThanOne) {
291 sb.append(", ");
292 } else {
293 moreThanOne = true;
294 }
295 sb.append("(family=");
296 sb.append(Bytes.toString(entry.getKey()));
297 sb.append(", keyvalues=(");
298 boolean moreThanOneB = false;
299 for(KeyValue kv : entry.getValue()) {
300 if(moreThanOneB) {
301 sb.append(", ");
302 } else {
303 moreThanOneB = true;
304 }
305 sb.append(kv.toString());
306 }
307 sb.append(")");
308 }
309 sb.append("}");
310 return sb.toString();
311 }
312
313
314 public void readFields(final DataInput in) throws IOException {
315 int version = in.readByte();
316 if (version > DELETE_VERSION) {
317 throw new IOException("version not supported");
318 }
319 this.row = Bytes.readByteArray(in);
320 this.ts = in.readLong();
321 this.lockId = in.readLong();
322 this.familyMap.clear();
323 int numFamilies = in.readInt();
324 for(int i=0;i<numFamilies;i++) {
325 byte [] family = Bytes.readByteArray(in);
326 int numColumns = in.readInt();
327 List<KeyValue> list = new ArrayList<KeyValue>(numColumns);
328 for(int j=0;j<numColumns;j++) {
329 KeyValue kv = new KeyValue();
330 kv.readFields(in);
331 list.add(kv);
332 }
333 this.familyMap.put(family, list);
334 }
335 }
336
337 public void write(final DataOutput out) throws IOException {
338 out.writeByte(DELETE_VERSION);
339 Bytes.writeByteArray(out, this.row);
340 out.writeLong(this.ts);
341 out.writeLong(this.lockId);
342 out.writeInt(familyMap.size());
343 for(Map.Entry<byte [], List<KeyValue>> entry : familyMap.entrySet()) {
344 Bytes.writeByteArray(out, entry.getKey());
345 List<KeyValue> list = entry.getValue();
346 out.writeInt(list.size());
347 for(KeyValue kv : list) {
348 kv.write(out);
349 }
350 }
351 }
352
353
354
355
356
357
358
359
360
361
362 public Delete deleteColumns(byte [] column, long timestamp) {
363 byte [][] parts = KeyValue.parseColumn(column);
364 this.deleteColumns(parts[0], parts[1], timestamp);
365 return this;
366 }
367
368
369
370
371
372
373
374
375 public Delete deleteColumn(byte [] column) {
376 byte [][] parts = KeyValue.parseColumn(column);
377 this.deleteColumn(parts[0], parts[1], HConstants.LATEST_TIMESTAMP);
378 return this;
379 }
380
381
382 }