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 @Deprecated 055 @Override 056 public ReturnCode filterKeyValue(final Cell c) { 057 return filterCell(c); 058 } 059 060 @Override 061 public ReturnCode filterCell(final Cell c) { 062 if (foundKV) return ReturnCode.NEXT_ROW; 063 foundKV = true; 064 return ReturnCode.INCLUDE; 065 } 066 067 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 068 Preconditions.checkArgument(filterArguments.isEmpty(), "Expected 0 but got: %s", 069 filterArguments.size()); 070 return new FirstKeyOnlyFilter(); 071 } 072 073 /** Returns true if first KV has been found. */ 074 protected boolean hasFoundKV() { 075 return this.foundKV; 076 } 077 078 /** 079 * Set or clear the indication if the first KV has been found. 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 * Parse a serialized representation of {@link FirstKeyOnlyFilter} 095 * @param pbBytes A pb serialized {@link FirstKeyOnlyFilter} instance 096 * @return An instance of {@link FirstKeyOnlyFilter} made from <code>bytes</code> 097 * @throws DeserializationException if an error occurred 098 * @see #toByteArray 099 */ 100 public static FirstKeyOnlyFilter parseFrom(final byte[] pbBytes) throws DeserializationException { 101 // There is nothing to deserialize. Why do this at all? 102 try { 103 FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes); 104 } catch (InvalidProtocolBufferException e) { 105 throw new DeserializationException(e); 106 } 107 // Just return a new instance. 108 return new FirstKeyOnlyFilter(); 109 } 110 111 /** 112 * Returns 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) { 118 return true; 119 } 120 if (!(o instanceof FirstKeyOnlyFilter)) { 121 return false; 122 } 123 return true; 124 } 125 126 @Override 127 public boolean equals(Object obj) { 128 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 129 } 130 131 @Override 132 public int hashCode() { 133 return Boolean.hashCode(foundKV); 134 } 135}