001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.filter;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Objects;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.exceptions.DeserializationException;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
029
030import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
031import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
032
033/**
034 * A filter that will only return the first KV from each row.
035 * <p>
036 * This filter can be used to more efficiently perform row count operations.
037 */
038@InterfaceAudience.Public
039public class FirstKeyOnlyFilter extends FilterBase {
040  private boolean foundKV = false;
041
042  public FirstKeyOnlyFilter() {
043  }
044
045  @Override
046  public void reset() {
047    foundKV = false;
048  }
049
050  @Override
051  public boolean filterRowKey(Cell cell) throws IOException {
052    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
053    return false;
054  }
055
056  @Override
057  public ReturnCode filterCell(final Cell c) {
058    if(foundKV) return ReturnCode.NEXT_ROW;
059    foundKV = true;
060    return ReturnCode.INCLUDE;
061  }
062
063  public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
064    Preconditions.checkArgument(filterArguments.isEmpty(),
065                                "Expected 0 but got: %s", filterArguments.size());
066    return new FirstKeyOnlyFilter();
067  }
068
069  /**
070   * @return true if first KV has been found.
071   */
072  protected boolean hasFoundKV() {
073    return this.foundKV;
074  }
075
076  /**
077   *
078   * @param value update {@link #foundKV} flag with value.
079   */
080  protected void setFoundKV(boolean value) {
081    this.foundKV = value;
082  }
083
084  /**
085   * @return The filter serialized using pb
086   */
087  @Override
088  public byte [] toByteArray() {
089    FilterProtos.FirstKeyOnlyFilter.Builder builder =
090      FilterProtos.FirstKeyOnlyFilter.newBuilder();
091    return builder.build().toByteArray();
092  }
093
094  /**
095   * @param pbBytes A pb serialized {@link FirstKeyOnlyFilter} instance
096   * @return An instance of {@link FirstKeyOnlyFilter} made from <code>bytes</code>
097   * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
098   * @see #toByteArray
099   */
100  public static FirstKeyOnlyFilter parseFrom(final byte [] pbBytes)
101  throws DeserializationException {
102    // There is nothing to deserialize.  Why do this at all?
103    try {
104      FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes);
105    } catch (InvalidProtocolBufferException e) {
106      throw new DeserializationException(e);
107    }
108    // Just return a new instance.
109    return new FirstKeyOnlyFilter();
110  }
111
112  /**
113   * @param o the other filter to compare with
114   * @return true if and only if the fields of the filter that are serialized
115   * are equal to the corresponding fields in other.  Used for testing.
116   */
117  @Override
118  boolean areSerializedFieldsEqual(Filter o) {
119    if (o == this) return true;
120    if (!(o instanceof FirstKeyOnlyFilter)) return false;
121
122    return true;
123  }
124
125  @Override
126  public boolean equals(Object obj) {
127    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
128  }
129
130  @Override
131  public int hashCode() {
132    return Objects.hashCode(foundKV);
133  }
134}