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;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
027import org.apache.hadoop.hbase.wal.WAL.Entry;
028import org.apache.yetus.audience.InterfaceAudience;
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    entry = filterEntry(entry);
072    if (entry == null) {
073      return null;
074    }
075
076    filterCells(entry);
077    return entry;
078  }
079
080  protected Entry filterEntry(Entry entry) {
081    for (WALEntryFilter filter : filters) {
082      if (entry == null) {
083        return null;
084      }
085      entry = filter.filter(entry);
086    }
087    return entry;
088  }
089
090  protected void filterCells(Entry entry) {
091    if (entry == null || cellFilters.length == 0) {
092      return;
093    }
094    WALUtil.filterCells(entry.getEdit(), c -> filterCell(entry, c));
095  }
096
097  private Cell filterCell(Entry entry, Cell cell) {
098    for (WALCellFilter filter : cellFilters) {
099      cell = filter.filterCell(entry, cell);
100      if (cell == null) {
101        break;
102      }
103    }
104    return cell;
105  }
106}