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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.UUID;
29
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.util.Bytes;
33
34 public abstract class Mutation extends OperationWithAttributes {
35
36 private static final String CLUSTER_ID_ATTR = "_c.id_";
37
38 protected byte [] row = null;
39 protected long ts = HConstants.LATEST_TIMESTAMP;
40 protected long lockId = -1L;
41 protected boolean writeToWAL = true;
42 protected Map<byte [], List<KeyValue>> familyMap =
43 new TreeMap<byte [], List<KeyValue>>(Bytes.BYTES_COMPARATOR);
44
45
46
47
48
49
50
51 @Override
52 public Map<String, Object> getFingerprint() {
53 Map<String, Object> map = new HashMap<String, Object>();
54 List<String> families = new ArrayList<String>();
55
56
57 map.put("families", families);
58 for (Map.Entry<byte [], List<KeyValue>> entry : this.familyMap.entrySet()) {
59 families.add(Bytes.toStringBinary(entry.getKey()));
60 }
61 return map;
62 }
63
64
65
66
67
68
69
70
71 @Override
72 public Map<String, Object> toMap(int maxCols) {
73
74 Map<String, Object> map = getFingerprint();
75
76
77 Map<String, List<Map<String, Object>>> columns =
78 new HashMap<String, List<Map<String, Object>>>();
79 map.put("families", columns);
80 map.put("row", Bytes.toStringBinary(this.row));
81 int colCount = 0;
82
83 for (Map.Entry<byte [], List<KeyValue>> entry : this.familyMap.entrySet()) {
84
85 List<Map<String, Object>> qualifierDetails =
86 new ArrayList<Map<String, Object>>();
87 columns.put(Bytes.toStringBinary(entry.getKey()), qualifierDetails);
88 colCount += entry.getValue().size();
89 if (maxCols <= 0) {
90 continue;
91 }
92
93 for (KeyValue kv : entry.getValue()) {
94 if (--maxCols <= 0 ) {
95 continue;
96 }
97 Map<String, Object> kvMap = kv.toStringMap();
98
99 kvMap.remove("row");
100 kvMap.remove("family");
101 qualifierDetails.add(kvMap);
102 }
103 }
104 map.put("totalColumns", colCount);
105 return map;
106 }
107
108
109
110
111 public boolean getWriteToWAL() {
112 return this.writeToWAL;
113 }
114
115
116
117
118
119
120 public void setWriteToWAL(boolean write) {
121 this.writeToWAL = write;
122 }
123
124
125
126
127
128 public Map<byte [], List<KeyValue>> getFamilyMap() {
129 return this.familyMap;
130 }
131
132
133
134
135 public void setFamilyMap(Map<byte [], List<KeyValue>> map) {
136 this.familyMap = map;
137 }
138
139
140
141
142
143 public boolean isEmpty() {
144 return familyMap.isEmpty();
145 }
146
147
148
149
150
151 public byte [] getRow() {
152 return this.row;
153 }
154
155 public int compareTo(final Row d) {
156 return Bytes.compareTo(this.getRow(), d.getRow());
157 }
158
159
160
161
162
163 public RowLock getRowLock() {
164 return new RowLock(this.row, this.lockId);
165 }
166
167
168
169
170
171
172 public long getLockId() {
173 return this.lockId;
174 }
175
176
177
178
179
180 public long getTimeStamp() {
181 return this.ts;
182 }
183
184
185
186
187
188 public void setClusterId(UUID clusterId) {
189 byte[] val = new byte[2*Bytes.SIZEOF_LONG];
190 Bytes.putLong(val, 0, clusterId.getMostSignificantBits());
191 Bytes.putLong(val, Bytes.SIZEOF_LONG, clusterId.getLeastSignificantBits());
192 setAttribute(CLUSTER_ID_ATTR, val);
193 }
194
195
196
197
198 public UUID getClusterId() {
199 byte[] attr = getAttribute(CLUSTER_ID_ATTR);
200 if (attr == null) {
201 return HConstants.DEFAULT_CLUSTER_ID;
202 }
203 return new UUID(Bytes.toLong(attr,0), Bytes.toLong(attr, Bytes.SIZEOF_LONG));
204 }
205 }