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