View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.filter.Filter;
25  import org.apache.hadoop.hbase.io.TimeRange;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.hbase.util.Classes;
28  import org.apache.hadoop.io.Writable;
29  
30  import java.io.DataInput;
31  import java.io.DataOutput;
32  import java.io.IOException;
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.NavigableSet;
38  import java.util.Set;
39  import java.util.TreeMap;
40  import java.util.TreeSet;
41  
42  /**
43   * Used to perform Get operations on a single row.
44   * <p>
45   * To get everything for a row, instantiate a Get object with the row to get.
46   * To further define the scope of what to get, perform additional methods as
47   * outlined below.
48   * <p>
49   * To get all columns from specific families, execute {@link #addFamily(byte[]) addFamily}
50   * for each family to retrieve.
51   * <p>
52   * To get specific columns, execute {@link #addColumn(byte[], byte[]) addColumn}
53   * for each column to retrieve.
54   * <p>
55   * To only retrieve columns within a specific range of version timestamps,
56   * execute {@link #setTimeRange(long, long) setTimeRange}.
57   * <p>
58   * To only retrieve columns with a specific timestamp, execute
59   * {@link #setTimeStamp(long) setTimestamp}.
60   * <p>
61   * To limit the number of versions of each column to be returned, execute
62   * {@link #setMaxVersions(int) setMaxVersions}.
63   * <p>
64   * To add a filter, execute {@link #setFilter(Filter) setFilter}.
65   */
66  public class Get extends OperationWithAttributes
67    implements Writable, Row, Comparable<Row> {
68    private static final byte GET_VERSION = (byte)2;
69  
70    private byte [] row = null;
71    private long lockId = -1L;
72    private int maxVersions = 1;
73    private boolean cacheBlocks = true;
74    private Filter filter = null;
75    private TimeRange tr = new TimeRange();
76    private Map<byte [], NavigableSet<byte []>> familyMap =
77      new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
78  
79    /** Constructor for Writable.  DO NOT USE */
80    public Get() {}
81  
82    /**
83     * Create a Get operation for the specified row.
84     * <p>
85     * If no further operations are done, this will get the latest version of
86     * all columns in all families of the specified row.
87     * @param row row key
88     */
89    public Get(byte [] row) {
90      this(row, null);
91    }
92  
93    /**
94     * Create a Get operation for the specified row, using an existing row lock.
95     * <p>
96     * If no further operations are done, this will get the latest version of
97     * all columns in all families of the specified row.
98     * @param row row key
99     * @param rowLock previously acquired row lock, or null
100    * @deprecated {@link RowLock} is deprecated, use {@link #Get(byte[])}.
101    */
102   public Get(byte [] row, RowLock rowLock) {
103     this.row = row;
104     if(rowLock != null) {
105       this.lockId = rowLock.getLockId();
106     }
107   }
108 
109   /**
110    * Get all columns from the specified family.
111    * <p>
112    * Overrides previous calls to addColumn for this family.
113    * @param family family name
114    * @return the Get object
115    */
116   public Get addFamily(byte [] family) {
117     familyMap.remove(family);
118     familyMap.put(family, null);
119     return this;
120   }
121 
122   /**
123    * Get the column from the specific family with the specified qualifier.
124    * <p>
125    * Overrides previous calls to addFamily for this family.
126    * @param family family name
127    * @param qualifier column qualifier
128    * @return the Get objec
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    * Get versions of columns only within the specified timestamp range,
145    * [minStamp, maxStamp).
146    * @param minStamp minimum timestamp value, inclusive
147    * @param maxStamp maximum timestamp value, exclusive
148    * @throws IOException if invalid time range
149    * @return this for invocation chaining
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    * Get versions of columns with the specified timestamp.
159    * @param timestamp version timestamp
160    * @return this for invocation chaining
161    */
162   public Get setTimeStamp(long timestamp) {
163     try {
164       tr = new TimeRange(timestamp, timestamp+1);
165     } catch(IOException e) {
166       // Will never happen
167     }
168     return this;
169   }
170 
171   /**
172    * Get all available versions.
173    * @return this for invocation chaining
174    */
175   public Get setMaxVersions() {
176     this.maxVersions = Integer.MAX_VALUE;
177     return this;
178   }
179 
180   /**
181    * Get up to the specified number of versions of each column.
182    * @param maxVersions maximum versions for each column
183    * @throws IOException if invalid number of versions
184    * @return this for invocation chaining
185    */
186   public Get setMaxVersions(int maxVersions) throws IOException {
187     if(maxVersions <= 0) {
188       throw new IOException("maxVersions must be positive");
189     }
190     this.maxVersions = maxVersions;
191     return this;
192   }
193 
194   /**
195    * Apply the specified server-side filter when performing the Get.
196    * Only {@link Filter#filterKeyValue(KeyValue)} is called AFTER all tests
197    * for ttl, column match, deletes and max versions have been run.
198    * @param filter filter to run on the server
199    * @return this for invocation chaining
200    */
201   public Get setFilter(Filter filter) {
202     this.filter = filter;
203     return this;
204   }
205 
206   /* Accessors */
207 
208   /**
209    * @return Filter
210    */
211   public Filter getFilter() {
212     return this.filter;
213   }
214 
215   /**
216    * Set whether blocks should be cached for this Get.
217    * <p>
218    * This is true by default.  When true, default settings of the table and
219    * family are used (this will never override caching blocks if the block
220    * cache is disabled for that family or entirely).
221    *
222    * @param cacheBlocks if false, default settings are overridden and blocks
223    * will not be cached
224    */
225   public void setCacheBlocks(boolean cacheBlocks) {
226     this.cacheBlocks = cacheBlocks;
227   }
228 
229   /**
230    * Get whether blocks should be cached for this Get.
231    * @return true if default caching should be used, false if blocks should not
232    * be cached
233    */
234   public boolean getCacheBlocks() {
235     return cacheBlocks;
236   }
237 
238   /**
239    * Method for retrieving the get's row
240    * @return row
241    */
242   public byte [] getRow() {
243     return this.row;
244   }
245 
246   /**
247    * Method for retrieving the get's RowLock
248    * @return RowLock
249    */
250   @SuppressWarnings("deprecation")
251   public RowLock getRowLock() {
252     return new RowLock(this.row, this.lockId);
253   }
254 
255   /**
256    * Method for retrieving the get's lockId
257    * @return lockId
258    */
259   public long getLockId() {
260     return this.lockId;
261   }
262 
263   /**
264    * Method for retrieving the get's maximum number of version
265    * @return the maximum number of version to fetch for this get
266    */
267   public int getMaxVersions() {
268     return this.maxVersions;
269   }
270 
271   /**
272    * Method for retrieving the get's TimeRange
273    * @return timeRange
274    */
275   public TimeRange getTimeRange() {
276     return this.tr;
277   }
278 
279   /**
280    * Method for retrieving the keys in the familyMap
281    * @return keys in the current familyMap
282    */
283   public Set<byte[]> familySet() {
284     return this.familyMap.keySet();
285   }
286 
287   /**
288    * Method for retrieving the number of families to get from
289    * @return number of families
290    */
291   public int numFamilies() {
292     return this.familyMap.size();
293   }
294 
295   /**
296    * Method for checking if any families have been inserted into this Get
297    * @return true if familyMap is non empty false otherwise
298    */
299   public boolean hasFamilies() {
300     return !this.familyMap.isEmpty();
301   }
302 
303   /**
304    * Method for retrieving the get's familyMap
305    * @return familyMap
306    */
307   public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
308     return this.familyMap;
309   }
310 
311   /**
312    * Compile the table and column family (i.e. schema) information
313    * into a String. Useful for parsing and aggregation by debugging,
314    * logging, and administration tools.
315    * @return Map
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    * Compile the details beyond the scope of getFingerprint (row, columns,
331    * timestamps, etc.) into a Map along with the fingerprinted information.
332    * Useful for debugging, logging, and administration tools.
333    * @param maxCols a limit on the number of columns output prior to truncation
334    * @return Map
335    */
336   @Override
337   public Map<String, Object> toMap(int maxCols) {
338     // we start with the fingerprint map and build on top of it.
339     Map<String, Object> map = getFingerprint();
340     // replace the fingerprint's simple list of families with a 
341     // map from column families to lists of qualifiers and kv details
342     Map<String, List<String>> columns = new HashMap<String, List<String>>();
343     map.put("families", columns);
344     // add scalar information first
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     // iterate through affected families and add details
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     // add the id if set
380     if (getId() != null) {
381       map.put("id", getId());
382     }
383     return map;
384   }
385 
386   //Row
387   public int compareTo(Row other) {
388     return Bytes.compareTo(this.getRow(), other.getRow());
389   }
390   
391   //Writable
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 = Classes.createWritableForName(
404         Bytes.toString(Bytes.readByteArray(in)));
405       this.filter.readFields(in);
406     }
407     this.cacheBlocks = in.readBoolean();
408     this.tr = new TimeRange();
409     tr.readFields(in);
410     int numFamilies = in.readInt();
411     this.familyMap =
412       new TreeMap<byte [],NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
413     for(int i=0; i<numFamilies; i++) {
414       byte [] family = Bytes.readByteArray(in);
415       boolean hasColumns = in.readBoolean();
416       NavigableSet<byte []> set = null;
417       if(hasColumns) {
418         int numColumns = in.readInt();
419         set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
420         for(int j=0; j<numColumns; j++) {
421           byte [] qualifier = Bytes.readByteArray(in);
422           set.add(qualifier);
423         }
424       }
425       this.familyMap.put(family, set);
426     }
427     readAttributes(in);
428   }
429 
430   public void write(final DataOutput out)
431   throws IOException {
432     out.writeByte(GET_VERSION);
433     Bytes.writeByteArray(out, this.row);
434     out.writeLong(this.lockId);
435     out.writeInt(this.maxVersions);
436     if(this.filter == null) {
437       out.writeBoolean(false);
438     } else {
439       out.writeBoolean(true);
440       Bytes.writeByteArray(out, Bytes.toBytes(filter.getClass().getName()));
441       filter.write(out);
442     }
443     out.writeBoolean(this.cacheBlocks);
444     tr.write(out);
445     out.writeInt(familyMap.size());
446     for(Map.Entry<byte [], NavigableSet<byte []>> entry :
447       familyMap.entrySet()) {
448       Bytes.writeByteArray(out, entry.getKey());
449       NavigableSet<byte []> columnSet = entry.getValue();
450       if(columnSet == null) {
451         out.writeBoolean(false);
452       } else {
453         out.writeBoolean(true);
454         out.writeInt(columnSet.size());
455         for(byte [] qualifier : columnSet) {
456           Bytes.writeByteArray(out, qualifier);
457         }
458       }
459     }
460     writeAttributes(out);
461   }
462 }