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