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