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