001/*
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase.filter;
021
022import java.io.IOException;
023import java.util.List;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.exceptions.DeserializationException;
028import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
030import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
031
032/**
033 * This is a Filter wrapper class which is used in the server side. Some filter
034 * related hooks can be defined in this wrapper. The only way to create a
035 * FilterWrapper instance is passing a client side Filter instance through
036 * {@link org.apache.hadoop.hbase.client.Scan#getFilter()}.
037 *
038 */
039@InterfaceAudience.Private
040final public class FilterWrapper extends Filter {
041  Filter filter = null;
042
043  public FilterWrapper( Filter filter ) {
044    if (null == filter) {
045      // ensure the filter instance is not null
046      throw new NullPointerException("Cannot create FilterWrapper with null Filter");
047    }
048    this.filter = filter;
049  }
050
051  /**
052   * @return The filter serialized using pb
053   */
054  @Override
055  public byte[] toByteArray() throws IOException {
056    FilterProtos.FilterWrapper.Builder builder =
057      FilterProtos.FilterWrapper.newBuilder();
058    builder.setFilter(ProtobufUtil.toFilter(this.filter));
059    return builder.build().toByteArray();
060  }
061
062  /**
063   * @param pbBytes A pb serialized {@link FilterWrapper} instance
064   * @return An instance of {@link FilterWrapper} made from <code>bytes</code>
065   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
066   * @see #toByteArray
067   */
068  public static FilterWrapper parseFrom(final byte [] pbBytes)
069  throws DeserializationException {
070    FilterProtos.FilterWrapper proto;
071    try {
072      proto = FilterProtos.FilterWrapper.parseFrom(pbBytes);
073    } catch (InvalidProtocolBufferException e) {
074      throw new DeserializationException(e);
075    }
076    try {
077      return new FilterWrapper(ProtobufUtil.toFilter(proto.getFilter()));
078    } catch (IOException ioe) {
079      throw new DeserializationException(ioe);
080    }
081  }
082
083  @Override
084  public void reset() throws IOException {
085    this.filter.reset();
086  }
087
088  @Override
089  public boolean filterAllRemaining() throws IOException {
090    return this.filter.filterAllRemaining();
091  }
092
093  @Override
094  public boolean filterRow() throws IOException {
095    return this.filter.filterRow();
096  }
097
098  @Override
099  public Cell getNextCellHint(Cell currentCell) throws IOException {
100    return this.filter.getNextCellHint(currentCell);
101  }
102
103  @Override
104  public boolean filterRowKey(Cell cell) throws IOException {
105    if (filterAllRemaining()) return true;
106    return this.filter.filterRowKey(cell);
107  }
108
109  @Override
110  public ReturnCode filterCell(final Cell c) throws IOException {
111    return this.filter.filterCell(c);
112  }
113
114  @Override
115  public Cell transformCell(Cell v) throws IOException {
116    return this.filter.transformCell(v);
117  }
118
119  @Override
120  public boolean hasFilterRow() {
121    return this.filter.hasFilterRow();
122  }
123
124  @Override
125  public void filterRowCells(List<Cell> kvs) throws IOException {
126    filterRowCellsWithRet(kvs);
127  }
128
129  public enum FilterRowRetCode {
130    NOT_CALLED,
131    INCLUDE,     // corresponds to filter.filterRow() returning false
132    EXCLUDE,     // corresponds to filter.filterRow() returning true
133    INCLUDE_THIS_FAMILY  // exclude other families
134  }
135  public FilterRowRetCode filterRowCellsWithRet(List<Cell> kvs) throws IOException {
136    //To fix HBASE-6429,
137    //Filter with filterRow() returning true is incompatible with scan with limit
138    //1. hasFilterRow() returns true, if either filterRow() or filterRow(kvs) is implemented.
139    //2. filterRow() is merged with filterRow(kvs),
140    //so that to make all those row related filtering stuff in the same function.
141    this.filter.filterRowCells(kvs);
142    if (!kvs.isEmpty()) {
143      if (this.filter.filterRow()) {
144        kvs.clear();
145        return FilterRowRetCode.EXCLUDE;
146      }
147      return FilterRowRetCode.INCLUDE;
148    }
149    return FilterRowRetCode.NOT_CALLED;
150  }
151
152  @Override
153  public boolean isFamilyEssential(byte[] name) throws IOException {
154    return filter.isFamilyEssential(name);
155  }
156
157  /**
158   * @param o the other filter to compare with
159   * @return true if and only if the fields of the filter that are serialized
160   * are equal to the corresponding fields in other.  Used for testing.
161   */
162  @Override
163  boolean areSerializedFieldsEqual(Filter o) {
164    if (o == this) return true;
165    if (!(o instanceof FilterWrapper)) return false;
166
167    FilterWrapper other = (FilterWrapper)o;
168    return this.filter.areSerializedFieldsEqual(other.filter);
169  }
170}