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.conf.Configuration;
23 import org.apache.hadoop.hbase.HConstants;
24 import org.apache.hadoop.hbase.KeyValue;
25 import org.apache.hadoop.hbase.filter.Filter;
26 import org.apache.hadoop.hbase.io.TimeRange;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.apache.hadoop.io.Writable;
29 import org.apache.hadoop.io.WritableFactories;
30
31 import java.io.DataInput;
32 import java.io.DataOutput;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.NavigableSet;
39 import java.util.Set;
40 import java.util.TreeMap;
41 import java.util.TreeSet;
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
67 public class Get extends OperationWithAttributes
68 implements Writable, Row, Comparable<Row> {
69 private static final byte GET_VERSION = (byte)2;
70
71 private byte [] row = null;
72 private long lockId = -1L;
73 private int maxVersions = 1;
74 private boolean cacheBlocks = true;
75 private Filter filter = null;
76 private TimeRange tr = new TimeRange();
77 private Map<byte [], NavigableSet<byte []>> familyMap =
78 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
79
80
81 public Get() {}
82
83
84
85
86
87
88
89
90 public Get(byte [] row) {
91 this(row, null);
92 }
93
94
95
96
97
98
99
100
101
102
103 public Get(byte [] row, RowLock rowLock) {
104 this.row = row;
105 if(rowLock != null) {
106 this.lockId = rowLock.getLockId();
107 }
108 }
109
110
111
112
113
114
115
116
117 public Get addFamily(byte [] family) {
118 familyMap.remove(family);
119 familyMap.put(family, null);
120 return this;
121 }
122
123
124
125
126
127
128
129
130
131 public Get addColumn(byte [] family, byte [] qualifier) {
132 NavigableSet<byte []> set = familyMap.get(family);
133 if(set == null) {
134 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
135 }
136 if (qualifier == null) {
137 qualifier = HConstants.EMPTY_BYTE_ARRAY;
138 }
139 set.add(qualifier);
140 familyMap.put(family, set);
141 return this;
142 }
143
144
145
146
147
148
149
150
151
152 public Get setTimeRange(long minStamp, long maxStamp)
153 throws IOException {
154 tr = new TimeRange(minStamp, maxStamp);
155 return this;
156 }
157
158
159
160
161
162
163 public Get setTimeStamp(long timestamp) {
164 try {
165 tr = new TimeRange(timestamp, timestamp+1);
166 } catch(IOException e) {
167
168 }
169 return this;
170 }
171
172
173
174
175
176 public Get setMaxVersions() {
177 this.maxVersions = Integer.MAX_VALUE;
178 return this;
179 }
180
181
182
183
184
185
186
187 public Get setMaxVersions(int maxVersions) throws IOException {
188 if(maxVersions <= 0) {
189 throw new IOException("maxVersions must be positive");
190 }
191 this.maxVersions = maxVersions;
192 return this;
193 }
194
195
196
197
198
199
200
201
202 public Get setFilter(Filter filter) {
203 this.filter = filter;
204 return this;
205 }
206
207
208
209
210
211
212 public Filter getFilter() {
213 return this.filter;
214 }
215
216
217
218
219
220
221
222
223
224
225
226 public void setCacheBlocks(boolean cacheBlocks) {
227 this.cacheBlocks = cacheBlocks;
228 }
229
230
231
232
233
234
235 public boolean getCacheBlocks() {
236 return cacheBlocks;
237 }
238
239
240
241
242
243 public byte [] getRow() {
244 return this.row;
245 }
246
247
248
249
250
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 = (Filter)createForName(Bytes.toString(Bytes.readByteArray(in)));
404 this.filter.readFields(in);
405 }
406 this.cacheBlocks = in.readBoolean();
407 this.tr = new TimeRange();
408 tr.readFields(in);
409 int numFamilies = in.readInt();
410 this.familyMap =
411 new TreeMap<byte [],NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
412 for(int i=0; i<numFamilies; i++) {
413 byte [] family = Bytes.readByteArray(in);
414 boolean hasColumns = in.readBoolean();
415 NavigableSet<byte []> set = null;
416 if(hasColumns) {
417 int numColumns = in.readInt();
418 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
419 for(int j=0; j<numColumns; j++) {
420 byte [] qualifier = Bytes.readByteArray(in);
421 set.add(qualifier);
422 }
423 }
424 this.familyMap.put(family, set);
425 }
426 readAttributes(in);
427 }
428
429 public void write(final DataOutput out)
430 throws IOException {
431 out.writeByte(GET_VERSION);
432 Bytes.writeByteArray(out, this.row);
433 out.writeLong(this.lockId);
434 out.writeInt(this.maxVersions);
435 if(this.filter == null) {
436 out.writeBoolean(false);
437 } else {
438 out.writeBoolean(true);
439 Bytes.writeByteArray(out, Bytes.toBytes(filter.getClass().getName()));
440 filter.write(out);
441 }
442 out.writeBoolean(this.cacheBlocks);
443 tr.write(out);
444 out.writeInt(familyMap.size());
445 for(Map.Entry<byte [], NavigableSet<byte []>> entry :
446 familyMap.entrySet()) {
447 Bytes.writeByteArray(out, entry.getKey());
448 NavigableSet<byte []> columnSet = entry.getValue();
449 if(columnSet == null) {
450 out.writeBoolean(false);
451 } else {
452 out.writeBoolean(true);
453 out.writeInt(columnSet.size());
454 for(byte [] qualifier : columnSet) {
455 Bytes.writeByteArray(out, qualifier);
456 }
457 }
458 }
459 writeAttributes(out);
460 }
461
462 @SuppressWarnings("unchecked")
463 private Writable createForName(String className) {
464 try {
465 Class<? extends Writable> clazz =
466 (Class<? extends Writable>) Class.forName(className);
467 return WritableFactories.newInstance(clazz, new Configuration());
468 } catch (ClassNotFoundException e) {
469 throw new RuntimeException("Can't find class " + className);
470 }
471 }
472 }