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