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.NavigableMap;
022
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.wal.WALEdit;
028import org.apache.hadoop.hbase.wal.WAL.Entry;
029
030import org.apache.hbase.thirdparty.com.google.common.base.Predicate;
031
032/**
033 * Keeps KVs that are scoped other than local
034 */
035@InterfaceAudience.Private
036public class ScopeWALEntryFilter implements WALEntryFilter, WALCellFilter {
037
038  BulkLoadCellFilter bulkLoadFilter = new BulkLoadCellFilter();
039
040  @Override
041  public Entry filter(Entry entry) {
042    NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
043    if (scopes == null || scopes.isEmpty()) {
044      return null;
045    }
046    return entry;
047  }
048
049  @Override
050  public Cell filterCell(Entry entry, Cell cell) {
051    final NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
052      // The scope will be null or empty if
053      // there's nothing to replicate in that WALEdit
054      byte[] fam = CellUtil.cloneFamily(cell);
055      if (CellUtil.matchingColumn(cell, WALEdit.METAFAMILY, WALEdit.BULK_LOAD)) {
056        cell = bulkLoadFilter.filterCell(cell, new Predicate<byte[]>() {
057          @Override
058          public boolean apply(byte[] fam) {
059            return !scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL;
060          }
061        });
062      } else {
063        if (!scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL) {
064          return null;
065        }
066      }
067    return cell;
068  }
069}