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.io.IOException;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.NavigableMap;
25 import java.util.TreeMap;
26 import java.util.UUID;
27
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.Cell;
31 import org.apache.hadoop.hbase.CellUtil;
32 import org.apache.hadoop.hbase.KeyValue;
33 import org.apache.hadoop.hbase.io.TimeRange;
34 import org.apache.hadoop.hbase.security.access.Permission;
35 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.ClassSize;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @InterfaceAudience.Public
52 @InterfaceStability.Stable
53 public class Increment extends Mutation implements Comparable<Row> {
54 private static final long HEAP_OVERHEAD = ClassSize.REFERENCE + ClassSize.TIMERANGE;
55 private TimeRange tr = new TimeRange();
56
57
58
59
60
61
62
63 public Increment(byte [] row) {
64 this(row, 0, row.length);
65 }
66
67
68
69
70
71
72
73 public Increment(final byte [] row, final int offset, final int length) {
74 checkRow(row, offset, length);
75 this.row = Bytes.copy(row, offset, length);
76 }
77
78
79
80
81 public Increment(Increment i) {
82 this.row = i.getRow();
83 this.ts = i.getTimeStamp();
84 this.tr = i.getTimeRange();
85 this.familyMap.putAll(i.getFamilyCellMap());
86 for (Map.Entry<String, byte[]> entry : i.getAttributesMap().entrySet()) {
87 this.setAttribute(entry.getKey(), entry.getValue());
88 }
89 }
90
91
92
93
94
95
96
97 public Increment add(Cell cell) throws IOException{
98 byte [] family = CellUtil.cloneFamily(cell);
99 List<Cell> list = getCellList(family);
100
101 int res = Bytes.compareTo(this.row, 0, row.length,
102 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
103 if (res != 0) {
104 throw new WrongRowIOException("The row in " + cell +
105 " doesn't match the original one " + Bytes.toStringBinary(this.row));
106 }
107 list.add(cell);
108 familyMap.put(family, list);
109 return this;
110 }
111
112
113
114
115
116
117
118
119
120
121
122 public Increment addColumn(byte [] family, byte [] qualifier, long amount) {
123 if (family == null) {
124 throw new IllegalArgumentException("family cannot be null");
125 }
126 if (qualifier == null) {
127 throw new IllegalArgumentException("qualifier cannot be null");
128 }
129 List<Cell> list = getCellList(family);
130 KeyValue kv = createPutKeyValue(family, qualifier, ts, Bytes.toBytes(amount));
131 list.add(kv);
132 familyMap.put(CellUtil.cloneFamily(kv), list);
133 return this;
134 }
135
136
137
138
139
140 public TimeRange getTimeRange() {
141 return this.tr;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public Increment setTimeRange(long minStamp, long maxStamp)
159 throws IOException {
160 tr = new TimeRange(minStamp, maxStamp);
161 return this;
162 }
163
164
165
166
167
168
169 public Increment setReturnResults(boolean returnResults) {
170 super.setReturnResults(returnResults);
171 return this;
172 }
173
174
175
176
177
178 public boolean isReturnResults() {
179 return super.isReturnResults();
180 }
181
182
183
184
185
186 @Override
187 public int numFamilies() {
188 return this.familyMap.size();
189 }
190
191
192
193
194
195 public boolean hasFamilies() {
196 return !this.familyMap.isEmpty();
197 }
198
199
200
201
202
203
204
205
206
207 public Map<byte[], NavigableMap<byte [], Long>> getFamilyMapOfLongs() {
208 NavigableMap<byte[], List<Cell>> map = super.getFamilyCellMap();
209 Map<byte [], NavigableMap<byte[], Long>> results =
210 new TreeMap<byte[], NavigableMap<byte [], Long>>(Bytes.BYTES_COMPARATOR);
211 for (Map.Entry<byte [], List<Cell>> entry: map.entrySet()) {
212 NavigableMap<byte [], Long> longs = new TreeMap<byte [], Long>(Bytes.BYTES_COMPARATOR);
213 for (Cell cell: entry.getValue()) {
214 longs.put(CellUtil.cloneQualifier(cell),
215 Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
216 }
217 results.put(entry.getKey(), longs);
218 }
219 return results;
220 }
221
222
223
224
225 @Override
226 public String toString() {
227 StringBuilder sb = new StringBuilder();
228 sb.append("row=");
229 sb.append(Bytes.toStringBinary(this.row));
230 if(this.familyMap.size() == 0) {
231 sb.append(", no columns set to be incremented");
232 return sb.toString();
233 }
234 sb.append(", families=");
235 boolean moreThanOne = false;
236 for(Map.Entry<byte [], List<Cell>> entry: this.familyMap.entrySet()) {
237 if(moreThanOne) {
238 sb.append("), ");
239 } else {
240 moreThanOne = true;
241 sb.append("{");
242 }
243 sb.append("(family=");
244 sb.append(Bytes.toString(entry.getKey()));
245 sb.append(", columns=");
246 if(entry.getValue() == null) {
247 sb.append("NONE");
248 } else {
249 sb.append("{");
250 boolean moreThanOneB = false;
251 for(Cell cell : entry.getValue()) {
252 if(moreThanOneB) {
253 sb.append(", ");
254 } else {
255 moreThanOneB = true;
256 }
257 sb.append(CellUtil.getCellKeyAsString(cell) + "+=" +
258 Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
259 }
260 sb.append("}");
261 }
262 }
263 sb.append("}");
264 return sb.toString();
265 }
266
267 @Override
268 public int compareTo(Row i) {
269
270 return Bytes.compareTo(this.getRow(), i.getRow());
271 }
272
273 @Override
274 public int hashCode() {
275
276
277 return Bytes.hashCode(this.getRow());
278 }
279
280 @Override
281 public boolean equals(Object obj) {
282
283 if (this == obj) {
284 return true;
285 }
286 if (obj == null || getClass() != obj.getClass()) {
287 return false;
288 }
289 Row other = (Row) obj;
290 return compareTo(other) == 0;
291 }
292
293 @Override
294 protected long extraHeapSize(){
295 return HEAP_OVERHEAD;
296 }
297
298 @Override
299 public Increment setAttribute(String name, byte[] value) {
300 return (Increment) super.setAttribute(name, value);
301 }
302
303 @Override
304 public Increment setId(String id) {
305 return (Increment) super.setId(id);
306 }
307
308 @Override
309 @Deprecated
310 public Increment setWriteToWAL(boolean write) {
311 return (Increment) super.setWriteToWAL(write);
312 }
313
314 @Override
315 public Increment setDurability(Durability d) {
316 return (Increment) super.setDurability(d);
317 }
318
319 @Override
320 public Increment setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
321 return (Increment) super.setFamilyCellMap(map);
322 }
323
324 @Override
325 @Deprecated
326 public Increment setFamilyMap(NavigableMap<byte[], List<KeyValue>> map) {
327 return (Increment) super.setFamilyMap(map);
328 }
329
330 @Override
331 public Increment setClusterIds(List<UUID> clusterIds) {
332 return (Increment) super.setClusterIds(clusterIds);
333 }
334
335 @Override
336 public Increment setCellVisibility(CellVisibility expression) {
337 return (Increment) super.setCellVisibility(expression);
338 }
339
340 @Override
341 public Increment setACL(String user, Permission perms) {
342 return (Increment) super.setACL(user, perms);
343 }
344
345 @Override
346 public Increment setACL(Map<String, Permission> perms) {
347 return (Increment) super.setACL(perms);
348 }
349
350 @Override
351 public Increment setTTL(long ttl) {
352 return (Increment) super.setTTL(ttl);
353 }
354 }