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 implements Row {
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
106 if (getId() != null) {
107 map.put("id", getId());
108 }
109 return map;
110 }
111
112
113
114
115 public boolean getWriteToWAL() {
116 return this.writeToWAL;
117 }
118
119
120
121
122
123
124 public void setWriteToWAL(boolean write) {
125 this.writeToWAL = write;
126 }
127
128
129
130
131
132 public Map<byte [], List<KeyValue>> getFamilyMap() {
133 return this.familyMap;
134 }
135
136
137
138
139 public void setFamilyMap(Map<byte [], List<KeyValue>> map) {
140 this.familyMap = map;
141 }
142
143
144
145
146
147 public boolean isEmpty() {
148 return familyMap.isEmpty();
149 }
150
151
152
153
154
155 @Override
156 public byte [] getRow() {
157 return this.row;
158 }
159
160 public int compareTo(final Row d) {
161 return Bytes.compareTo(this.getRow(), d.getRow());
162 }
163
164
165
166
167
168
169 public RowLock getRowLock() {
170 return new RowLock(this.row, this.lockId);
171 }
172
173
174
175
176
177
178
179 public long getLockId() {
180 return this.lockId;
181 }
182
183
184
185
186
187 public long getTimeStamp() {
188 return this.ts;
189 }
190
191
192
193
194
195 public void setClusterId(UUID clusterId) {
196 if (clusterId == null) return;
197 byte[] val = new byte[2*Bytes.SIZEOF_LONG];
198 Bytes.putLong(val, 0, clusterId.getMostSignificantBits());
199 Bytes.putLong(val, Bytes.SIZEOF_LONG, clusterId.getLeastSignificantBits());
200 setAttribute(CLUSTER_ID_ATTR, val);
201 }
202
203
204
205
206 public UUID getClusterId() {
207 byte[] attr = getAttribute(CLUSTER_ID_ATTR);
208 if (attr == null) {
209 return HConstants.DEFAULT_CLUSTER_ID;
210 }
211 return new UUID(Bytes.toLong(attr,0), Bytes.toLong(attr, Bytes.SIZEOF_LONG));
212 }
213
214
215
216
217 public int size() {
218 int size = 0;
219 for(List<KeyValue> kvList : this.familyMap.values()) {
220 size += kvList.size();
221 }
222 return size;
223 }
224
225
226
227
228 public int numFamilies() {
229 return familyMap.size();
230 }
231 }