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  import org.apache.hadoop.hbase.util.Classes;
25  
26  import java.io.DataInput;
27  import java.io.DataOutput;
28  import java.io.IOException;
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    @Override
79    public KeyValue transform(KeyValue v) {
80      return filter.transform(v);
81    }
82  
83    public boolean filterRow() {
84      boolean filterRow = this.filter.filterRow();
85      changeFAR(filterRow);
86      return filterRow;
87    }
88  
89    public void write(DataOutput out) throws IOException {
90      out.writeUTF(this.filter.getClass().getName());
91      this.filter.write(out);
92    }
93  
94    public void readFields(DataInput in) throws IOException {
95      this.filter = Classes.createForName(in.readUTF());
96      this.filter.readFields(in);
97    }
98  
99    public boolean isFamilyEssential(byte[] name) {
100     return FilterBase.isFamilyEssential(this.filter, name);
101   }
102 
103   @Override
104   public String toString() {
105     return this.getClass().getSimpleName() + " " + this.filter.toString();
106   }
107 }