View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.exceptions.DeserializationException;
28  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29  import org.apache.hadoop.hbase.util.ByteStringer;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import com.google.common.base.Preconditions;
33  import com.google.protobuf.InvalidProtocolBufferException;
34  
35  /**
36   * Pass results that have same row prefix.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Stable
40  public class PrefixFilter extends FilterBase {
41    protected byte [] prefix = null;
42    protected boolean passedPrefix = false;
43    protected boolean filterRow = true;
44  
45    public PrefixFilter(final byte [] prefix) {
46      this.prefix = prefix;
47    }
48  
49    public byte[] getPrefix() {
50      return prefix;
51    }
52  
53    public boolean filterRowKey(byte[] buffer, int offset, int length) {
54      if (buffer == null || this.prefix == null)
55        return true;
56      if (length < prefix.length)
57        return true;
58      // if they are equal, return false => pass row
59      // else return true, filter row
60      // if we are passed the prefix, set flag
61      int cmp = Bytes.compareTo(buffer, offset, this.prefix.length, this.prefix, 0,
62          this.prefix.length);
63      if ((!isReversed() && cmp > 0) || (isReversed() && cmp < 0)) {
64        passedPrefix = true;
65      }
66      filterRow = (cmp != 0);
67      return filterRow;
68    }
69  
70    @Override
71    public ReturnCode filterKeyValue(Cell v) {
72      if (filterRow) return ReturnCode.NEXT_ROW;
73      return ReturnCode.INCLUDE;
74    }
75  
76    public boolean filterRow() {
77      return filterRow;
78    }
79  
80    public void reset() {
81      filterRow = true;
82    }
83  
84    public boolean filterAllRemaining() {
85      return passedPrefix;
86    }
87  
88    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
89      Preconditions.checkArgument(filterArguments.size() == 1,
90                                  "Expected 1 but got: %s", filterArguments.size());
91      byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
92      return new PrefixFilter(prefix);
93    }
94  
95    /**
96     * @return The filter serialized using pb
97     */
98    public byte [] toByteArray() {
99      FilterProtos.PrefixFilter.Builder builder =
100       FilterProtos.PrefixFilter.newBuilder();
101     if (this.prefix != null) builder.setPrefix(ByteStringer.wrap(this.prefix));
102     return builder.build().toByteArray();
103   }
104 
105   /**
106    * @param pbBytes A pb serialized {@link PrefixFilter} instance
107    * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
108    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
109    * @see #toByteArray
110    */
111   public static PrefixFilter parseFrom(final byte [] pbBytes)
112   throws DeserializationException {
113     FilterProtos.PrefixFilter proto;
114     try {
115       proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
116     } catch (InvalidProtocolBufferException e) {
117       throw new DeserializationException(e);
118     }
119     return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
120   }
121 
122   /**
123    * @param other
124    * @return true if and only if the fields of the filter that are serialized
125    * are equal to the corresponding fields in other.  Used for testing.
126    */
127   boolean areSerializedFieldsEqual(Filter o) {
128     if (o == this) return true;
129     if (!(o instanceof PrefixFilter)) return false;
130 
131     PrefixFilter other = (PrefixFilter)o;
132     return Bytes.equals(this.getPrefix(), other.getPrefix());
133   }
134 
135   @Override
136   public String toString() {
137     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
138   }
139 }