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  
21  package org.apache.hadoop.hbase.filter;
22  
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  import com.google.common.base.Preconditions;
31  
32  /**
33   * Pass results that have same row prefix.
34   */
35  public class PrefixFilter extends FilterBase {
36    protected byte [] prefix = null;
37    protected boolean passedPrefix = false;
38    protected boolean filterRow = true;
39  
40    public PrefixFilter(final byte [] prefix) {
41      this.prefix = prefix;
42    }
43  
44    public PrefixFilter() {
45      super();
46    }
47  
48    public byte[] getPrefix() {
49      return prefix;
50    }
51  
52    public boolean filterRowKey(byte[] buffer, int offset, int length) {
53      if (buffer == null || this.prefix == null)
54        return true;
55      if (length < prefix.length)
56        return true;
57      // if they are equal, return false => pass row
58      // else return true, filter row
59      // if we are passed the prefix, set flag
60      int cmp = Bytes.compareTo(buffer, offset, this.prefix.length, this.prefix, 0,
61          this.prefix.length);
62      if(cmp > 0) {
63        passedPrefix = true;
64      }
65      filterRow = (cmp != 0);
66      return filterRow;
67    }
68  
69    public boolean filterRow() {
70      return filterRow;
71    }
72  
73    public void reset() {
74      filterRow = true;
75    }
76  
77    public boolean filterAllRemaining() {
78      return passedPrefix;
79    }
80  
81    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
82      Preconditions.checkArgument(filterArguments.size() == 1,
83                                  "Expected 1 but got: %s", filterArguments.size());
84      byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
85      return new PrefixFilter(prefix);
86    }
87  
88    public void write(DataOutput out) throws IOException {
89      Bytes.writeByteArray(out, this.prefix);
90    }
91  
92    public void readFields(DataInput in) throws IOException {
93      this.prefix = Bytes.readByteArray(in);
94    }
95  
96    @Override
97    public String toString() {
98      return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
99    }
100 }