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.Objects;
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   * A wrapper filter that returns true from {@link #filterAllRemaining()} as soon
36   * as the wrapped filters {@link Filter#filterRowKey(byte[], int, int)},
37   * {@link Filter#filterKeyValue(org.apache.hadoop.hbase.Cell)},
38   * {@link org.apache.hadoop.hbase.filter.Filter#filterRow()} or
39   * {@link org.apache.hadoop.hbase.filter.Filter#filterAllRemaining()} methods
40   * returns true.
41   */
42  @InterfaceAudience.Public
43  @InterfaceStability.Stable
44  public class WhileMatchFilter extends FilterBase {
45    private boolean filterAllRemaining = false;
46    private Filter filter;
47  
48    public WhileMatchFilter(Filter filter) {
49      this.filter = filter;
50    }
51  
52    public Filter getFilter() {
53      return filter;
54    }
55  
56    @Override
57    public void reset() throws IOException {
58      this.filter.reset();
59    }
60  
61    private void changeFAR(boolean value) {
62      filterAllRemaining = filterAllRemaining || value;
63    }
64  
65    @Override
66    public boolean filterAllRemaining() throws IOException {
67      return this.filterAllRemaining || this.filter.filterAllRemaining();
68    }
69  
70    @Override
71    public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException {
72      boolean value = filter.filterRowKey(buffer, offset, length);
73      changeFAR(value);
74      return value;
75    }
76  
77    @Override
78    public ReturnCode filterKeyValue(Cell v) throws IOException {
79      ReturnCode c = filter.filterKeyValue(v);
80      changeFAR(c != ReturnCode.INCLUDE);
81      return c;
82    }
83  
84    @Override
85    public Cell transformCell(Cell v) throws IOException {
86      return filter.transformCell(v);
87    }
88  
89    @Override
90    public boolean filterRow() throws IOException {
91      boolean filterRow = this.filter.filterRow();
92      changeFAR(filterRow);
93      return filterRow;
94    }
95    
96    @Override
97    public boolean hasFilterRow() {
98      return true;
99    }
100 
101   /**
102    * @return The filter serialized using pb
103    */
104   @Override
105   public byte[] toByteArray() throws IOException {
106     FilterProtos.WhileMatchFilter.Builder builder =
107       FilterProtos.WhileMatchFilter.newBuilder();
108     builder.setFilter(ProtobufUtil.toFilter(this.filter));
109     return builder.build().toByteArray();
110   }
111 
112   /**
113    * @param pbBytes A pb serialized {@link WhileMatchFilter} instance
114    * @return An instance of {@link WhileMatchFilter} made from <code>bytes</code>
115    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
116    * @see #toByteArray
117    */
118   public static WhileMatchFilter parseFrom(final byte [] pbBytes)
119   throws DeserializationException {
120     FilterProtos.WhileMatchFilter proto;
121     try {
122       proto = FilterProtos.WhileMatchFilter.parseFrom(pbBytes);
123     } catch (InvalidProtocolBufferException e) {
124       throw new DeserializationException(e);
125     }
126     try {
127       return new WhileMatchFilter(ProtobufUtil.toFilter(proto.getFilter()));
128     } catch (IOException ioe) {
129       throw new DeserializationException(ioe);
130     }
131   }
132 
133   /**
134    * @param other
135    * @return true if and only if the fields of the filter that are serialized
136    * are equal to the corresponding fields in other.  Used for testing.
137    */
138   @Override
139   boolean areSerializedFieldsEqual(Filter o) {
140     if (o == this) return true;
141     if (!(o instanceof WhileMatchFilter)) return false;
142 
143     WhileMatchFilter other = (WhileMatchFilter)o;
144     return getFilter().areSerializedFieldsEqual(other.getFilter());
145   }
146 
147   @Override
148   public boolean isFamilyEssential(byte[] name) throws IOException {
149     return filter.isFamilyEssential(name);
150   }
151 
152   @Override
153   public String toString() {
154     return this.getClass().getSimpleName() + " " + this.filter.toString();
155   }
156 
157   @Override
158   public boolean equals(Object obj) {
159     return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
160   }
161 
162   @Override
163   public int hashCode() {
164     return Objects.hash(this.filter);
165   }
166 }