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