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