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 java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.classification.InterfaceStability;
31 import org.apache.hadoop.hbase.Cell;
32 import org.apache.hadoop.hbase.CellUtil;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.Tag;
36 import org.apache.hadoop.hbase.io.HeapSize;
37 import org.apache.hadoop.hbase.util.Bytes;
38
39
40
41
42
43
44
45
46 @InterfaceAudience.Public
47 @InterfaceStability.Stable
48 public class Put extends Mutation implements HeapSize, Comparable<Row> {
49
50
51
52
53 public Put(byte [] row) {
54 this(row, HConstants.LATEST_TIMESTAMP);
55 }
56
57
58
59
60
61
62
63 public Put(byte[] row, long ts) {
64 this(row, 0, row.length, ts);
65 }
66
67
68
69
70
71
72
73 public Put(byte [] rowArray, int rowOffset, int rowLength) {
74 this(rowArray, rowOffset, rowLength, HConstants.LATEST_TIMESTAMP);
75 }
76
77
78
79
80
81 public Put(ByteBuffer row, long ts) {
82 if (ts < 0) {
83 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
84 }
85 checkRow(row);
86 this.row = new byte[row.remaining()];
87 row.get(this.row);
88 this.ts = ts;
89 }
90
91
92
93
94 public Put(ByteBuffer row) {
95 this(row, HConstants.LATEST_TIMESTAMP);
96 }
97
98
99
100
101
102
103
104
105 public Put(byte [] rowArray, int rowOffset, int rowLength, long ts) {
106 checkRow(rowArray, rowOffset, rowLength);
107 this.row = Bytes.copy(rowArray, rowOffset, rowLength);
108 this.ts = ts;
109 if (ts < 0) {
110 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
111 }
112 }
113
114
115
116
117
118 public Put(Put putToCopy) {
119 this(putToCopy.getRow(), putToCopy.ts);
120 this.familyMap = new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
121 for(Map.Entry<byte [], List<Cell>> entry: putToCopy.getFamilyCellMap().entrySet()) {
122 this.familyMap.put(entry.getKey(), entry.getValue());
123 }
124 this.durability = putToCopy.durability;
125 for (Map.Entry<String, byte[]> entry : putToCopy.getAttributesMap().entrySet()) {
126 this.setAttribute(entry.getKey(), entry.getValue());
127 }
128 }
129
130
131
132
133
134
135
136
137 public Put add(byte [] family, byte [] qualifier, byte [] value) {
138 return add(family, qualifier, this.ts, value);
139 }
140
141
142
143
144
145
146 public Put addImmutable(byte [] family, byte [] qualifier, byte [] value) {
147 return addImmutable(family, qualifier, this.ts, value);
148 }
149
150
151
152
153
154 public Put addImmutable(byte[] family, byte [] qualifier, byte [] value, Tag[] tag) {
155 return addImmutable(family, qualifier, this.ts, value, tag);
156 }
157
158
159
160
161
162
163
164
165
166
167 public Put add(byte [] family, byte [] qualifier, long ts, byte [] value) {
168 if (ts < 0) {
169 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
170 }
171 List<Cell> list = getCellList(family);
172 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
173 list.add(kv);
174 familyMap.put(CellUtil.cloneFamily(kv), list);
175 return this;
176 }
177
178
179
180
181
182
183 public Put addImmutable(byte [] family, byte [] qualifier, long ts, byte [] value) {
184 if (ts < 0) {
185 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
186 }
187 List<Cell> list = getCellList(family);
188 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
189 list.add(kv);
190 familyMap.put(family, list);
191 return this;
192 }
193
194
195
196
197
198 @SuppressWarnings("unchecked")
199 public Put addImmutable(byte[] family, byte[] qualifier, long ts, byte[] value, Tag[] tag) {
200 List<Cell> list = getCellList(family);
201 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, tag);
202 list.add(kv);
203 familyMap.put(family, list);
204 return this;
205 }
206
207
208
209
210
211 public Put addImmutable(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value,
212 Tag[] tag) {
213 if (ts < 0) {
214 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
215 }
216 List<Cell> list = getCellList(family);
217 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, tag);
218 list.add(kv);
219 familyMap.put(family, list);
220 return this;
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public Put add(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) {
234 if (ts < 0) {
235 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
236 }
237 List<Cell> list = getCellList(family);
238 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, null);
239 list.add(kv);
240 familyMap.put(CellUtil.cloneFamily(kv), list);
241 return this;
242 }
243
244
245
246
247
248
249 public Put addImmutable(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) {
250 if (ts < 0) {
251 throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
252 }
253 List<Cell> list = getCellList(family);
254 KeyValue kv = createPutKeyValue(family, qualifier, ts, value, null);
255 list.add(kv);
256 familyMap.put(family, list);
257 return this;
258 }
259
260
261
262
263
264
265
266
267
268 public Put add(Cell kv) throws IOException{
269 byte [] family = CellUtil.cloneFamily(kv);
270 List<Cell> list = getCellList(family);
271
272 int res = Bytes.compareTo(this.row, 0, row.length,
273 kv.getRowArray(), kv.getRowOffset(), kv.getRowLength());
274 if (res != 0) {
275 throw new WrongRowIOException("The row in " + kv.toString() +
276 " doesn't match the original one " + Bytes.toStringBinary(this.row));
277 }
278 list.add(kv);
279 familyMap.put(family, list);
280 return this;
281 }
282
283
284
285
286
287
288
289
290
291
292
293 public boolean has(byte [] family, byte [] qualifier) {
294 return has(family, qualifier, this.ts, new byte[0], true, true);
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308 public boolean has(byte [] family, byte [] qualifier, long ts) {
309 return has(family, qualifier, ts, new byte[0], false, true);
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323 public boolean has(byte [] family, byte [] qualifier, byte [] value) {
324 return has(family, qualifier, this.ts, value, true, false);
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339 public boolean has(byte [] family, byte [] qualifier, long ts, byte [] value) {
340 return has(family, qualifier, ts, value, false, false);
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 private boolean has(byte[] family, byte[] qualifier, long ts, byte[] value,
358 boolean ignoreTS, boolean ignoreValue) {
359 List<Cell> list = getCellList(family);
360 if (list.size() == 0) {
361 return false;
362 }
363
364
365
366
367
368 if (!ignoreTS && !ignoreValue) {
369 for (Cell cell : list) {
370 if (CellUtil.matchingFamily(cell, family) &&
371 CellUtil.matchingQualifier(cell, qualifier) &&
372 CellUtil.matchingValue(cell, value) &&
373 cell.getTimestamp() == ts) {
374 return true;
375 }
376 }
377 } else if (ignoreValue && !ignoreTS) {
378 for (Cell cell : list) {
379 if (CellUtil.matchingFamily(cell, family) && CellUtil.matchingQualifier(cell, qualifier)
380 && cell.getTimestamp() == ts) {
381 return true;
382 }
383 }
384 } else if (!ignoreValue && ignoreTS) {
385 for (Cell cell : list) {
386 if (CellUtil.matchingFamily(cell, family) && CellUtil.matchingQualifier(cell, qualifier)
387 && CellUtil.matchingValue(cell, value)) {
388 return true;
389 }
390 }
391 } else {
392 for (Cell cell : list) {
393 if (CellUtil.matchingFamily(cell, family) &&
394 CellUtil.matchingQualifier(cell, qualifier)) {
395 return true;
396 }
397 }
398 }
399 return false;
400 }
401
402
403
404
405
406
407
408
409
410 public List<Cell> get(byte[] family, byte[] qualifier) {
411 List<Cell> filteredList = new ArrayList<Cell>();
412 for (Cell cell: getCellList(family)) {
413 if (CellUtil.matchingQualifier(cell, qualifier)) {
414 filteredList.add(cell);
415 }
416 }
417 return filteredList;
418 }
419 }