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 org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.apache.hadoop.hbase.util.ClassSize;
26
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 @InterfaceAudience.Public
32 @InterfaceStability.Evolving
33 public abstract class OperationWithAttributes extends Operation implements Attributes {
34
35 private Map<String, byte[]> attributes;
36
37
38 public static final String ID_ATRIBUTE = "_operation.attributes.id";
39
40 @Override
41 public void setAttribute(String name, byte[] value) {
42 if (attributes == null && value == null) {
43 return;
44 }
45
46 if (attributes == null) {
47 attributes = new HashMap<String, byte[]>();
48 }
49
50 if (value == null) {
51 attributes.remove(name);
52 if (attributes.isEmpty()) {
53 this.attributes = null;
54 }
55 } else {
56 attributes.put(name, value);
57 }
58 }
59
60 @Override
61 public byte[] getAttribute(String name) {
62 if (attributes == null) {
63 return null;
64 }
65
66 return attributes.get(name);
67 }
68
69 @Override
70 public Map<String, byte[]> getAttributesMap() {
71 if (attributes == null) {
72 return Collections.emptyMap();
73 }
74 return Collections.unmodifiableMap(attributes);
75 }
76
77 protected long getAttributeSize() {
78 long size = 0;
79 if (attributes != null) {
80 size += ClassSize.align(this.attributes.size() * ClassSize.MAP_ENTRY);
81 for(Map.Entry<String, byte[]> entry : this.attributes.entrySet()) {
82 size += ClassSize.align(ClassSize.STRING + entry.getKey().length());
83 size += ClassSize.align(ClassSize.ARRAY + entry.getValue().length);
84 }
85 }
86 return size;
87 }
88
89
90
91
92
93
94
95
96
97
98 public void setId(String id) {
99 setAttribute(ID_ATRIBUTE, Bytes.toBytes(id));
100 }
101
102
103
104
105
106
107 public String getId() {
108 byte[] attr = getAttribute(ID_ATRIBUTE);
109 return attr == null? null: Bytes.toString(attr);
110 }
111 }