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