1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
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.CellScannable;
35 import org.apache.hadoop.hbase.CellScanner;
36 import org.apache.hadoop.hbase.CellUtil;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.KeyValue;
39 import org.apache.hadoop.hbase.KeyValueUtil;
40 import org.apache.hadoop.hbase.Tag;
41 import org.apache.hadoop.hbase.exceptions.DeserializationException;
42 import org.apache.hadoop.hbase.io.HeapSize;
43 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
44 import org.apache.hadoop.hbase.security.access.AccessControlConstants;
45 import org.apache.hadoop.hbase.security.access.Permission;
46 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
47 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
48 import org.apache.hadoop.hbase.util.Bytes;
49 import org.apache.hadoop.hbase.util.ClassSize;
50
51 import com.google.common.collect.ArrayListMultimap;
52 import com.google.common.collect.ListMultimap;
53 import com.google.common.collect.Lists;
54 import com.google.common.io.ByteArrayDataInput;
55 import com.google.common.io.ByteArrayDataOutput;
56 import com.google.common.io.ByteStreams;
57
58 @InterfaceAudience.Public
59 @InterfaceStability.Evolving
60 public abstract class Mutation extends OperationWithAttributes implements Row, CellScannable,
61 HeapSize {
62 public static final long MUTATION_OVERHEAD = ClassSize.align(
63
64 ClassSize.OBJECT +
65
66 2 * ClassSize.REFERENCE +
67
68 1 * Bytes.SIZEOF_LONG +
69
70 ClassSize.REFERENCE +
71
72 ClassSize.REFERENCE +
73
74 ClassSize.TREEMAP);
75
76
77
78
79 private static final String CONSUMED_CLUSTER_IDS = "_cs.id";
80
81
82
83
84 private static final String OP_ATTRIBUTE_TTL = "_ttl";
85
86 protected byte [] row = null;
87 protected long ts = HConstants.LATEST_TIMESTAMP;
88 protected Durability durability = Durability.USE_DEFAULT;
89
90
91 protected NavigableMap<byte [], List<Cell>> familyMap =
92 new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
93
94 @Override
95 public CellScanner cellScanner() {
96 return CellUtil.createCellScanner(getFamilyCellMap());
97 }
98
99
100
101
102
103
104
105
106 List<Cell> getCellList(byte[] family) {
107 List<Cell> list = this.familyMap.get(family);
108 if (list == null) {
109 list = new ArrayList<Cell>();
110 }
111 return list;
112 }
113
114
115
116
117
118
119 KeyValue createPutKeyValue(byte[] family, byte[] qualifier, long ts, byte[] value) {
120 return new KeyValue(this.row, family, qualifier, ts, KeyValue.Type.Put, value);
121 }
122
123
124
125
126
127
128
129
130
131
132 KeyValue createPutKeyValue(byte[] family, byte[] qualifier, long ts, byte[] value, Tag[] tags) {
133 KeyValue kvWithTag = new KeyValue(this.row, family, qualifier, ts, value, tags);
134 return kvWithTag;
135 }
136
137
138
139
140
141
142 KeyValue createPutKeyValue(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value,
143 Tag[] tags) {
144 return new KeyValue(this.row, 0, this.row == null ? 0 : this.row.length,
145 family, 0, family == null ? 0 : family.length,
146 qualifier, ts, KeyValue.Type.Put, value, tags != null ? Arrays.asList(tags) : null);
147 }
148
149
150
151
152
153
154
155 @Override
156 public Map<String, Object> getFingerprint() {
157 Map<String, Object> map = new HashMap<String, Object>();
158 List<String> families = new ArrayList<String>();
159
160
161 map.put("families", families);
162 for (Map.Entry<byte [], List<Cell>> entry : this.familyMap.entrySet()) {
163 families.add(Bytes.toStringBinary(entry.getKey()));
164 }
165 return map;
166 }
167
168
169
170
171
172
173
174
175 @Override
176 public Map<String, Object> toMap(int maxCols) {
177
178 Map<String, Object> map = getFingerprint();
179
180
181 Map<String, List<Map<String, Object>>> columns =
182 new HashMap<String, List<Map<String, Object>>>();
183 map.put("families", columns);
184 map.put("row", Bytes.toStringBinary(this.row));
185 int colCount = 0;
186
187 for (Map.Entry<byte [], List<Cell>> entry : this.familyMap.entrySet()) {
188
189 List<Map<String, Object>> qualifierDetails = new ArrayList<Map<String, Object>>();
190 columns.put(Bytes.toStringBinary(entry.getKey()), qualifierDetails);
191 colCount += entry.getValue().size();
192 if (maxCols <= 0) {
193 continue;
194 }
195
196 for (Cell cell: entry.getValue()) {
197 if (--maxCols <= 0 ) {
198 continue;
199 }
200 Map<String, Object> cellMap = cellToStringMap(cell);
201 qualifierDetails.add(cellMap);
202 }
203 }
204 map.put("totalColumns", colCount);
205
206 if (getId() != null) {
207 map.put("id", getId());
208 }
209
210
211
212 if (getTTL() != Long.MAX_VALUE) {
213 map.put("ttl", getTTL());
214 }
215 return map;
216 }
217
218 private static Map<String, Object> cellToStringMap(Cell c) {
219 Map<String, Object> stringMap = new HashMap<String, Object>();
220 stringMap.put("qualifier", Bytes.toStringBinary(c.getQualifierArray(), c.getQualifierOffset(),
221 c.getQualifierLength()));
222 stringMap.put("timestamp", c.getTimestamp());
223 stringMap.put("vlen", c.getValueLength());
224 List<Tag> tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
225 if (tags != null) {
226 List<String> tagsString = new ArrayList<String>();
227 for (Tag t : tags) {
228 tagsString.add((t.getType()) + ":" + Bytes.toStringBinary(t.getValue()));
229 }
230 stringMap.put("tag", tagsString);
231 }
232 return stringMap;
233 }
234
235
236
237
238
239 @Deprecated
240 public boolean getWriteToWAL() {
241 return this.durability != Durability.SKIP_WAL;
242 }
243
244
245
246
247
248
249
250
251 @Deprecated
252 public void setWriteToWAL(boolean write) {
253 setDurability(write ? Durability.USE_DEFAULT : Durability.SKIP_WAL);
254 }
255
256
257
258
259
260 public void setDurability(Durability d) {
261 this.durability = d;
262 }
263
264
265 public Durability getDurability() {
266 return this.durability;
267 }
268
269
270
271
272
273 public NavigableMap<byte [], List<Cell>> getFamilyCellMap() {
274 return this.familyMap;
275 }
276
277
278
279
280 public void setFamilyCellMap(NavigableMap<byte [], List<Cell>> map) {
281
282
283 this.familyMap = map;
284 }
285
286
287
288
289
290
291 @Deprecated
292 public NavigableMap<byte [], List<KeyValue>> getFamilyMap() {
293 TreeMap<byte[], List<KeyValue>> fm =
294 new TreeMap<byte[], List<KeyValue>>(Bytes.BYTES_COMPARATOR);
295 for (Map.Entry<byte[], List<Cell>> e : familyMap.entrySet()) {
296 List<KeyValue> kvl = new ArrayList<KeyValue>(e.getValue().size());
297 for (Cell c : e.getValue()) {
298 kvl.add(KeyValueUtil.ensureKeyValue(c));
299 }
300 fm.put(e.getKey(), kvl);
301 }
302 return fm;
303 }
304
305
306
307
308
309 @Deprecated
310 public void setFamilyMap(NavigableMap<byte [], List<KeyValue>> map) {
311 TreeMap<byte[], List<Cell>> fm = new TreeMap<byte[], List<Cell>>(Bytes.BYTES_COMPARATOR);
312 for (Map.Entry<byte[], List<KeyValue>> e : map.entrySet()) {
313 fm.put(e.getKey(), Lists.<Cell>newArrayList(e.getValue()));
314 }
315 this.familyMap = fm;
316 }
317
318
319
320
321
322 public boolean isEmpty() {
323 return familyMap.isEmpty();
324 }
325
326
327
328
329
330 @Override
331 public byte [] getRow() {
332 return this.row;
333 }
334
335 @Override
336 public int compareTo(final Row d) {
337 return Bytes.compareTo(this.getRow(), d.getRow());
338 }
339
340
341
342
343
344 public long getTimeStamp() {
345 return this.ts;
346 }
347
348
349
350
351
352 public void setClusterIds(List<UUID> clusterIds) {
353 ByteArrayDataOutput out = ByteStreams.newDataOutput();
354 out.writeInt(clusterIds.size());
355 for (UUID clusterId : clusterIds) {
356 out.writeLong(clusterId.getMostSignificantBits());
357 out.writeLong(clusterId.getLeastSignificantBits());
358 }
359 setAttribute(CONSUMED_CLUSTER_IDS, out.toByteArray());
360 }
361
362
363
364
365 public List<UUID> getClusterIds() {
366 List<UUID> clusterIds = new ArrayList<UUID>();
367 byte[] bytes = getAttribute(CONSUMED_CLUSTER_IDS);
368 if(bytes != null) {
369 ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
370 int numClusters = in.readInt();
371 for(int i=0; i<numClusters; i++){
372 clusterIds.add(new UUID(in.readLong(), in.readLong()));
373 }
374 }
375 return clusterIds;
376 }
377
378
379
380
381
382
383 public void setCellVisibility(CellVisibility expression) {
384 this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
385 .toCellVisibility(expression).toByteArray());
386 }
387
388
389
390
391
392 public CellVisibility getCellVisibility() throws DeserializationException {
393 byte[] cellVisibilityBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
394 if (cellVisibilityBytes == null) return null;
395 return ProtobufUtil.toCellVisibility(cellVisibilityBytes);
396 }
397
398
399
400
401
402 public int size() {
403 int size = 0;
404 for (List<Cell> cells : this.familyMap.values()) {
405 size += cells.size();
406 }
407 return size;
408 }
409
410
411
412
413 public int numFamilies() {
414 return familyMap.size();
415 }
416
417
418
419
420 @Override
421 public long heapSize() {
422 long heapsize = MUTATION_OVERHEAD;
423
424 heapsize += ClassSize.align(ClassSize.ARRAY + this.row.length);
425
426
427 heapsize +=
428 ClassSize.align(this.familyMap.size() * ClassSize.MAP_ENTRY);
429 for(Map.Entry<byte [], List<Cell>> entry : this.familyMap.entrySet()) {
430
431 heapsize +=
432 ClassSize.align(ClassSize.ARRAY + entry.getKey().length);
433
434
435
436
437 heapsize += ClassSize.align(ClassSize.ARRAYLIST);
438 int size = entry.getValue().size();
439 heapsize += ClassSize.align(ClassSize.ARRAY +
440 size * ClassSize.REFERENCE);
441
442 for(Cell cell : entry.getValue()) {
443 heapsize += CellUtil.estimatedHeapSizeOf(cell);
444 }
445 }
446 heapsize += getAttributeSize();
447 heapsize += extraHeapSize();
448 return ClassSize.align(heapsize);
449 }
450
451
452
453
454 public byte[] getACL() {
455 return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
456 }
457
458
459
460
461
462 public void setACL(String user, Permission perms) {
463 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
464 ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
465 }
466
467
468
469
470 public void setACL(Map<String, Permission> perms) {
471 ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
472 for (Map.Entry<String, Permission> entry : perms.entrySet()) {
473 permMap.put(entry.getKey(), entry.getValue());
474 }
475 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
476 ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
477 }
478
479
480
481
482
483
484 public long getTTL() {
485 byte[] ttlBytes = getAttribute(OP_ATTRIBUTE_TTL);
486 if (ttlBytes != null) {
487 return Bytes.toLong(ttlBytes);
488 }
489 return Long.MAX_VALUE;
490 }
491
492
493
494
495
496
497 public Mutation setTTL(long ttl) {
498 setAttribute(OP_ATTRIBUTE_TTL, Bytes.toBytes(ttl));
499 return this;
500 }
501
502
503
504
505
506 protected long extraHeapSize(){
507 return 0L;
508 }
509
510
511
512
513
514
515
516
517 static byte [] checkRow(final byte [] row) {
518 return checkRow(row, 0, row == null? 0: row.length);
519 }
520
521
522
523
524
525
526
527
528
529 static byte [] checkRow(final byte [] row, final int offset, final int length) {
530 if (row == null) {
531 throw new IllegalArgumentException("Row buffer is null");
532 }
533 if (length == 0) {
534 throw new IllegalArgumentException("Row length is 0");
535 }
536 if (length > HConstants.MAX_ROW_LENGTH) {
537 throw new IllegalArgumentException("Row length " + length + " is > " +
538 HConstants.MAX_ROW_LENGTH);
539 }
540 return row;
541 }
542
543 static void checkRow(ByteBuffer row) {
544 if (row == null) {
545 throw new IllegalArgumentException("Row buffer is null");
546 }
547 if (row.remaining() == 0) {
548 throw new IllegalArgumentException("Row length is 0");
549 }
550 if (row.remaining() > HConstants.MAX_ROW_LENGTH) {
551 throw new IllegalArgumentException("Row length " + row.remaining() + " is > " +
552 HConstants.MAX_ROW_LENGTH);
553 }
554 }
555 }