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  @Override
056  public ReturnCode filterCell(final Cell c) {
057    if (foundKV) return ReturnCode.NEXT_ROW;
058    foundKV = true;
059    return ReturnCode.INCLUDE;
060  }
061
062  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
063    Preconditions.checkArgument(filterArguments.isEmpty(), "Expected 0 but got: %s",
064      filterArguments.size());
065    return new FirstKeyOnlyFilter();
066  }
067
068  /**
069   * @return true if first KV has been found.
070   */
071  protected boolean hasFoundKV() {
072    return this.foundKV;
073  }
074
075  /**
076   * @param value update {@link #foundKV} flag with value.
077   */
078  protected void setFoundKV(boolean value) {
079    this.foundKV = value;
080  }
081
082  /**
083   * @return The filter serialized using pb
084   */
085  @Override
086  public byte[] toByteArray() {
087    FilterProtos.FirstKeyOnlyFilter.Builder builder = FilterProtos.FirstKeyOnlyFilter.newBuilder();
088    return builder.build().toByteArray();
089  }
090
091  /**
092   * @param pbBytes A pb serialized {@link FirstKeyOnlyFilter} instance
093   * @return An instance of {@link FirstKeyOnlyFilter} made from <code>bytes</code>
094   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
095   * @see #toByteArray
096   */
097  public static FirstKeyOnlyFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
098    // There is nothing to deserialize. Why do this at all?
099    try {
100      FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes);
101    } catch (InvalidProtocolBufferException e) {
102      throw new DeserializationException(e);
103    }
104    // Just return a new instance.
105    return new FirstKeyOnlyFilter();
106  }
107
108  /**
109   * @param o the other filter to compare with
110   * @return true if and only if the fields of the filter that are serialized are equal to the
111   *         corresponding fields in other. Used for testing.
112   */
113  @Override
114  boolean areSerializedFieldsEqual(Filter o) {
115    if (o == this) return true;
116    if (!(o instanceof FirstKeyOnlyFilter)) return false;
117
118    return true;
119  }
120
121  @Override
122  public boolean equals(Object obj) {
123    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
124  }
125
126  @Override
127  public int hashCode() {
128    return Objects.hashCode(foundKV);
129  }
130}