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.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.hadoop.hbase.ByteBufferedCell;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  import org.apache.hadoop.hbase.exceptions.DeserializationException;
31  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32  import org.apache.hadoop.hbase.util.ByteBufferUtils;
33  import org.apache.hadoop.hbase.util.ByteStringer;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import com.google.common.base.Preconditions;
37  import com.google.protobuf.InvalidProtocolBufferException;
38  
39  /**
40   * This filter is used for selecting only those keys with columns that matches
41   * a particular prefix. For example, if prefix is 'an', it will pass keys with
42   * columns like 'and', 'anti' but not keys with columns like 'ball', 'act'.
43   */
44  @InterfaceAudience.Public
45  @InterfaceStability.Stable
46  public class ColumnPrefixFilter extends FilterBase {
47    protected byte [] prefix = null;
48  
49    public ColumnPrefixFilter(final byte [] prefix) {
50      this.prefix = prefix;
51    }
52  
53    public byte[] getPrefix() {
54      return prefix;
55    }
56  
57    @Override
58    public boolean filterRowKey(Cell cell) throws IOException {
59      // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
60      return false;
61    }
62  
63    @Override
64    public ReturnCode filterKeyValue(Cell cell) {
65      if (this.prefix == null) {
66        return ReturnCode.INCLUDE;
67      } else {
68        return filterColumn(cell);
69      }
70    }
71  
72    public ReturnCode filterColumn(Cell cell) {
73      int qualifierLength = cell.getQualifierLength();
74      if (qualifierLength < prefix.length) {
75        int cmp = compareQualifierPart(cell, qualifierLength, this.prefix);
76        if (cmp <= 0) {
77          return ReturnCode.SEEK_NEXT_USING_HINT;
78        } else {
79          return ReturnCode.NEXT_ROW;
80        }
81      } else {
82        int cmp = compareQualifierPart(cell, this.prefix.length, this.prefix);
83        if (cmp < 0) {
84          return ReturnCode.SEEK_NEXT_USING_HINT;
85        } else if (cmp > 0) {
86          return ReturnCode.NEXT_ROW;
87        } else {
88          return ReturnCode.INCLUDE;
89        }
90      }
91    }
92  
93    private static int compareQualifierPart(Cell cell, int length, byte[] prefix) {
94      if (cell instanceof ByteBufferedCell) {
95        return ByteBufferUtils.compareTo(((ByteBufferedCell) cell).getQualifierByteBuffer(),
96            ((ByteBufferedCell) cell).getQualifierPositionInByteBuffer(), length, prefix, 0, length);
97      }
98      return Bytes.compareTo(cell.getQualifierArray(), cell.getQualifierOffset(), length, prefix, 0,
99          length);
100   }
101 
102   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
103     Preconditions.checkArgument(filterArguments.size() == 1,
104                                 "Expected 1 but got: %s", filterArguments.size());
105     byte [] columnPrefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
106     return new ColumnPrefixFilter(columnPrefix);
107   }
108 
109   /**
110    * @return The filter serialized using pb
111    */
112   public byte [] toByteArray() {
113     FilterProtos.ColumnPrefixFilter.Builder builder =
114       FilterProtos.ColumnPrefixFilter.newBuilder();
115     if (this.prefix != null) builder.setPrefix(ByteStringer.wrap(this.prefix));
116     return builder.build().toByteArray();
117   }
118 
119   /**
120    * @param pbBytes A pb serialized {@link ColumnPrefixFilter} instance
121    * @return An instance of {@link ColumnPrefixFilter} made from <code>bytes</code>
122    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
123    * @see #toByteArray
124    */
125   public static ColumnPrefixFilter parseFrom(final byte [] pbBytes)
126   throws DeserializationException {
127     FilterProtos.ColumnPrefixFilter proto;
128     try {
129       proto = FilterProtos.ColumnPrefixFilter.parseFrom(pbBytes);
130     } catch (InvalidProtocolBufferException e) {
131       throw new DeserializationException(e);
132     }
133     return new ColumnPrefixFilter(proto.getPrefix().toByteArray());
134   }
135 
136   /**
137    * @param other
138    * @return true if and only if the fields of the filter that are serialized
139    * are equal to the corresponding fields in other.  Used for testing.
140    */
141   boolean areSerializedFieldsEqual(Filter o) {
142    if (o == this) return true;
143    if (!(o instanceof ColumnPrefixFilter)) return false;
144 
145    ColumnPrefixFilter other = (ColumnPrefixFilter)o;
146     return Bytes.equals(this.getPrefix(), other.getPrefix());
147   }
148 
149   @Override
150   public Cell getNextCellHint(Cell cell) {
151     return CellUtil.createFirstOnRowCol(cell, prefix, 0, prefix.length);
152   }
153 
154   @Override
155   public String toString() {
156     return this.getClass().getSimpleName() + " " + Bytes.toStringBinary(this.prefix);
157   }
158 }