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 com.google.common.base.Preconditions;
23  import com.google.protobuf.InvalidProtocolBufferException;
24  import com.google.protobuf.ZeroCopyLiteralByteString;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import java.util.ArrayList;
33  
34  /**
35   * Pass results that have same row prefix.
36   */
37  @InterfaceAudience.Public
38  @InterfaceStability.Stable
39  public class PrefixFilter extends FilterBase {
40    protected byte [] prefix = null;
41    protected boolean passedPrefix = false;
42    protected boolean filterRow = true;
43  
44    public PrefixFilter(final byte [] prefix) {
45      this.prefix = prefix;
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    /**
89     * @return The filter serialized using pb
90     */
91    public byte [] toByteArray() {
92      FilterProtos.PrefixFilter.Builder builder =
93        FilterProtos.PrefixFilter.newBuilder();
94      if (this.prefix != null) builder.setPrefix(ZeroCopyLiteralByteString.wrap(this.prefix));
95      return builder.build().toByteArray();
96    }
97  
98    /**
99     * @param pbBytes A pb serialized {@link PrefixFilter} instance
100    * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
101    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
102    * @see #toByteArray
103    */
104   public static PrefixFilter parseFrom(final byte [] pbBytes)
105   throws DeserializationException {
106     FilterProtos.PrefixFilter proto;
107     try {
108       proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
109     } catch (InvalidProtocolBufferException e) {
110       throw new DeserializationException(e);
111     }
112     return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
113   }
114 
115   /**
116    * @param other
117    * @return true if and only if the fields of the filter that are serialized
118    * are equal to the corresponding fields in other.  Used for testing.
119    */
120   boolean areSerializedFieldsEqual(Filter o) {
121     if (o == this) return true;
122     if (!(o instanceof PrefixFilter)) return false;
123 
124     PrefixFilter other = (PrefixFilter)o;
125     return Bytes.equals(this.getPrefix(), other.getPrefix());
126   }
127 
128   @Override
129   public String toString() {
130     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
131   }
132 }