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