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.filter;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Objects;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.exceptions.DeserializationException;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
028import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
029
030import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
031
032/**
033 * A filter that will only return the first KV from each row.
034 * <p>
035 * This filter can be used to more efficiently perform row count operations.
036 */
037@InterfaceAudience.Public
038public class FirstKeyOnlyFilter extends FilterBase {
039  private boolean foundKV = false;
040
041  public FirstKeyOnlyFilter() {
042  }
043
044  @Override
045  public void reset() {
046    foundKV = false;
047  }
048
049  @Override
050  public boolean filterRowKey(Cell cell) throws IOException {
051    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
052    return false;
053  }
054
055  @Deprecated
056  @Override
057  public ReturnCode filterKeyValue(final Cell c) {
058    return filterCell(c);
059  }
060
061  @Override
062  public ReturnCode filterCell(final Cell c) {
063    if (foundKV) return ReturnCode.NEXT_ROW;
064    foundKV = true;
065    return ReturnCode.INCLUDE;
066  }
067
068  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
069    Preconditions.checkArgument(filterArguments.isEmpty(), "Expected 0 but got: %s",
070      filterArguments.size());
071    return new FirstKeyOnlyFilter();
072  }
073
074  /** Returns true if first KV has been found. */
075  protected boolean hasFoundKV() {
076    return this.foundKV;
077  }
078
079  /**
080   * @param value update {@link #foundKV} flag with value.
081   */
082  protected void setFoundKV(boolean value) {
083    this.foundKV = value;
084  }
085
086  /** Returns The filter serialized using pb */
087  @Override
088  public byte[] toByteArray() {
089    FilterProtos.FirstKeyOnlyFilter.Builder builder = FilterProtos.FirstKeyOnlyFilter.newBuilder();
090    return builder.build().toByteArray();
091  }
092
093  /**
094   * @param pbBytes A pb serialized {@link FirstKeyOnlyFilter} instance
095   * @return An instance of {@link FirstKeyOnlyFilter} made from <code>bytes</code>
096   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
097   * @see #toByteArray
098   */
099  public static FirstKeyOnlyFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
100    // There is nothing to deserialize. Why do this at all?
101    try {
102      FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes);
103    } catch (InvalidProtocolBufferException e) {
104      throw new DeserializationException(e);
105    }
106    // Just return a new instance.
107    return new FirstKeyOnlyFilter();
108  }
109
110  /**
111   * @param o the other filter to compare with
112   * @return true if and only if the fields of the filter that are serialized are equal to the
113   *         corresponding fields in other. Used for testing.
114   */
115  @Override
116  boolean areSerializedFieldsEqual(Filter o) {
117    if (o == this) return true;
118    if (!(o instanceof FirstKeyOnlyFilter)) return false;
119
120    return true;
121  }
122
123  @Override
124  public boolean equals(Object obj) {
125    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
126  }
127
128  @Override
129  public int hashCode() {
130    return Objects.hashCode(foundKV);
131  }
132}