001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.io.IOException;
021import java.util.Objects;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.exceptions.DeserializationException;
024import org.apache.hadoop.hbase.filter.FilterBase;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.apache.yetus.audience.InterfaceAudience;
027
028@InterfaceAudience.Private
029public final class ColumnCountOnRowFilter extends FilterBase {
030
031  private final int limit;
032
033  private int count = 0;
034
035  public ColumnCountOnRowFilter(int limit) {
036    this.limit = limit;
037  }
038
039  @Override
040  public ReturnCode filterCell(final Cell c) {
041    count++;
042    return count > limit ? ReturnCode.NEXT_ROW : ReturnCode.INCLUDE;
043  }
044
045  @Override
046  public void reset() throws IOException {
047    this.count = 0;
048  }
049
050  @Override
051  public byte[] toByteArray() throws IOException {
052    return Bytes.toBytes(limit);
053  }
054
055  public static ColumnCountOnRowFilter parseFrom(byte[] bytes) throws DeserializationException {
056    return new ColumnCountOnRowFilter(Bytes.toInt(bytes));
057  }
058
059  @Override
060  public boolean equals(Object obj) {
061    if (!(obj instanceof ColumnCountOnRowFilter)) {
062      return false;
063    }
064    if (this == obj) {
065      return true;
066    }
067    ColumnCountOnRowFilter f = (ColumnCountOnRowFilter) obj;
068    return this.limit == f.limit;
069  }
070
071  @Override
072  public int hashCode() {
073    return Objects.hash(this.limit);
074  }
075}