View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.filter;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.TreeSet;
26  import java.util.ArrayList;
27  
28  import org.apache.hadoop.hbase.KeyValue;
29  import com.google.common.base.Preconditions;
30  
31  /**
32   * Filter that returns only cells whose timestamp (version) is
33   * in the specified list of timestamps (versions).
34   * <p>
35   * Note: Use of this filter overrides any time range/time stamp
36   * options specified using {@link org.apache.hadoop.hbase.client.Get#setTimeRange(long, long)},
37   * {@link org.apache.hadoop.hbase.client.Scan#setTimeRange(long, long)}, {@link org.apache.hadoop.hbase.client.Get#setTimeStamp(long)},
38   * or {@link org.apache.hadoop.hbase.client.Scan#setTimeStamp(long)}.
39   */
40  public class TimestampsFilter extends FilterBase {
41  
42    TreeSet<Long> timestamps;
43  
44    // Used during scans to hint the scan to stop early
45    // once the timestamps fall below the minTimeStamp.
46    long minTimeStamp = Long.MAX_VALUE;
47  
48    /**
49     * Used during deserialization. Do not use otherwise.
50     */
51    public TimestampsFilter() {
52      super();
53    }
54  
55    /**
56     * Constructor for filter that retains only those
57     * cells whose timestamp (version) is in the specified
58     * list of timestamps.
59     *
60     * @param timestamps
61     */
62    public TimestampsFilter(List<Long> timestamps) {
63      for (Long timestamp : timestamps) {
64        Preconditions.checkArgument(timestamp >= 0, "must be positive %s", timestamp);
65      }
66      this.timestamps = new TreeSet<Long>(timestamps);
67      init();
68    }
69  
70    /**
71     * @return the list of timestamps
72     */
73    public List<Long> getTimestamps() {
74      List<Long> list = new ArrayList<Long>(timestamps.size());
75      list.addAll(timestamps);
76      return list;
77    }
78  
79    private void init() {
80      if (this.timestamps.size() > 0) {
81        minTimeStamp = this.timestamps.first();
82      }
83    }
84  
85    /**
86     * Gets the minimum timestamp requested by filter.
87     * @return  minimum timestamp requested by filter.
88     */
89    public long getMin() {
90      return minTimeStamp;
91    }
92  
93    @Override
94    public ReturnCode filterKeyValue(KeyValue v) {
95      if (this.timestamps.contains(v.getTimestamp())) {
96        return ReturnCode.INCLUDE;
97      } else if (v.getTimestamp() < minTimeStamp) {
98        // The remaining versions of this column are guaranteed
99        // to be lesser than all of the other values.
100       return ReturnCode.NEXT_COL;
101     }
102     return ReturnCode.SKIP;
103   }
104 
105   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
106     ArrayList<Long> timestamps = new ArrayList<Long>();
107     for (int i = 0; i<filterArguments.size(); i++) {
108       long timestamp = ParseFilter.convertByteArrayToLong(filterArguments.get(i));
109       timestamps.add(timestamp);
110     }
111     return new TimestampsFilter(timestamps);
112   }
113 
114   @Override
115   public void readFields(DataInput in) throws IOException {
116     int numTimestamps = in.readInt();
117     this.timestamps = new TreeSet<Long>();
118     for (int idx = 0; idx < numTimestamps; idx++) {
119       this.timestamps.add(in.readLong());
120     }
121     init();
122   }
123 
124   @Override
125   public void write(DataOutput out) throws IOException {
126     int numTimestamps = this.timestamps.size();
127     out.writeInt(numTimestamps);
128     for (Long timestamp : this.timestamps) {
129       out.writeLong(timestamp);
130     }
131   }
132 }