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.ByteBufferedCell;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.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.ByteBufferUtils;
31  import org.apache.hadoop.hbase.util.ByteStringer;
32  import org.apache.hadoop.hbase.util.Bytes;
33  
34  import com.google.common.base.Preconditions;
35  import com.google.protobuf.InvalidProtocolBufferException;
36  
37  /**
38   * Pass results that have same row prefix.
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Stable
42  public class PrefixFilter extends FilterBase {
43    protected byte [] prefix = null;
44    protected boolean passedPrefix = false;
45    protected boolean filterRow = true;
46  
47    public PrefixFilter(final byte [] prefix) {
48      this.prefix = prefix;
49    }
50  
51    public byte[] getPrefix() {
52      return prefix;
53    }
54  
55    public boolean filterRowKey(Cell firstRowCell) {
56      if (firstRowCell == null || this.prefix == null)
57        return true;
58      int length = firstRowCell.getRowLength();
59      if (length < prefix.length) return true;
60      // if they are equal, return false => pass row
61      // else return true, filter row
62      // if we are passed the prefix, set flag
63      int cmp;
64      if (firstRowCell instanceof ByteBufferedCell) {
65        cmp = ByteBufferUtils.compareTo(((ByteBufferedCell) firstRowCell).getRowByteBuffer(),
66            ((ByteBufferedCell) firstRowCell).getRowPositionInByteBuffer(), this.prefix.length,
67            this.prefix, 0, this.prefix.length);
68      } else {
69        cmp = Bytes.compareTo(firstRowCell.getRowArray(), firstRowCell.getRowOffset(),
70            this.prefix.length, this.prefix, 0, this.prefix.length);
71      }
72      if ((!isReversed() && cmp > 0) || (isReversed() && cmp < 0)) {
73        passedPrefix = true;
74      }
75      filterRow = (cmp != 0);
76      return filterRow;
77    }
78  
79    @Override
80    public ReturnCode filterKeyValue(Cell v) {
81      if (filterRow) return ReturnCode.NEXT_ROW;
82      return ReturnCode.INCLUDE;
83    }
84  
85    public boolean filterRow() {
86      return filterRow;
87    }
88  
89    public void reset() {
90      filterRow = true;
91    }
92  
93    public boolean filterAllRemaining() {
94      return passedPrefix;
95    }
96  
97    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
98      Preconditions.checkArgument(filterArguments.size() == 1,
99                                  "Expected 1 but got: %s", filterArguments.size());
100     byte [] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
101     return new PrefixFilter(prefix);
102   }
103 
104   /**
105    * @return The filter serialized using pb
106    */
107   public byte [] toByteArray() {
108     FilterProtos.PrefixFilter.Builder builder =
109       FilterProtos.PrefixFilter.newBuilder();
110     if (this.prefix != null) builder.setPrefix(ByteStringer.wrap(this.prefix));
111     return builder.build().toByteArray();
112   }
113 
114   /**
115    * @param pbBytes A pb serialized {@link PrefixFilter} instance
116    * @return An instance of {@link PrefixFilter} made from <code>bytes</code>
117    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
118    * @see #toByteArray
119    */
120   public static PrefixFilter parseFrom(final byte [] pbBytes)
121   throws DeserializationException {
122     FilterProtos.PrefixFilter proto;
123     try {
124       proto = FilterProtos.PrefixFilter.parseFrom(pbBytes);
125     } catch (InvalidProtocolBufferException e) {
126       throw new DeserializationException(e);
127     }
128     return new PrefixFilter(proto.hasPrefix()?proto.getPrefix().toByteArray():null);
129   }
130 
131   /**
132    * @param other
133    * @return true if and only if the fields of the filter that are serialized
134    * are equal to the corresponding fields in other.  Used for testing.
135    */
136   boolean areSerializedFieldsEqual(Filter o) {
137     if (o == this) return true;
138     if (!(o instanceof PrefixFilter)) return false;
139 
140     PrefixFilter other = (PrefixFilter)o;
141     return Bytes.equals(this.getPrefix(), other.getPrefix());
142   }
143 
144   @Override
145   public String toString() {
146     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
147   }
148 }