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