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 com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.exceptions.DeserializationException;
29  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
30  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
31  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32  
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
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   public boolean hasFilterRow() {
101    return true;
102   }
103 
104   // Here we remove from row all key values from testing column
105   @Override
106   public void filterRowCells(List<Cell> kvs) {
107     Iterator<? extends Cell> it = kvs.iterator();
108     while (it.hasNext()) {
109       KeyValue kv = KeyValueUtil.ensureKeyValue(it.next());
110       // If the current column is actually the tested column,
111       // we will skip it instead.
112       if (kv.matchingColumn(this.columnFamily, this.columnQualifier)) {
113         it.remove();
114       }
115     }
116   }
117 
118   public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
119     SingleColumnValueFilter tempFilter = (SingleColumnValueFilter)
120       SingleColumnValueFilter.createFilterFromArguments(filterArguments);
121     SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter (
122       tempFilter.getFamily(), tempFilter.getQualifier(),
123       tempFilter.getOperator(), tempFilter.getComparator());
124 
125     if (filterArguments.size() == 6) {
126       filter.setFilterIfMissing(tempFilter.getFilterIfMissing());
127       filter.setLatestVersionOnly(tempFilter.getLatestVersionOnly());
128     }
129     return filter;
130   }
131 
132   /**
133    * @return The filter serialized using pb
134    */
135   public byte [] toByteArray() {
136     FilterProtos.SingleColumnValueExcludeFilter.Builder builder =
137       FilterProtos.SingleColumnValueExcludeFilter.newBuilder();
138     builder.setSingleColumnValueFilter(super.convert());
139     return builder.build().toByteArray();
140   }
141 
142   /**
143    * @param pbBytes A pb serialized {@link SingleColumnValueExcludeFilter} instance
144    * @return An instance of {@link SingleColumnValueExcludeFilter} made from <code>bytes</code>
145    * @throws DeserializationException
146    * @see #toByteArray
147    */
148   public static SingleColumnValueExcludeFilter parseFrom(final byte [] pbBytes)
149   throws DeserializationException {
150     FilterProtos.SingleColumnValueExcludeFilter proto;
151     try {
152       proto = FilterProtos.SingleColumnValueExcludeFilter.parseFrom(pbBytes);
153     } catch (InvalidProtocolBufferException e) {
154       throw new DeserializationException(e);
155     }
156 
157     FilterProtos.SingleColumnValueFilter parentProto = proto.getSingleColumnValueFilter();
158     final CompareOp compareOp =
159       CompareOp.valueOf(parentProto.getCompareOp().name());
160     final ByteArrayComparable comparator;
161     try {
162       comparator = ProtobufUtil.toComparator(parentProto.getComparator());
163     } catch (IOException ioe) {
164       throw new DeserializationException(ioe);
165     }
166 
167     return new SingleColumnValueExcludeFilter(parentProto.hasColumnFamily() ? parentProto
168         .getColumnFamily().toByteArray() : null, parentProto.hasColumnQualifier() ? parentProto
169         .getColumnQualifier().toByteArray() : null, compareOp, comparator, parentProto
170         .getFilterIfMissing(), parentProto.getLatestVersionOnly());
171   }
172 
173   /**
174    * @param other
175    * @return true if and only if the fields of the filter that are serialized
176    * are equal to the corresponding fields in other.  Used for testing.
177    */
178   boolean areSerializedFieldsEqual(Filter o) {
179     if (o == this) return true;
180     if (!(o instanceof SingleColumnValueExcludeFilter)) return false;
181 
182     return super.areSerializedFieldsEqual(o);
183   }
184 }