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 public boolean isCheckExistenceOnly() {
94 return checkExistenceOnly;
95 }
96
97 public void setCheckExistenceOnly(boolean checkExistenceOnly) {
98 this.checkExistenceOnly = checkExistenceOnly;
99 }
100
101 public boolean isClosestRowBefore() {
102 return closestRowBefore;
103 }
104
105 public void setClosestRowBefore(boolean closestRowBefore) {
106 this.closestRowBefore = closestRowBefore;
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 throws IOException {
164 try {
165 tr = new TimeRange(timestamp, timestamp+1);
166 } catch(IOException e) {
167
168 LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
169 throw e;
170 }
171 return this;
172 }
173
174
175
176
177
178 public Get setMaxVersions() {
179 this.maxVersions = Integer.MAX_VALUE;
180 return this;
181 }
182
183
184
185
186
187
188
189 public Get setMaxVersions(int maxVersions) throws IOException {
190 if(maxVersions <= 0) {
191 throw new IOException("maxVersions must be positive");
192 }
193 this.maxVersions = maxVersions;
194 return this;
195 }
196
197
198
199
200
201
202 public Get setMaxResultsPerColumnFamily(int limit) {
203 this.storeLimit = limit;
204 return this;
205 }
206
207
208
209
210
211
212
213 public Get setRowOffsetPerColumnFamily(int offset) {
214 this.storeOffset = offset;
215 return this;
216 }
217
218 @Override
219 public Get setFilter(Filter filter) {
220 super.setFilter(filter);
221 return this;
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public void setCacheBlocks(boolean cacheBlocks) {
237 this.cacheBlocks = cacheBlocks;
238 }
239
240
241
242
243
244
245 public boolean getCacheBlocks() {
246 return cacheBlocks;
247 }
248
249
250
251
252
253 public byte [] getRow() {
254 return this.row;
255 }
256
257
258
259
260
261 public int getMaxVersions() {
262 return this.maxVersions;
263 }
264
265
266
267
268
269
270 public int getMaxResultsPerColumnFamily() {
271 return this.storeLimit;
272 }
273
274
275
276
277
278
279 public int getRowOffsetPerColumnFamily() {
280 return this.storeOffset;
281 }
282
283
284
285
286
287 public TimeRange getTimeRange() {
288 return this.tr;
289 }
290
291
292
293
294
295 public Set<byte[]> familySet() {
296 return this.familyMap.keySet();
297 }
298
299
300
301
302
303 public int numFamilies() {
304 return this.familyMap.size();
305 }
306
307
308
309
310
311 public boolean hasFamilies() {
312 return !this.familyMap.isEmpty();
313 }
314
315
316
317
318
319 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
320 return this.familyMap;
321 }
322
323
324
325
326
327
328
329 @Override
330 public Map<String, Object> getFingerprint() {
331 Map<String, Object> map = new HashMap<String, Object>();
332 List<String> families = new ArrayList<String>();
333 map.put("families", families);
334 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
335 this.familyMap.entrySet()) {
336 families.add(Bytes.toStringBinary(entry.getKey()));
337 }
338 return map;
339 }
340
341
342
343
344
345
346
347
348 @Override
349 public Map<String, Object> toMap(int maxCols) {
350
351 Map<String, Object> map = getFingerprint();
352
353
354 Map<String, List<String>> columns = new HashMap<String, List<String>>();
355 map.put("families", columns);
356
357 map.put("row", Bytes.toStringBinary(this.row));
358 map.put("maxVersions", this.maxVersions);
359 map.put("cacheBlocks", this.cacheBlocks);
360 List<Long> timeRange = new ArrayList<Long>();
361 timeRange.add(this.tr.getMin());
362 timeRange.add(this.tr.getMax());
363 map.put("timeRange", timeRange);
364 int colCount = 0;
365
366 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
367 this.familyMap.entrySet()) {
368 List<String> familyList = new ArrayList<String>();
369 columns.put(Bytes.toStringBinary(entry.getKey()), familyList);
370 if(entry.getValue() == null) {
371 colCount++;
372 --maxCols;
373 familyList.add("ALL");
374 } else {
375 colCount += entry.getValue().size();
376 if (maxCols <= 0) {
377 continue;
378 }
379 for (byte [] column : entry.getValue()) {
380 if (--maxCols <= 0) {
381 continue;
382 }
383 familyList.add(Bytes.toStringBinary(column));
384 }
385 }
386 }
387 map.put("totalColumns", colCount);
388 if (this.filter != null) {
389 map.put("filter", this.filter.toString());
390 }
391
392 if (getId() != null) {
393 map.put("id", getId());
394 }
395 return map;
396 }
397
398
399 @Override
400 public int compareTo(Row other) {
401
402 return Bytes.compareTo(this.getRow(), other.getRow());
403 }
404
405 @Override
406 public int hashCode() {
407
408
409 return Bytes.hashCode(this.getRow());
410 }
411
412 @Override
413 public boolean equals(Object obj) {
414 if (this == obj) {
415 return true;
416 }
417 if (obj == null || getClass() != obj.getClass()) {
418 return false;
419 }
420 Row other = (Row) obj;
421
422 return compareTo(other) == 0;
423 }
424 }