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. Only most recent versions are considered
34   * for pagination.
35   */
36  public class ColumnPaginationFilter extends FilterBase
37  {
38    private int limit = 0;
39    private int offset = 0;
40    private int count = 0;
41  
42      /**
43       * Used during serialization. Do not use.
44       */
45    public ColumnPaginationFilter()
46    {
47      super();
48    }
49  
50    public ColumnPaginationFilter(final int limit, final int offset)
51    {
52      Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit);
53      Preconditions.checkArgument(offset >= 0, "offset must be positive %s", offset);
54      this.limit = limit;
55      this.offset = offset;
56    }
57  
58    /**
59     * @return limit
60     */
61    public int getLimit() {
62      return limit;
63    }
64  
65    /**
66     * @return offset
67     */
68    public int getOffset() {
69      return offset;
70    }
71  
72    @Override
73    public ReturnCode filterKeyValue(KeyValue v)
74    {
75      if(count >= offset + limit)
76      {
77        return ReturnCode.NEXT_ROW;
78      }
79  
80      ReturnCode code = count < offset ? ReturnCode.NEXT_COL :
81                                         ReturnCode.INCLUDE_AND_NEXT_COL;
82      count++;
83      return code;
84    }
85  
86    @Override
87    public void reset()
88    {
89      this.count = 0;
90    }
91  
92    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
93      Preconditions.checkArgument(filterArguments.size() == 2,
94                                  "Expected 2 but got: %s", filterArguments.size());
95      int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
96      int offset = ParseFilter.convertByteArrayToInt(filterArguments.get(1));
97      return new ColumnPaginationFilter(limit, offset);
98    }
99  
100   public void readFields(DataInput in) throws IOException
101   {
102     this.limit = in.readInt();
103     this.offset = in.readInt();
104   }
105 
106   public void write(DataOutput out) throws IOException
107   {
108     out.writeInt(this.limit);
109     out.writeInt(this.offset);
110   }
111 
112   @Override
113   public String toString() {
114     return String.format("%s (%d, %d)", this.getClass().getSimpleName(),
115         this.limit, this.offset);
116   }
117 }