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 this.familyMap = get.getFamilyMap();
109 for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
110 setAttribute(attr.getKey(), attr.getValue());
111 }
112 }
113
114 public boolean isCheckExistenceOnly() {
115 return checkExistenceOnly;
116 }
117
118 public void setCheckExistenceOnly(boolean checkExistenceOnly) {
119 this.checkExistenceOnly = checkExistenceOnly;
120 }
121
122 public boolean isClosestRowBefore() {
123 return closestRowBefore;
124 }
125
126 public void setClosestRowBefore(boolean closestRowBefore) {
127 this.closestRowBefore = closestRowBefore;
128 }
129
130
131
132
133
134
135
136
137 public Get addFamily(byte [] family) {
138 familyMap.remove(family);
139 familyMap.put(family, null);
140 return this;
141 }
142
143
144
145
146
147
148
149
150
151 public Get addColumn(byte [] family, byte [] qualifier) {
152 NavigableSet<byte []> set = familyMap.get(family);
153 if(set == null) {
154 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
155 }
156 if (qualifier == null) {
157 qualifier = HConstants.EMPTY_BYTE_ARRAY;
158 }
159 set.add(qualifier);
160 familyMap.put(family, set);
161 return this;
162 }
163
164
165
166
167
168
169
170
171
172 public Get setTimeRange(long minStamp, long maxStamp)
173 throws IOException {
174 tr = new TimeRange(minStamp, maxStamp);
175 return this;
176 }
177
178
179
180
181
182
183 public Get setTimeStamp(long timestamp)
184 throws IOException {
185 try {
186 tr = new TimeRange(timestamp, timestamp+1);
187 } catch(IOException e) {
188
189 LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
190 throw e;
191 }
192 return this;
193 }
194
195
196
197
198
199 public Get setMaxVersions() {
200 this.maxVersions = Integer.MAX_VALUE;
201 return this;
202 }
203
204
205
206
207
208
209
210 public Get setMaxVersions(int maxVersions) throws IOException {
211 if(maxVersions <= 0) {
212 throw new IOException("maxVersions must be positive");
213 }
214 this.maxVersions = maxVersions;
215 return this;
216 }
217
218
219
220
221
222
223 public Get setMaxResultsPerColumnFamily(int limit) {
224 this.storeLimit = limit;
225 return this;
226 }
227
228
229
230
231
232
233
234 public Get setRowOffsetPerColumnFamily(int offset) {
235 this.storeOffset = offset;
236 return this;
237 }
238
239 @Override
240 public Get setFilter(Filter filter) {
241 super.setFilter(filter);
242 return this;
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public void setCacheBlocks(boolean cacheBlocks) {
258 this.cacheBlocks = cacheBlocks;
259 }
260
261
262
263
264
265
266 public boolean getCacheBlocks() {
267 return cacheBlocks;
268 }
269
270
271
272
273
274 public byte [] getRow() {
275 return this.row;
276 }
277
278
279
280
281
282 public int getMaxVersions() {
283 return this.maxVersions;
284 }
285
286
287
288
289
290
291 public int getMaxResultsPerColumnFamily() {
292 return this.storeLimit;
293 }
294
295
296
297
298
299
300 public int getRowOffsetPerColumnFamily() {
301 return this.storeOffset;
302 }
303
304
305
306
307
308 public TimeRange getTimeRange() {
309 return this.tr;
310 }
311
312
313
314
315
316 public Set<byte[]> familySet() {
317 return this.familyMap.keySet();
318 }
319
320
321
322
323
324 public int numFamilies() {
325 return this.familyMap.size();
326 }
327
328
329
330
331
332 public boolean hasFamilies() {
333 return !this.familyMap.isEmpty();
334 }
335
336
337
338
339
340 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
341 return this.familyMap;
342 }
343
344
345
346
347
348
349
350 @Override
351 public Map<String, Object> getFingerprint() {
352 Map<String, Object> map = new HashMap<String, Object>();
353 List<String> families = new ArrayList<String>();
354 map.put("families", families);
355 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
356 this.familyMap.entrySet()) {
357 families.add(Bytes.toStringBinary(entry.getKey()));
358 }
359 return map;
360 }
361
362
363
364
365
366
367
368
369 @Override
370 public Map<String, Object> toMap(int maxCols) {
371
372 Map<String, Object> map = getFingerprint();
373
374
375 Map<String, List<String>> columns = new HashMap<String, List<String>>();
376 map.put("families", columns);
377
378 map.put("row", Bytes.toStringBinary(this.row));
379 map.put("maxVersions", this.maxVersions);
380 map.put("cacheBlocks", this.cacheBlocks);
381 List<Long> timeRange = new ArrayList<Long>();
382 timeRange.add(this.tr.getMin());
383 timeRange.add(this.tr.getMax());
384 map.put("timeRange", timeRange);
385 int colCount = 0;
386
387 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
388 this.familyMap.entrySet()) {
389 List<String> familyList = new ArrayList<String>();
390 columns.put(Bytes.toStringBinary(entry.getKey()), familyList);
391 if(entry.getValue() == null) {
392 colCount++;
393 --maxCols;
394 familyList.add("ALL");
395 } else {
396 colCount += entry.getValue().size();
397 if (maxCols <= 0) {
398 continue;
399 }
400 for (byte [] column : entry.getValue()) {
401 if (--maxCols <= 0) {
402 continue;
403 }
404 familyList.add(Bytes.toStringBinary(column));
405 }
406 }
407 }
408 map.put("totalColumns", colCount);
409 if (this.filter != null) {
410 map.put("filter", this.filter.toString());
411 }
412
413 if (getId() != null) {
414 map.put("id", getId());
415 }
416 return map;
417 }
418
419
420 @Override
421 public int compareTo(Row other) {
422
423 return Bytes.compareTo(this.getRow(), other.getRow());
424 }
425
426 @Override
427 public int hashCode() {
428
429
430 return Bytes.hashCode(this.getRow());
431 }
432
433 @Override
434 public boolean equals(Object obj) {
435 if (this == obj) {
436 return true;
437 }
438 if (obj == null || getClass() != obj.getClass()) {
439 return false;
440 }
441 Row other = (Row) obj;
442
443 return compareTo(other) == 0;
444 }
445 }