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