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