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.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31  
32  import com.google.protobuf.InvalidProtocolBufferException;
33  
34  /**
35   * <p>
36   * This filter is used to filter based on the column family. It takes an
37   * operator (equal, greater, not equal, etc) and a byte [] comparator for the
38   * column family portion of a key.
39   * </p><p>
40   * This filter can be wrapped with {@link org.apache.hadoop.hbase.filter.WhileMatchFilter} and {@link org.apache.hadoop.hbase.filter.SkipFilter}
41   * to add more control.
42   * </p><p>
43   * Multiple filters can be combined using {@link org.apache.hadoop.hbase.filter.FilterList}.
44   * </p>
45   * If an already known column family is looked for, use {@link org.apache.hadoop.hbase.client.Get#addFamily(byte[])}
46   * directly rather than a filter.
47   */
48  @InterfaceAudience.Public
49  @InterfaceStability.Stable
50  public class FamilyFilter extends CompareFilter {
51  
52    /**
53     * Constructor.
54     *
55     * @param familyCompareOp  the compare op for column family matching
56     * @param familyComparator the comparator for column family matching
57     */
58    public FamilyFilter(final CompareOp familyCompareOp,
59                        final ByteArrayComparable familyComparator) {
60        super(familyCompareOp, familyComparator);
61    }
62  
63    @Override
64    public ReturnCode filterKeyValue(Cell v) {
65      int familyLength = v.getFamilyLength();
66      if (familyLength > 0) {
67        if (doCompare(this.compareOp, this.comparator, v.getFamilyArray(),
68            v.getFamilyOffset(), familyLength)) {
69          return ReturnCode.NEXT_ROW;
70        }
71      }
72      return ReturnCode.INCLUDE;
73    }
74  
75    public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
76      ArrayList<?> arguments = CompareFilter.extractArguments(filterArguments);
77      CompareOp compareOp = (CompareOp)arguments.get(0);
78      ByteArrayComparable comparator = (ByteArrayComparable)arguments.get(1);
79      return new FamilyFilter(compareOp, comparator);
80    }
81  
82    /**
83     * @return The filter serialized using pb
84     */
85    @Override
86    public byte [] toByteArray() {
87      FilterProtos.FamilyFilter.Builder builder =
88        FilterProtos.FamilyFilter.newBuilder();
89      builder.setCompareFilter(super.convert());
90      return builder.build().toByteArray();
91    }
92  
93    /**
94     * @param pbBytes A pb serialized {@link FamilyFilter} instance
95     * @return An instance of {@link FamilyFilter} made from <code>bytes</code>
96     * @throws DeserializationException
97     * @see #toByteArray
98     */
99    public static FamilyFilter parseFrom(final byte [] pbBytes)
100   throws DeserializationException {
101     FilterProtos.FamilyFilter proto;
102     try {
103       proto = FilterProtos.FamilyFilter.parseFrom(pbBytes);
104     } catch (InvalidProtocolBufferException e) {
105       throw new DeserializationException(e);
106     }
107     final CompareOp valueCompareOp =
108       CompareOp.valueOf(proto.getCompareFilter().getCompareOp().name());
109     ByteArrayComparable valueComparator = null;
110     try {
111       if (proto.getCompareFilter().hasComparator()) {
112         valueComparator = ProtobufUtil.toComparator(proto.getCompareFilter().getComparator());
113       }
114     } catch (IOException ioe) {
115       throw new DeserializationException(ioe);
116     }
117     return new FamilyFilter(valueCompareOp,valueComparator);
118   }
119 
120   /**
121    * @param other
122    * @return true if and only if the fields of the filter that are serialized
123    * are equal to the corresponding fields in other.  Used for testing.
124    */
125   @Override
126   boolean areSerializedFieldsEqual(Filter o) {
127     if (o == this) return true;
128     if (!(o instanceof FamilyFilter)) return false;
129 
130     FamilyFilter other = (FamilyFilter)o;
131     return super.areSerializedFieldsEqual(other);
132  }
133 
134   @Override
135   public boolean equals(Object obj) {
136     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
137   }
138 
139   @Override
140   public int hashCode() {
141     return super.hashCode();
142   }
143 }