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  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.CellUtil;
31  import org.apache.hadoop.hbase.exceptions.DeserializationException;
32  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
35  
36  import com.google.protobuf.InvalidProtocolBufferException;
37  
38  /**
39   * A {@link Filter} that checks a single column value, but does not emit the
40   * tested column. This will enable a performance boost over
41   * {@link SingleColumnValueFilter}, if the tested column value is not actually
42   * needed as input (besides for the filtering itself).
43   */
44  @InterfaceAudience.Public
45  @InterfaceStability.Stable
46  public class SingleColumnValueExcludeFilter extends SingleColumnValueFilter {
47  
48    /**
49     * Constructor for binary compare of the value of a single column. If the
50     * column is found and the condition passes, all columns of the row will be
51     * emitted; except for the tested column value. If the column is not found or
52     * the condition fails, the row will not be emitted.
53     *
54     * @param family name of column family
55     * @param qualifier name of column qualifier
56     * @param compareOp operator
57     * @param value value to compare column values against
58     */
59    public SingleColumnValueExcludeFilter(byte[] family, byte[] qualifier,
60        CompareOp compareOp, byte[] value) {
61      super(family, qualifier, compareOp, value);
62    }
63  
64    /**
65     * Constructor for binary compare of the value of a single column. If the
66     * column is found and the condition passes, all columns of the row will be
67     * emitted; except for the tested column value. If the condition fails, the
68     * row will not be emitted.
69     * <p>
70     * Use the filterIfColumnMissing flag to set whether the rest of the columns
71     * in a row will be emitted if the specified column to check is not found in
72     * the row.
73     *
74     * @param family name of column family
75     * @param qualifier name of column qualifier
76     * @param compareOp operator
77     * @param comparator Comparator to use.
78     */
79    public SingleColumnValueExcludeFilter(byte[] family, byte[] qualifier,
80        CompareOp compareOp, ByteArrayComparable comparator) {
81      super(family, qualifier, compareOp, comparator);
82    }
83  
84    /**
85     * Constructor for protobuf deserialization only.
86     * @param family
87     * @param qualifier
88     * @param compareOp
89     * @param comparator
90     * @param filterIfMissing
91     * @param latestVersionOnly
92     */
93    protected SingleColumnValueExcludeFilter(final byte[] family, final byte[] qualifier,
94        final CompareOp compareOp, ByteArrayComparable comparator, final boolean filterIfMissing,
95        final boolean latestVersionOnly) {
96      super(family, qualifier, compareOp, comparator, filterIfMissing, latestVersionOnly);
97    }
98  
99    // We cleaned result row in FilterRow to be consistent with scanning process.
100   @Override
101   public boolean hasFilterRow() {
102    return true;
103   }
104 
105   // Here we remove from row all key values from testing column
106   @Override
107   public void filterRowCells(List<Cell> kvs) {
108     Iterator<? extends Cell> it = kvs.iterator();
109     while (it.hasNext()) {
110       Cell cell = it.next();
111       // If the current column is actually the tested column,
112       // we will skip it instead.
113       if (CellUtil.matchingColumn(cell, this.columnFamily, this.columnQualifier)) {
114         it.remove();
115       }
116     }
117   }
118 
119   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
120     SingleColumnValueFilter tempFilter = (SingleColumnValueFilter)
121       SingleColumnValueFilter.createFilterFromArguments(filterArguments);
122     SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter (
123       tempFilter.getFamily(), tempFilter.getQualifier(),
124       tempFilter.getOperator(), tempFilter.getComparator());
125 
126     if (filterArguments.size() == 6) {
127       filter.setFilterIfMissing(tempFilter.getFilterIfMissing());
128       filter.setLatestVersionOnly(tempFilter.getLatestVersionOnly());
129     }
130     return filter;
131   }
132 
133   /**
134    * @return The filter serialized using pb
135    */
136   @Override
137   public byte [] toByteArray() {
138     FilterProtos.SingleColumnValueExcludeFilter.Builder builder =
139       FilterProtos.SingleColumnValueExcludeFilter.newBuilder();
140     builder.setSingleColumnValueFilter(super.convert());
141     return builder.build().toByteArray();
142   }
143 
144   /**
145    * @param pbBytes A pb serialized {@link SingleColumnValueExcludeFilter} instance
146    * @return An instance of {@link SingleColumnValueExcludeFilter} made from <code>bytes</code>
147    * @throws DeserializationException
148    * @see #toByteArray
149    */
150   public static SingleColumnValueExcludeFilter parseFrom(final byte [] pbBytes)
151   throws DeserializationException {
152     FilterProtos.SingleColumnValueExcludeFilter proto;
153     try {
154       proto = FilterProtos.SingleColumnValueExcludeFilter.parseFrom(pbBytes);
155     } catch (InvalidProtocolBufferException e) {
156       throw new DeserializationException(e);
157     }
158 
159     FilterProtos.SingleColumnValueFilter parentProto = proto.getSingleColumnValueFilter();
160     final CompareOp compareOp =
161       CompareOp.valueOf(parentProto.getCompareOp().name());
162     final ByteArrayComparable comparator;
163     try {
164       comparator = ProtobufUtil.toComparator(parentProto.getComparator());
165     } catch (IOException ioe) {
166       throw new DeserializationException(ioe);
167     }
168 
169     return new SingleColumnValueExcludeFilter(parentProto.hasColumnFamily() ? parentProto
170         .getColumnFamily().toByteArray() : null, parentProto.hasColumnQualifier() ? parentProto
171         .getColumnQualifier().toByteArray() : null, compareOp, comparator, parentProto
172         .getFilterIfMissing(), parentProto.getLatestVersionOnly());
173   }
174 
175   /**
176    * @param other
177    * @return true if and only if the fields of the filter that are serialized
178    * are equal to the corresponding fields in other.  Used for testing.
179    */
180   @Override
181   boolean areSerializedFieldsEqual(Filter o) {
182     if (o == this) return true;
183     if (!(o instanceof SingleColumnValueExcludeFilter)) return false;
184 
185     return super.areSerializedFieldsEqual(o);
186   }
187 
188   @Override
189   public boolean equals(Object obj) {
190     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
191   }
192 
193   @Override
194   public int hashCode() {
195     return super.hashCode();
196   }
197 }