View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.filter;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  
25  import java.io.DataInput;
26  import java.io.DataOutput;
27  import java.io.IOException;
28  import java.util.List;
29  
30  /**
31   * A wrapper filter that returns true from {@link #filterAllRemaining()} as soon
32   * as the wrapped filters {@link Filter#filterRowKey(byte[], int, int)},
33   * {@link Filter#filterKeyValue(org.apache.hadoop.hbase.KeyValue)},
34   * {@link org.apache.hadoop.hbase.filter.Filter#filterRow()} or
35   * {@link org.apache.hadoop.hbase.filter.Filter#filterAllRemaining()} methods
36   * returns true.
37   */
38  public class WhileMatchFilter extends FilterBase {
39    private boolean filterAllRemaining = false;
40    private Filter filter;
41  
42    public WhileMatchFilter() {
43      super();
44    }
45  
46    public WhileMatchFilter(Filter filter) {
47      this.filter = filter;
48    }
49  
50    public Filter getFilter() {
51      return filter;
52    }
53  
54    public void reset() {
55      this.filter.reset();
56    }
57  
58    private void changeFAR(boolean value) {
59      filterAllRemaining = filterAllRemaining || value;
60    }
61  
62    public boolean filterAllRemaining() {
63      return this.filterAllRemaining || this.filter.filterAllRemaining();
64    }
65  
66    public boolean filterRowKey(byte[] buffer, int offset, int length) {
67      boolean value = filter.filterRowKey(buffer, offset, length);
68      changeFAR(value);
69      return value;
70    }
71  
72    public ReturnCode filterKeyValue(KeyValue v) {
73      ReturnCode c = filter.filterKeyValue(v);
74      changeFAR(c != ReturnCode.INCLUDE);
75      return c;
76    }
77  
78    public boolean filterRow() {
79      boolean filterRow = this.filter.filterRow();
80      changeFAR(filterRow);
81      return filterRow;
82    }
83  
84    public void write(DataOutput out) throws IOException {
85      out.writeUTF(this.filter.getClass().getName());
86      this.filter.write(out);
87    }
88  
89    public void readFields(DataInput in) throws IOException {
90      String className = in.readUTF();
91      try {
92        this.filter = (Filter)(Class.forName(className).newInstance());
93        this.filter.readFields(in);
94      } catch (InstantiationException e) {
95        throw new RuntimeException("Failed deserialize.", e);
96      } catch (IllegalAccessException e) {
97        throw new RuntimeException("Failed deserialize.", e);
98      } catch (ClassNotFoundException e) {
99        throw new RuntimeException("Failed deserialize.", e);
100     }
101   }
102 }