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  package org.apache.hadoop.hbase.replication;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.CellUtil;
29  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.BulkLoadDescriptor;
30  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.StoreDescriptor;
31  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
32  
33  import com.google.common.base.Predicate;
34  
35  public class BulkLoadCellFilter {
36    private static final Log LOG = LogFactory.getLog(BulkLoadCellFilter.class);
37  
38    /**
39     * Filters the bulk load cell using the supplied predicate.
40     * @param cell The WAL cell to filter.
41     * @param famPredicate Returns true of given family should be removed.
42     * @return The filtered cell.
43     */
44    public Cell filterCell(Cell cell, Predicate<byte[]> famPredicate) {
45      byte[] fam;
46      BulkLoadDescriptor bld = null;
47      try {
48        bld = WALEdit.getBulkLoadDescriptor(cell);
49      } catch (IOException e) {
50        LOG.warn("Failed to get bulk load events information from the WAL file.", e);
51        return cell;
52      }
53      List<StoreDescriptor> storesList = bld.getStoresList();
54      // Copy the StoreDescriptor list and update it as storesList is a unmodifiableList
55      List<StoreDescriptor> copiedStoresList = new ArrayList<StoreDescriptor>(storesList);
56      Iterator<StoreDescriptor> copiedStoresListIterator = copiedStoresList.iterator();
57      boolean anyStoreRemoved = false;
58      while (copiedStoresListIterator.hasNext()) {
59        StoreDescriptor sd = copiedStoresListIterator.next();
60        fam = sd.getFamilyName().toByteArray();
61        if (famPredicate.apply(fam)) {
62          copiedStoresListIterator.remove();
63          anyStoreRemoved = true;
64        }
65      }
66  
67      if (!anyStoreRemoved) {
68        return cell;
69      } else if (copiedStoresList.isEmpty()) {
70        return null;
71      }
72      BulkLoadDescriptor.Builder newDesc =
73          BulkLoadDescriptor.newBuilder().setTableName(bld.getTableName())
74              .setEncodedRegionName(bld.getEncodedRegionName())
75              .setBulkloadSeqNum(bld.getBulkloadSeqNum());
76      newDesc.addAllStores(copiedStoresList);
77      BulkLoadDescriptor newBulkLoadDescriptor = newDesc.build();
78      return CellUtil.createCell(CellUtil.cloneRow(cell), WALEdit.METAFAMILY, WALEdit.BULK_LOAD,
79          cell.getTimestamp(), cell.getTypeByte(), newBulkLoadDescriptor.toByteArray());
80    }
81  }