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.util.ArrayList;
24 import java.util.Arrays;
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.HConstants;
33 import org.apache.hadoop.hbase.KeyValue;
34 import org.apache.hadoop.hbase.KeyValueUtil;
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
73 public Put(byte [] rowArray, int rowOffset, int rowLength, long ts) {
74 checkRow(rowArray, rowOffset, rowLength);
75 this.row = Bytes.copy(rowArray, rowOffset, rowLength);
76 this.ts = ts;
77 }
78
79
80
81
82
83 public Put(Put putToCopy) {
84 this(putToCopy.getRow(), putToCopy.ts);
85 this.familyMap = new TreeMap<byte [], List<? extends Cell>>(Bytes.BYTES_COMPARATOR);
86 for(Map.Entry<byte [], List<? extends Cell>> entry: putToCopy.getFamilyMap().entrySet()) {
87 this.familyMap.put(entry.getKey(), entry.getValue());
88 }
89 this.writeToWAL = putToCopy.writeToWAL;
90 }
91
92
93
94
95
96
97
98
99 public Put add(byte [] family, byte [] qualifier, byte [] value) {
100 return add(family, qualifier, this.ts, value);
101 }
102
103
104
105
106
107
108
109
110
111
112 @SuppressWarnings("unchecked")
113 public Put add(byte [] family, byte [] qualifier, long ts, byte [] value) {
114 List<? extends Cell> list = getCellList(family);
115 KeyValue kv = createPutKeyValue(family, qualifier, ts, value);
116 ((List<KeyValue>)list).add(kv);
117 familyMap.put(kv.getFamily(), list);
118 return this;
119 }
120
121
122
123
124
125
126
127
128
129 @SuppressWarnings("unchecked")
130 public Put add(KeyValue kv) throws IOException{
131 byte [] family = kv.getFamily();
132 List<? extends Cell> list = getCellList(family);
133
134 int res = Bytes.compareTo(this.row, 0, row.length,
135 kv.getBuffer(), kv.getRowOffset(), kv.getRowLength());
136 if (res != 0) {
137 throw new WrongRowIOException("The row in " + kv.toString() +
138 " doesn't match the original one " + Bytes.toStringBinary(this.row));
139 }
140 ((List<KeyValue>)list).add(kv);
141 familyMap.put(family, list);
142 return this;
143 }
144
145
146
147
148
149
150
151
152
153
154
155 public boolean has(byte [] family, byte [] qualifier) {
156 return has(family, qualifier, this.ts, new byte[0], true, true);
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170 public boolean has(byte [] family, byte [] qualifier, long ts) {
171 return has(family, qualifier, ts, new byte[0], false, true);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public boolean has(byte [] family, byte [] qualifier, byte [] value) {
186 return has(family, qualifier, this.ts, value, true, false);
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public boolean has(byte [] family, byte [] qualifier, long ts, byte [] value) {
202 return has(family, qualifier, ts, value, false, false);
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 private boolean has(byte[] family, byte[] qualifier, long ts, byte[] value,
220 boolean ignoreTS, boolean ignoreValue) {
221 List<? extends Cell> list = getCellList(family);
222 if (list.size() == 0) {
223 return false;
224 }
225
226
227
228
229
230 if (!ignoreTS && !ignoreValue) {
231 for (Cell cell : list) {
232 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
233 if (Arrays.equals(kv.getFamily(), family) &&
234 Arrays.equals(kv.getQualifier(), qualifier) &&
235 Arrays.equals(kv.getValue(), value) &&
236 kv.getTimestamp() == ts) {
237 return true;
238 }
239 }
240 } else if (ignoreValue && !ignoreTS) {
241 for (Cell cell : list) {
242 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
243 if (Arrays.equals(kv.getFamily(), family) && Arrays.equals(kv.getQualifier(), qualifier)
244 && kv.getTimestamp() == ts) {
245 return true;
246 }
247 }
248 } else if (!ignoreValue && ignoreTS) {
249 for (Cell cell : list) {
250 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
251 if (Arrays.equals(kv.getFamily(), family) && Arrays.equals(kv.getQualifier(), qualifier)
252 && Arrays.equals(kv.getValue(), value)) {
253 return true;
254 }
255 }
256 } else {
257 for (Cell cell : list) {
258 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
259 if (Arrays.equals(kv.getFamily(), family) &&
260 Arrays.equals(kv.getQualifier(), qualifier)) {
261 return true;
262 }
263 }
264 }
265 return false;
266 }
267
268
269
270
271
272
273
274
275
276 public List<KeyValue> get(byte[] family, byte[] qualifier) {
277 List<KeyValue> filteredList = new ArrayList<KeyValue>();
278 for (Cell cell: getCellList(family)) {
279 KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
280 if (Arrays.equals(kv.getQualifier(), qualifier)) {
281 filteredList.add(kv);
282 }
283 }
284 return filteredList;
285 }
286 }