001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.filter;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Objects;
025
026import org.apache.hadoop.hbase.Cell;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.exceptions.DeserializationException;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
030
031import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
032import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
033
034/**
035 * Simple filter that returns first N columns on row only.
036 * This filter was written to test filters in Get and as soon as it gets
037 * its quota of columns, {@link #filterAllRemaining()} returns true.  This
038 * makes this filter unsuitable as a Scan filter.
039 */
040@InterfaceAudience.Public
041public class ColumnCountGetFilter extends FilterBase {
042  private int limit = 0;
043  private int count = 0;
044
045  public ColumnCountGetFilter(final int n) {
046    Preconditions.checkArgument(n >= 0, "limit be positive %s", n);
047    this.limit = n;
048  }
049
050  public int getLimit() {
051    return limit;
052  }
053
054  @Override
055  public boolean filterRowKey(Cell cell) throws IOException {
056    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
057    if (filterAllRemaining()) return true;
058    return false;
059  }
060
061  @Override
062  public boolean filterAllRemaining() {
063    return this.count > this.limit;
064  }
065
066  @Deprecated
067  @Override
068  public ReturnCode filterKeyValue(final Cell c) {
069    return filterCell(c);
070  }
071
072  @Override
073  public ReturnCode filterCell(final Cell c) {
074    this.count++;
075    return filterAllRemaining() ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL;
076  }
077
078  @Override
079  public void reset() {
080    this.count = 0;
081  }
082
083  public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
084    Preconditions.checkArgument(filterArguments.size() == 1,
085                                "Expected 1 but got: %s", filterArguments.size());
086    int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
087    return new ColumnCountGetFilter(limit);
088  }
089
090  /**
091   * @return The filter serialized using pb
092   */
093  @Override
094  public byte [] toByteArray() {
095    FilterProtos.ColumnCountGetFilter.Builder builder =
096      FilterProtos.ColumnCountGetFilter.newBuilder();
097    builder.setLimit(this.limit);
098    return builder.build().toByteArray();
099  }
100
101  /**
102   * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance
103   * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code>
104   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
105   * @see #toByteArray
106   */
107  public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
108  throws DeserializationException {
109    FilterProtos.ColumnCountGetFilter proto;
110    try {
111      proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
112    } catch (InvalidProtocolBufferException e) {
113      throw new DeserializationException(e);
114    }
115    return new ColumnCountGetFilter(proto.getLimit());
116  }
117
118  /**
119   * @param o the other filter to compare with
120   * @return true if and only if the fields of the filter that are serialized
121   * are equal to the corresponding fields in other.  Used for testing.
122   */
123  @Override
124  boolean areSerializedFieldsEqual(Filter o) {
125    if (o == this) return true;
126    if (!(o instanceof ColumnCountGetFilter)) return false;
127
128    ColumnCountGetFilter other = (ColumnCountGetFilter)o;
129    return this.getLimit() == other.getLimit();
130  }
131
132  @Override
133  public String toString() {
134    return this.getClass().getSimpleName() + " " + this.limit;
135  }
136
137  @Override
138  public boolean equals(Object obj) {
139    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
140  }
141
142  @Override
143  public int hashCode() {
144    return Objects.hash(this.limit);
145  }
146}