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