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;
022
023import org.apache.hadoop.hbase.Cell;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.hadoop.hbase.exceptions.DeserializationException;
026import org.apache.hadoop.hbase.filter.FilterBase;
027import org.apache.hadoop.hbase.util.Bytes;
028
029@InterfaceAudience.Private
030public final class ColumnCountOnRowFilter extends FilterBase {
031
032  private final int limit;
033
034  private int count = 0;
035
036  public ColumnCountOnRowFilter(int limit) {
037    this.limit = limit;
038  }
039
040  @Override
041  public ReturnCode filterCell(final Cell c) {
042    count++;
043    return count > limit ? ReturnCode.NEXT_ROW : ReturnCode.INCLUDE;
044  }
045
046  @Override
047  public void reset() throws IOException {
048    this.count = 0;
049  }
050
051  @Override
052  public byte[] toByteArray() throws IOException {
053    return Bytes.toBytes(limit);
054  }
055
056  public static ColumnCountOnRowFilter parseFrom(byte[] bytes) throws DeserializationException {
057    return new ColumnCountOnRowFilter(Bytes.toInt(bytes));
058  }
059
060  @Override
061  public boolean equals(Object obj) {
062    if (!(obj instanceof ColumnCountOnRowFilter)) {
063      return false;
064    }
065    if (this == obj) {
066      return true;
067    }
068    ColumnCountOnRowFilter f = (ColumnCountOnRowFilter) obj;
069    return this.limit == f.limit;
070  }
071
072  @Override
073  public int hashCode() {
074    return Objects.hash(this.limit);
075  }
076}