View Javadoc

1   package org.apache.hadoop.hbase.filter;
2   
3   import java.io.DataInput;
4   import java.io.DataOutput;
5   import java.io.IOException;
6   import java.util.List;
7   import java.util.TreeSet;
8   
9   import org.apache.hadoop.hbase.KeyValue;
10  
11  /**
12   * Filter that returns only cells whose timestamp (version) is
13   * in the specified list of timestamps (versions).
14   * <p>
15   * Note: Use of this filter overrides any time range/time stamp
16   * options specified using {@link org.apache.hadoop.hbase.client.Get#setTimeRange(long, long)},
17   * {@link org.apache.hadoop.hbase.client.Scan#setTimeRange(long, long)}, {@link org.apache.hadoop.hbase.client.Get#setTimeStamp(long)},
18   * or {@link org.apache.hadoop.hbase.client.Scan#setTimeStamp(long)}.
19   */
20  public class TimestampsFilter extends FilterBase {
21  
22    TreeSet<Long> timestamps;
23  
24    // Used during scans to hint the scan to stop early
25    // once the timestamps fall below the minTimeStamp.
26    long minTimeStamp = Long.MAX_VALUE;
27  
28    /**
29     * Used during deserialization. Do not use otherwise.
30     */
31    public TimestampsFilter() {
32      super();
33    }
34  
35    /**
36     * Constructor for filter that retains only those
37     * cells whose timestamp (version) is in the specified
38     * list of timestamps.
39     *
40     * @param timestamps
41     */
42    public TimestampsFilter(List<Long> timestamps) {
43      this.timestamps = new TreeSet<Long>(timestamps);
44      init();
45    }
46  
47    private void init() {
48      if (this.timestamps.size() > 0) {
49        minTimeStamp = this.timestamps.first();
50      }
51    }
52  
53    /**
54     * Gets the minimum timestamp requested by filter.
55     * @return  minimum timestamp requested by filter.
56     */
57    public long getMin() {
58      return minTimeStamp;
59    }
60  
61    @Override
62    public ReturnCode filterKeyValue(KeyValue v) {
63      if (this.timestamps.contains(v.getTimestamp())) {
64        return ReturnCode.INCLUDE;
65      } else if (v.getTimestamp() < minTimeStamp) {
66        // The remaining versions of this column are guaranteed
67        // to be lesser than all of the other values.
68        return ReturnCode.NEXT_COL;
69      }
70      return ReturnCode.SKIP;
71    }
72  
73    @Override
74    public void readFields(DataInput in) throws IOException {
75      int numTimestamps = in.readInt();
76      this.timestamps = new TreeSet<Long>();
77      for (int idx = 0; idx < numTimestamps; idx++) {
78        this.timestamps.add(in.readLong());
79      }
80      init();
81    }
82  
83    @Override
84    public void write(DataOutput out) throws IOException {
85      int numTimestamps = this.timestamps.size();
86      out.writeInt(numTimestamps);
87      for (Long timestamp : this.timestamps) {
88        out.writeLong(timestamp);
89      }
90    }
91  }