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.filter;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  
27  import org.apache.hadoop.hbase.KeyValue;
28  import com.google.common.base.Preconditions;
29  
30  /**
31   * A filter, based on the ColumnCountGetFilter, takes two arguments: limit and offset.
32   * This filter can be used for row-based indexing, where references to other tables are stored across many columns,
33   * in order to efficient lookups and paginated results for end users.
34   */
35  public class ColumnPaginationFilter extends FilterBase
36  {
37    private int limit = 0;
38    private int offset = 0;
39    private int count = 0;
40  
41      /**
42       * Used during serialization. Do not use.
43       */
44    public ColumnPaginationFilter()
45    {
46      super();
47    }
48  
49    public ColumnPaginationFilter(final int limit, final int offset)
50    {
51      Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit);
52      Preconditions.checkArgument(offset >= 0, "offset must be positive %s", offset);
53      this.limit = limit;
54      this.offset = offset;
55    }
56  
57    /**
58     * @return limit
59     */
60    public int getLimit() {
61      return limit;
62    }
63  
64    /**
65     * @return offset
66     */
67    public int getOffset() {
68      return offset;
69    }
70  
71    @Override
72    public ReturnCode filterKeyValue(KeyValue v)
73    {
74      if(count >= offset + limit)
75      {
76        return ReturnCode.NEXT_ROW;
77      }
78  
79      ReturnCode code = count < offset ? ReturnCode.SKIP : ReturnCode.INCLUDE;
80      count++;
81      return code;
82    }
83  
84    @Override
85    public void reset()
86    {
87      this.count = 0;
88    }
89  
90    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
91      Preconditions.checkArgument(filterArguments.size() == 2,
92                                  "Expected 2 but got: %s", filterArguments.size());
93      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
94      int offset = ParseFilter.convertByteArrayToInt(filterArguments.get(1));
95      return new ColumnPaginationFilter(limit, offset);
96    }
97  
98    public void readFields(DataInput in) throws IOException
99    {
100     this.limit = in.readInt();
101     this.offset = in.readInt();
102   }
103 
104   public void write(DataOutput out) throws IOException
105   {
106     out.writeInt(this.limit);
107     out.writeInt(this.offset);
108   }
109 }