View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.replication;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
28  import org.apache.hadoop.hbase.wal.WAL.Entry;
29  
30  /**
31   * A {@link WALEntryFilter} which contains multiple filters and applies them
32   * in chain order
33   */
34  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
35  public class ChainWALEntryFilter implements WALEntryFilter {
36  
37    private final WALEntryFilter[] filters;
38    private WALCellFilter[] cellFilters;
39  
40    public ChainWALEntryFilter(WALEntryFilter...filters) {
41      this.filters = filters;
42      initCellFilters();
43    }
44  
45    public ChainWALEntryFilter(List<WALEntryFilter> filters) {
46      ArrayList<WALEntryFilter> rawFilters = new ArrayList<WALEntryFilter>(filters.size());
47      // flatten the chains
48      for (WALEntryFilter filter : filters) {
49        if (filter instanceof ChainWALEntryFilter) {
50          Collections.addAll(rawFilters, ((ChainWALEntryFilter) filter).filters);
51        } else {
52          rawFilters.add(filter);
53        }
54      }
55      this.filters = rawFilters.toArray(new WALEntryFilter[rawFilters.size()]);
56      initCellFilters();
57    }
58  
59    public void initCellFilters() {
60      ArrayList<WALCellFilter> cellFilters = new ArrayList<>(filters.length);
61      for (WALEntryFilter filter : filters) {
62        if (filter instanceof WALCellFilter) {
63          cellFilters.add((WALCellFilter) filter);
64        }
65      }
66      this.cellFilters = cellFilters.toArray(new WALCellFilter[cellFilters.size()]);
67    }
68  
69    @Override
70    public Entry filter(Entry entry) {
71      for (WALEntryFilter filter : filters) {
72        if (entry == null) {
73          return null;
74        }
75        entry = filter.filter(entry);
76      }
77      filterCells(entry);
78      return entry;
79    }
80  
81    private void filterCells(Entry entry) {
82      if (entry == null || cellFilters.length == 0) {
83        return;
84      }
85      ArrayList<Cell> cells = entry.getEdit().getCells();
86      int size = cells.size();
87      for (int i = size - 1; i >= 0; i--) {
88        Cell cell = cells.get(i);
89        for (WALCellFilter filter : cellFilters) {
90          cell = filter.filterCell(entry, cell);
91          if (cell != null) {
92            cells.set(i, cell);
93          } else {
94            cells.remove(i);
95            break;
96          }
97        }
98      }
99      if (cells.size() < size / 2) {
100       cells.trimToSize();
101     }
102   }
103 }