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 */ 018package org.apache.hadoop.hbase.util; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.Cell; 022import org.apache.hadoop.hbase.CellComparator; 023import org.apache.hadoop.hbase.ExtendedCell; 024import org.apache.hadoop.hbase.io.hfile.HFile; 025import org.apache.yetus.audience.InterfaceAudience; 026 027/** 028 * The bloom context that is used by the StorefileWriter to add the bloom details per cell 029 */ 030@InterfaceAudience.Private 031public abstract class BloomContext { 032 033 protected BloomFilterWriter bloomFilterWriter; 034 protected CellComparator comparator; 035 036 public BloomContext(BloomFilterWriter bloomFilterWriter, CellComparator comparator) { 037 this.bloomFilterWriter = bloomFilterWriter; 038 this.comparator = comparator; 039 } 040 041 public Cell getLastCell() { 042 return this.bloomFilterWriter.getPrevCell(); 043 } 044 045 /** 046 * Bloom information from the cell is retrieved 047 */ 048 public void writeBloom(ExtendedCell cell) throws IOException { 049 // only add to the bloom filter on a new, unique key 050 if (isNewKey(cell)) { 051 sanityCheck(cell); 052 bloomFilterWriter.append(cell); 053 } 054 } 055 056 private void sanityCheck(ExtendedCell cell) throws IOException { 057 if (this.getLastCell() != null) { 058 if (comparator.compare(cell, this.getLastCell()) <= 0) { 059 throw new IOException("Added a key not lexically larger than" + " previous. Current cell = " 060 + cell + ", prevCell = " + this.getLastCell()); 061 } 062 } 063 } 064 065 /** 066 * Adds the last bloom key to the HFile Writer as part of StorefileWriter close. 067 */ 068 public abstract void addLastBloomKey(HFile.Writer writer) throws IOException; 069 070 /** 071 * Returns true if the cell is a new key as per the bloom type 072 * @param cell the cell to be verified 073 * @return true if a new key else false 074 */ 075 protected abstract boolean isNewKey(ExtendedCell cell); 076}