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 java.util.List; 023import org.apache.hadoop.hbase.Cell; 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** 027 * Abstract base class to help you implement new Filters. Common "ignore" or NOOP type methods can 028 * go here, helping to reduce boiler plate in an ever-expanding filter library. If you could 029 * instantiate FilterBase, it would end up being a "null" filter - that is one that never filters 030 * anything. 031 */ 032@InterfaceAudience.Private // TODO add filter limited private level 033public abstract class FilterBase extends Filter { 034 035 /** 036 * Filters that are purely stateless and do nothing in their reset() methods can inherit this 037 * null/empty implementation. {@inheritDoc} 038 */ 039 @Override 040 public void reset() throws IOException { 041 } 042 043 /** 044 * Filters that do not filter by row key can inherit this implementation that never filters 045 * anything. (ie: returns false). {@inheritDoc} 046 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. Instead use 047 * {@link #filterRowKey(Cell)} 048 */ 049 @Override 050 @Deprecated 051 public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException { 052 if (filterAllRemaining()) return true; 053 return false; 054 } 055 056 @Override 057 public boolean filterRowKey(Cell cell) throws IOException { 058 if (filterAllRemaining()) return true; 059 return filterRowKey(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); 060 } 061 062 /** 063 * Filters that never filter all remaining can inherit this implementation that never stops the 064 * filter early. {@inheritDoc} 065 */ 066 @Override 067 public boolean filterAllRemaining() throws IOException { 068 return false; 069 } 070 071 /** 072 * By default no transformation takes place {@inheritDoc} 073 */ 074 @Override 075 public Cell transformCell(Cell v) throws IOException { 076 return v; 077 } 078 079 /** 080 * Filters that never filter by modifying the returned List of Cells can inherit this 081 * implementation that does nothing. {@inheritDoc} 082 */ 083 @Override 084 public void filterRowCells(List<Cell> ignored) throws IOException { 085 } 086 087 /** 088 * Fitlers that never filter by modifying the returned List of Cells can inherit this 089 * implementation that does nothing. {@inheritDoc} 090 */ 091 @Override 092 public boolean hasFilterRow() { 093 return false; 094 } 095 096 /** 097 * Filters that never filter by rows based on previously gathered state from 098 * {@link #filterCell(Cell)} can inherit this implementation that never filters a row. 099 * {@inheritDoc} 100 */ 101 @Override 102 public boolean filterRow() throws IOException { 103 return false; 104 } 105 106 /** 107 * Filters that are not sure which key must be next seeked to, can inherit this implementation 108 * that, by default, returns a null Cell. {@inheritDoc} 109 */ 110 @Override 111 public Cell getNextCellHint(Cell currentCell) throws IOException { 112 return null; 113 } 114 115 /** 116 * By default, we require all scan's column families to be present. Our subclasses may be more 117 * precise. {@inheritDoc} 118 */ 119 @Override 120 public boolean isFamilyEssential(byte[] name) throws IOException { 121 return true; 122 } 123 124 /** 125 * Given the filter's arguments it constructs the filter 126 * <p> 127 * @param filterArguments the filter's arguments 128 * @return constructed filter object 129 */ 130 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 131 throw new IllegalArgumentException("This method has not been implemented"); 132 } 133 134 /** 135 * Return filter's info for debugging and logging purpose. 136 */ 137 @Override 138 public String toString() { 139 return this.getClass().getSimpleName(); 140 } 141 142 /** 143 * Return length 0 byte array for Filters that don't require special serialization 144 */ 145 @Override 146 public byte[] toByteArray() throws IOException { 147 return new byte[0]; 148 } 149 150 /** 151 * Default implementation so that writers of custom filters aren't forced to implement. n 152 * * @return true if and only if the fields of the filter that are serialized are equal to the 153 * corresponding fields in other. Used for testing. 154 */ 155 @Override 156 boolean areSerializedFieldsEqual(Filter other) { 157 return true; 158 } 159}