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