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 org.apache.hadoop.hbase.util.ByteStringer;
24  import com.google.protobuf.InvalidProtocolBufferException;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.exceptions.DeserializationException;
30  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  import java.util.ArrayList;
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    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
77    // See HBASE-12068
78    @Override
79    public Cell transformCell(Cell v) {
80      return v;
81    }
82  
83    public boolean filterRow() {
84      return filterRow;
85    }
86  
87    public void reset() {
88      filterRow = true;
89    }
90  
91    public boolean filterAllRemaining() {
92      return passedPrefix;
93    }
94  
95    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
96      Preconditions.checkArgument(filterArguments.size() == 1,
97                                  "Expected 1 but got: %s", filterArguments.size());
98      byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
99      return new PrefixFilter(prefix);
100   }
101 
102   /**
103    * @return The filter serialized using pb
104    */
105   public byte [] toByteArray() {
106     FilterProtos.PrefixFilter.Builder builder =
107       FilterProtos.PrefixFilter.newBuilder();
108     if (this.prefix != null) builder.setPrefix(ByteStringer.wrap(this.prefix));
109     return builder.build().toByteArray();
110   }
111 
112   /**
113    * @param pbBytes A pb serialized {@link PrefixFilter} instance
114    * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
115    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
116    * @see #toByteArray
117    */
118   public static PrefixFilter parseFrom(final byte [] pbBytes)
119   throws DeserializationException {
120     FilterProtos.PrefixFilter proto;
121     try {
122       proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
123     } catch (InvalidProtocolBufferException e) {
124       throw new DeserializationException(e);
125     }
126     return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
127   }
128 
129   /**
130    * @param other
131    * @return true if and only if the fields of the filter that are serialized
132    * are equal to the corresponding fields in other.  Used for testing.
133    */
134   boolean areSerializedFieldsEqual(Filter o) {
135     if (o == this) return true;
136     if (!(o instanceof PrefixFilter)) return false;
137 
138     PrefixFilter other = (PrefixFilter)o;
139     return Bytes.equals(this.getPrefix(), other.getPrefix());
140   }
141 
142   @Override
143   public String toString() {
144     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
145   }
146 }