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