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 */ 019 020package org.apache.hadoop.hbase.filter; 021 022import java.io.IOException; 023import java.util.ArrayList; 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 * Simple filter that returns first N columns on row only. 035 * This filter was written to test filters in Get and as soon as it gets 036 * its quota of columns, {@link #filterAllRemaining()} returns true. This 037 * makes this filter unsuitable as a Scan filter. 038 */ 039@InterfaceAudience.Public 040public class ColumnCountGetFilter extends FilterBase { 041 private int limit = 0; 042 private int count = 0; 043 044 public ColumnCountGetFilter(final int n) { 045 Preconditions.checkArgument(n >= 0, "limit be positive %s", n); 046 this.limit = n; 047 } 048 049 public int getLimit() { 050 return limit; 051 } 052 053 @Override 054 public boolean filterRowKey(Cell cell) throws IOException { 055 // Impl in FilterBase might do unnecessary copy for Off heap backed Cells. 056 if (filterAllRemaining()) return true; 057 return false; 058 } 059 060 @Override 061 public boolean filterAllRemaining() { 062 return this.count > this.limit; 063 } 064 065 @Deprecated 066 @Override 067 public ReturnCode filterKeyValue(final Cell c) { 068 return filterCell(c); 069 } 070 071 @Override 072 public ReturnCode filterCell(final Cell c) { 073 this.count++; 074 return filterAllRemaining() ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL; 075 } 076 077 @Override 078 public void reset() { 079 this.count = 0; 080 } 081 082 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) { 083 Preconditions.checkArgument(filterArguments.size() == 1, 084 "Expected 1 but got: %s", filterArguments.size()); 085 int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0)); 086 return new ColumnCountGetFilter(limit); 087 } 088 089 /** 090 * @return The filter serialized using pb 091 */ 092 @Override 093 public byte [] toByteArray() { 094 FilterProtos.ColumnCountGetFilter.Builder builder = 095 FilterProtos.ColumnCountGetFilter.newBuilder(); 096 builder.setLimit(this.limit); 097 return builder.build().toByteArray(); 098 } 099 100 /** 101 * @param pbBytes A pb serialized {@link ColumnCountGetFilter} instance 102 * @return An instance of {@link ColumnCountGetFilter} made from <code>bytes</code> 103 * @throws org.apache.hadoop.hbase.exceptions.DeserializationException 104 * @see #toByteArray 105 */ 106 public static ColumnCountGetFilter parseFrom(final byte [] pbBytes) 107 throws DeserializationException { 108 FilterProtos.ColumnCountGetFilter proto; 109 try { 110 proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes); 111 } catch (InvalidProtocolBufferException e) { 112 throw new DeserializationException(e); 113 } 114 return new ColumnCountGetFilter(proto.getLimit()); 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 ColumnCountGetFilter)) return false; 126 127 ColumnCountGetFilter other = (ColumnCountGetFilter)o; 128 return this.getLimit() == other.getLimit(); 129 } 130 131 @Override 132 public String toString() { 133 return this.getClass().getSimpleName() + " " + this.limit; 134 } 135}