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 org.apache.hadoop.hbase.HConstants;
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.filter.Filter;
25 import org.apache.hadoop.hbase.io.TimeRange;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hadoop.hbase.util.Classes;
28 import org.apache.hadoop.io.Writable;
29
30 import java.io.DataInput;
31 import java.io.DataOutput;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.NavigableSet;
38 import java.util.Set;
39 import java.util.TreeMap;
40 import java.util.TreeSet;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class Get extends OperationWithAttributes
67 implements Writable, Row, Comparable<Row> {
68 private static final byte GET_VERSION = (byte)2;
69
70 private byte [] row = null;
71 private long lockId = -1L;
72 private int maxVersions = 1;
73 private boolean cacheBlocks = true;
74 private Filter filter = null;
75 private TimeRange tr = new TimeRange();
76 private Map<byte [], NavigableSet<byte []>> familyMap =
77 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
78
79
80 public Get() {}
81
82
83
84
85
86
87
88
89 public Get(byte [] row) {
90 this(row, null);
91 }
92
93
94
95
96
97
98
99
100
101
102 public Get(byte [] row, RowLock rowLock) {
103 this.row = row;
104 if(rowLock != null) {
105 this.lockId = rowLock.getLockId();
106 }
107 }
108
109
110
111
112
113
114
115
116 public Get addFamily(byte [] family) {
117 familyMap.remove(family);
118 familyMap.put(family, null);
119 return this;
120 }
121
122
123
124
125
126
127
128
129
130 public Get addColumn(byte [] family, byte [] qualifier) {
131 NavigableSet<byte []> set = familyMap.get(family);
132 if(set == null) {
133 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
134 }
135 if (qualifier == null) {
136 qualifier = HConstants.EMPTY_BYTE_ARRAY;
137 }
138 set.add(qualifier);
139 familyMap.put(family, set);
140 return this;
141 }
142
143
144
145
146
147
148
149
150
151 public Get setTimeRange(long minStamp, long maxStamp)
152 throws IOException {
153 tr = new TimeRange(minStamp, maxStamp);
154 return this;
155 }
156
157
158
159
160
161
162 public Get setTimeStamp(long timestamp) {
163 try {
164 tr = new TimeRange(timestamp, timestamp+1);
165 } catch(IOException e) {
166
167 }
168 return this;
169 }
170
171
172
173
174
175 public Get setMaxVersions() {
176 this.maxVersions = Integer.MAX_VALUE;
177 return this;
178 }
179
180
181
182
183
184
185
186 public Get setMaxVersions(int maxVersions) throws IOException {
187 if(maxVersions <= 0) {
188 throw new IOException("maxVersions must be positive");
189 }
190 this.maxVersions = maxVersions;
191 return this;
192 }
193
194
195
196
197
198
199
200
201 public Get setFilter(Filter filter) {
202 this.filter = filter;
203 return this;
204 }
205
206
207
208
209
210
211 public Filter getFilter() {
212 return this.filter;
213 }
214
215
216
217
218
219
220
221
222
223
224
225 public void setCacheBlocks(boolean cacheBlocks) {
226 this.cacheBlocks = cacheBlocks;
227 }
228
229
230
231
232
233
234 public boolean getCacheBlocks() {
235 return cacheBlocks;
236 }
237
238
239
240
241
242 public byte [] getRow() {
243 return this.row;
244 }
245
246
247
248
249
250 @SuppressWarnings("deprecation")
251 public RowLock getRowLock() {
252 return new RowLock(this.row, this.lockId);
253 }
254
255
256
257
258
259 public long getLockId() {
260 return this.lockId;
261 }
262
263
264
265
266
267 public int getMaxVersions() {
268 return this.maxVersions;
269 }
270
271
272
273
274
275 public TimeRange getTimeRange() {
276 return this.tr;
277 }
278
279
280
281
282
283 public Set<byte[]> familySet() {
284 return this.familyMap.keySet();
285 }
286
287
288
289
290
291 public int numFamilies() {
292 return this.familyMap.size();
293 }
294
295
296
297
298
299 public boolean hasFamilies() {
300 return !this.familyMap.isEmpty();
301 }
302
303
304
305
306
307 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
308 return this.familyMap;
309 }
310
311
312
313
314
315
316
317 @Override
318 public Map<String, Object> getFingerprint() {
319 Map<String, Object> map = new HashMap<String, Object>();
320 List<String> families = new ArrayList<String>();
321 map.put("families", families);
322 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
323 this.familyMap.entrySet()) {
324 families.add(Bytes.toStringBinary(entry.getKey()));
325 }
326 return map;
327 }
328
329
330
331
332
333
334
335
336 @Override
337 public Map<String, Object> toMap(int maxCols) {
338
339 Map<String, Object> map = getFingerprint();
340
341
342 Map<String, List<String>> columns = new HashMap<String, List<String>>();
343 map.put("families", columns);
344
345 map.put("row", Bytes.toStringBinary(this.row));
346 map.put("maxVersions", this.maxVersions);
347 map.put("cacheBlocks", this.cacheBlocks);
348 List<Long> timeRange = new ArrayList<Long>();
349 timeRange.add(this.tr.getMin());
350 timeRange.add(this.tr.getMax());
351 map.put("timeRange", timeRange);
352 int colCount = 0;
353
354 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
355 this.familyMap.entrySet()) {
356 List<String> familyList = new ArrayList<String>();
357 columns.put(Bytes.toStringBinary(entry.getKey()), familyList);
358 if(entry.getValue() == null) {
359 colCount++;
360 --maxCols;
361 familyList.add("ALL");
362 } else {
363 colCount += entry.getValue().size();
364 if (maxCols <= 0) {
365 continue;
366 }
367 for (byte [] column : entry.getValue()) {
368 if (--maxCols <= 0) {
369 continue;
370 }
371 familyList.add(Bytes.toStringBinary(column));
372 }
373 }
374 }
375 map.put("totalColumns", colCount);
376 if (this.filter != null) {
377 map.put("filter", this.filter.toString());
378 }
379
380 if (getId() != null) {
381 map.put("id", getId());
382 }
383 return map;
384 }
385
386
387 public int compareTo(Row other) {
388 return Bytes.compareTo(this.getRow(), other.getRow());
389 }
390
391
392 public void readFields(final DataInput in)
393 throws IOException {
394 int version = in.readByte();
395 if (version > GET_VERSION) {
396 throw new IOException("unsupported version");
397 }
398 this.row = Bytes.readByteArray(in);
399 this.lockId = in.readLong();
400 this.maxVersions = in.readInt();
401 boolean hasFilter = in.readBoolean();
402 if (hasFilter) {
403 this.filter = Classes.createWritableForName(
404 Bytes.toString(Bytes.readByteArray(in)));
405 this.filter.readFields(in);
406 }
407 this.cacheBlocks = in.readBoolean();
408 this.tr = new TimeRange();
409 tr.readFields(in);
410 int numFamilies = in.readInt();
411 this.familyMap =
412 new TreeMap<byte [],NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
413 for(int i=0; i<numFamilies; i++) {
414 byte [] family = Bytes.readByteArray(in);
415 boolean hasColumns = in.readBoolean();
416 NavigableSet<byte []> set = null;
417 if(hasColumns) {
418 int numColumns = in.readInt();
419 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
420 for(int j=0; j<numColumns; j++) {
421 byte [] qualifier = Bytes.readByteArray(in);
422 set.add(qualifier);
423 }
424 }
425 this.familyMap.put(family, set);
426 }
427 readAttributes(in);
428 }
429
430 public void write(final DataOutput out)
431 throws IOException {
432 out.writeByte(GET_VERSION);
433 Bytes.writeByteArray(out, this.row);
434 out.writeLong(this.lockId);
435 out.writeInt(this.maxVersions);
436 if(this.filter == null) {
437 out.writeBoolean(false);
438 } else {
439 out.writeBoolean(true);
440 Bytes.writeByteArray(out, Bytes.toBytes(filter.getClass().getName()));
441 filter.write(out);
442 }
443 out.writeBoolean(this.cacheBlocks);
444 tr.write(out);
445 out.writeInt(familyMap.size());
446 for(Map.Entry<byte [], NavigableSet<byte []>> entry :
447 familyMap.entrySet()) {
448 Bytes.writeByteArray(out, entry.getKey());
449 NavigableSet<byte []> columnSet = entry.getValue();
450 if(columnSet == null) {
451 out.writeBoolean(false);
452 } else {
453 out.writeBoolean(true);
454 out.writeInt(columnSet.size());
455 for(byte [] qualifier : columnSet) {
456 Bytes.writeByteArray(out, qualifier);
457 }
458 }
459 }
460 writeAttributes(out);
461 }
462 }