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.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.Cell;
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 ReturnCode filterKeyValue(Cell v) throws IOException {
77      ReturnCode c = filter.filterKeyValue(v);
78      changeFAR(c != ReturnCode.INCLUDE);
79      return c;
80    }
81  
82    @Override
83    public Cell transformCell(Cell v) throws IOException {
84      return filter.transformCell(v);
85    }
86  
87    @Override
88    public boolean filterRow() throws IOException {
89      boolean filterRow = this.filter.filterRow();
90      changeFAR(filterRow);
91      return filterRow;
92    }
93    
94    @Override
95    public boolean hasFilterRow() {
96      return true;
97    }
98  
99    /**
100    * @return The filter serialized using pb
101    */
102   public byte[] toByteArray() throws IOException {
103     FilterProtos.WhileMatchFilter.Builder builder =
104       FilterProtos.WhileMatchFilter.newBuilder();
105     builder.setFilter(ProtobufUtil.toFilter(this.filter));
106     return builder.build().toByteArray();
107   }
108 
109   /**
110    * @param pbBytes A pb serialized {@link WhileMatchFilter} instance
111    * @return An instance of {@link WhileMatchFilter} made from <code>bytes</code>
112    * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
113    * @see #toByteArray
114    */
115   public static WhileMatchFilter parseFrom(final byte [] pbBytes)
116   throws DeserializationException {
117     FilterProtos.WhileMatchFilter proto;
118     try {
119       proto = FilterProtos.WhileMatchFilter.parseFrom(pbBytes);
120     } catch (InvalidProtocolBufferException e) {
121       throw new DeserializationException(e);
122     }
123     try {
124       return new WhileMatchFilter(ProtobufUtil.toFilter(proto.getFilter()));
125     } catch (IOException ioe) {
126       throw new DeserializationException(ioe);
127     }
128   }
129 
130   /**
131    * @param other
132    * @return true if and only if the fields of the filter that are serialized
133    * are equal to the corresponding fields in other.  Used for testing.
134    */
135   boolean areSerializedFieldsEqual(Filter o) {
136     if (o == this) return true;
137     if (!(o instanceof WhileMatchFilter)) return false;
138 
139     WhileMatchFilter other = (WhileMatchFilter)o;
140     return getFilter().areSerializedFieldsEqual(other.getFilter());
141   }
142 
143   public boolean isFamilyEssential(byte[] name) throws IOException {
144     return filter.isFamilyEssential(name);
145   }
146 
147   @Override
148   public String toString() {
149     return this.getClass().getSimpleName() + " " + this.filter.toString();
150   }
151 }