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 */ 018 019package org.apache.hadoop.hbase.replication; 020 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024 025import org.apache.yetus.audience.InterfaceAudience; 026import org.apache.hadoop.hbase.Cell; 027import org.apache.hadoop.hbase.HBaseInterfaceAudience; 028import org.apache.hadoop.hbase.wal.WAL.Entry; 029 030/** 031 * A {@link WALEntryFilter} which contains multiple filters and applies them 032 * in chain order 033 */ 034@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION) 035public class ChainWALEntryFilter implements WALEntryFilter { 036 037 private final WALEntryFilter[] filters; 038 private WALCellFilter[] cellFilters; 039 040 public ChainWALEntryFilter(WALEntryFilter...filters) { 041 this.filters = filters; 042 initCellFilters(); 043 } 044 045 public ChainWALEntryFilter(List<WALEntryFilter> filters) { 046 ArrayList<WALEntryFilter> rawFilters = new ArrayList<>(filters.size()); 047 // flatten the chains 048 for (WALEntryFilter filter : filters) { 049 if (filter instanceof ChainWALEntryFilter) { 050 Collections.addAll(rawFilters, ((ChainWALEntryFilter) filter).filters); 051 } else { 052 rawFilters.add(filter); 053 } 054 } 055 this.filters = rawFilters.toArray(new WALEntryFilter[rawFilters.size()]); 056 initCellFilters(); 057 } 058 059 public void initCellFilters() { 060 ArrayList<WALCellFilter> cellFilters = new ArrayList<>(filters.length); 061 for (WALEntryFilter filter : filters) { 062 if (filter instanceof WALCellFilter) { 063 cellFilters.add((WALCellFilter) filter); 064 } 065 } 066 this.cellFilters = cellFilters.toArray(new WALCellFilter[cellFilters.size()]); 067 } 068 069 @Override 070 public Entry filter(Entry entry) { 071 for (WALEntryFilter filter : filters) { 072 if (entry == null) { 073 return null; 074 } 075 entry = filter.filter(entry); 076 } 077 filterCells(entry); 078 return entry; 079 } 080 081 private void filterCells(Entry entry) { 082 if (entry == null || cellFilters.length == 0) { 083 return; 084 } 085 ArrayList<Cell> cells = entry.getEdit().getCells(); 086 int size = cells.size(); 087 for (int i = size - 1; i >= 0; i--) { 088 Cell cell = cells.get(i); 089 for (WALCellFilter filter : cellFilters) { 090 cell = filter.filterCell(entry, cell); 091 if (cell != null) { 092 cells.set(i, cell); 093 } else { 094 cells.remove(i); 095 break; 096 } 097 } 098 } 099 if (cells.size() < size / 2) { 100 cells.trimToSize(); 101 } 102 } 103}