@ -33,13 +33,9 @@ namespace rocksdb {
// We are using the fact that we know for Posix files the unique ID is three
// varints.
const size_t kMaxCacheKeyPrefixSize = kMaxVarint64Length * 3 + 1 ;
using std : : unique_ptr ;
struct BlockBasedTable : : Rep {
~ Rep ( ) {
delete filter ;
delete [ ] filter_data ;
delete index_block ;
}
Rep ( const EnvOptions & storage_options ) :
soptions ( storage_options ) {
}
@ -52,11 +48,16 @@ struct BlockBasedTable::Rep {
size_t cache_key_prefix_size ;
char compressed_cache_key_prefix [ kMaxCacheKeyPrefixSize ] ;
size_t compressed_cache_key_prefix_size ;
FilterBlockReader * filter ;
const char * filter_data ;
BlockHandle metaindex_handle ; // Handle to metaindex_block: saved from footer
Block * index_block ;
// Handle to metaindex_block: saved from footer
BlockHandle metaindex_handle ;
// Handle to index: saved from footer
BlockHandle index_handle ;
// index_block will be populated and used only when options.block_cache is
// NULL; otherwise we will get the index block via the block cache.
unique_ptr < Block > index_block ;
unique_ptr < FilterBlockReader > filter ;
TableStats table_stats ;
} ;
@ -64,6 +65,30 @@ BlockBasedTable::~BlockBasedTable() {
delete rep_ ;
}
// CachableEntry represents the entries that *may* be fetched from block cache.
// field `value` is the item we want to get.
// field `cache_handle` is the cache handle to the block cache. If the value
// was not read from cache, `cache_handle` will be nullptr.
template < class TValue >
struct BlockBasedTable : : CachableEntry {
CachableEntry ( TValue * value , Cache : : Handle * cache_handle )
: value ( value )
, cache_handle ( cache_handle ) {
}
CachableEntry ( ) : CachableEntry ( nullptr , nullptr ) { }
void Release ( Cache * cache ) {
if ( cache_handle ) {
cache - > Release ( cache_handle ) ;
value = nullptr ;
cache_handle = nullptr ;
}
}
TValue * value = nullptr ;
// if the entry is from the cache, cache_handle will be populated.
Cache : : Handle * cache_handle = nullptr ;
} ;
// Helper function to setup the cache key's prefix for the Table.
void BlockBasedTable : : SetupCacheKeyPrefix ( Rep * rep ) {
assert ( kMaxCacheKeyPrefixSize > = 10 ) ;
@ -116,7 +141,8 @@ namespace { // anonymous namespace, not visible externally
// Set *didIO to true if didIO is not null.
// On failure return non-OK.
// On success fill *result and return OK - caller owns *result
Status ReadBlock ( RandomAccessFile * file ,
Status ReadBlockFromFile (
RandomAccessFile * file ,
const ReadOptions & options ,
const BlockHandle & handle ,
Block * * result ,
@ -136,6 +162,62 @@ Status ReadBlock(RandomAccessFile* file,
return s ;
}
void DeleteBlock ( void * arg , void * ignored ) {
delete reinterpret_cast < Block * > ( arg ) ;
}
void DeleteCachedBlock ( const Slice & key , void * value ) {
Block * block = reinterpret_cast < Block * > ( value ) ;
delete block ;
}
void DeleteCachedFilter ( const Slice & key , void * value ) {
auto filter = reinterpret_cast < FilterBlockReader * > ( value ) ;
delete filter ;
}
void ReleaseBlock ( void * arg , void * h ) {
Cache * cache = reinterpret_cast < Cache * > ( arg ) ;
Cache : : Handle * handle = reinterpret_cast < Cache : : Handle * > ( h ) ;
cache - > Release ( handle ) ;
}
Slice GetCacheKey ( const char * cache_key_prefix ,
size_t cache_key_prefix_size ,
const BlockHandle & handle ,
char * cache_key ) {
assert ( cache_key ! = nullptr ) ;
assert ( cache_key_prefix_size ! = 0 ) ;
assert ( cache_key_prefix_size < = kMaxCacheKeyPrefixSize ) ;
memcpy ( cache_key , cache_key_prefix , cache_key_prefix_size ) ;
char * end = EncodeVarint64 ( cache_key + cache_key_prefix_size ,
handle . offset ( ) ) ;
return Slice ( cache_key , static_cast < size_t > ( end - cache_key ) ) ;
}
Cache : : Handle * GetFromBlockCache (
Cache * block_cache ,
const Slice & key ,
Tickers block_cache_miss_ticker ,
Tickers block_cache_hit_ticker ,
std : : shared_ptr < Statistics > statistics ) {
auto cache_handle = block_cache - > Lookup ( key ) ;
if ( cache_handle ! = nullptr ) {
BumpPerfCount ( & perf_context . block_cache_hit_count ) ;
// overall cache hit
RecordTick ( statistics , BLOCK_CACHE_HIT ) ;
// block-type specific cache hit
RecordTick ( statistics , block_cache_hit_ticker ) ;
} else {
// overall cache miss
RecordTick ( statistics , BLOCK_CACHE_MISS ) ;
// block-type specific cache miss
RecordTick ( statistics , block_cache_miss_ticker ) ;
}
return cache_handle ;
}
} // end of anonymous namespace
Status BlockBasedTable : : Open ( const Options & options ,
@ -164,27 +246,85 @@ Status BlockBasedTable::Open(const Options& options,
s = footer . DecodeFrom ( & footer_input ) ;
if ( ! s . ok ( ) ) return s ;
Block * index_block = nullptr ;
// TODO: we never really verify check sum for index block
s = ReadBlock ( file . get ( ) , ReadOptions ( ) , footer . index_handle ( ) , & index_block ,
options . env ) ;
if ( s . ok ( ) ) {
// We've successfully read the footer and the index block: we're
// ready to serve requests.
assert ( index_block - > compressionType ( ) = = kNoCompression ) ;
BlockBasedTable : : Rep * rep = new BlockBasedTable : : Rep ( soptions ) ;
Rep * rep = new BlockBasedTable : : Rep ( soptions ) ;
rep - > options = options ;
rep - > file = std : : move ( file ) ;
rep - > metaindex_handle = footer . metaindex_handle ( ) ;
rep - > index_block = index_block ;
rep - > index_handle = footer . index_handle ( ) ;
SetupCacheKeyPrefix ( rep ) ;
rep - > filter_data = nullptr ;
rep - > filter = nullptr ;
table_reader - > reset ( new BlockBasedTable ( rep ) ) ;
( ( BlockBasedTable * ) ( table_reader - > get ( ) ) ) - > ReadMeta ( footer ) ;
unique_ptr < BlockBasedTable > new_table ( new BlockBasedTable ( rep ) ) ;
// Read meta index
std : : unique_ptr < Block > meta ;
std : : unique_ptr < Iterator > meta_iter ;
s = ReadMetaBlock ( rep , & meta , & meta_iter ) ;
// Read the stats
meta_iter - > Seek ( kStatsBlock ) ;
if ( meta_iter - > Valid ( ) & & meta_iter - > key ( ) = = Slice ( kStatsBlock ) ) {
s = meta_iter - > status ( ) ;
if ( s . ok ( ) ) {
s = ReadStats ( meta_iter - > value ( ) , rep , & rep - > table_stats ) ;
}
if ( ! s . ok ( ) ) {
auto err_msg =
" [Warning] Encountered error while reading data from stats block " +
s . ToString ( ) ;
Log ( rep - > options . info_log , err_msg . c_str ( ) ) ;
}
}
// Initialize index/filter blocks. If block cache is not specified,
// these blocks will be kept in member variables in Rep, which will
// reside in the memory as long as this table object is alive; otherwise
// they will be added to block cache.
if ( ! options . block_cache ) {
Block * index_block = nullptr ;
// TODO: we never really verify check sum for index block
s = ReadBlockFromFile (
rep - > file . get ( ) ,
ReadOptions ( ) ,
footer . index_handle ( ) ,
& index_block ,
options . env
) ;
if ( s . ok ( ) ) {
assert ( index_block - > compressionType ( ) = = kNoCompression ) ;
rep - > index_block . reset ( index_block ) ;
// Set index block
if ( rep - > options . filter_policy ) {
std : : string key = kFilterBlockPrefix ;
key . append ( rep - > options . filter_policy - > Name ( ) ) ;
meta_iter - > Seek ( key ) ;
if ( meta_iter - > Valid ( ) & & meta_iter - > key ( ) = = Slice ( key ) ) {
rep - > filter . reset ( ReadFilter ( meta_iter - > value ( ) , rep ) ) ;
}
}
} else {
if ( index_block ) delete index_block ;
delete index_block ;
}
} else {
// Call IndexBlockReader() to implicitly add index to the block_cache
unique_ptr < Iterator > iter (
new_table - > IndexBlockReader ( ReadOptions ( ) )
) ;
s = iter - > status ( ) ;
if ( s . ok ( ) ) {
// Call GetFilter() to implicitly add filter to the block_cache
auto filter_entry = new_table - > GetFilter ( ) ;
filter_entry . Release ( options . block_cache . get ( ) ) ;
}
}
if ( s . ok ( ) ) {
* table_reader = std : : move ( new_table ) ;
}
return s ;
@ -213,72 +353,71 @@ TableStats& BlockBasedTable::GetTableStats() {
return rep_ - > table_stats ;
}
void BlockBasedTable : : ReadMeta ( const Footer & footer ) {
// Load the meta-block from the file. On success, return the loaded meta block
// and its iterator.
Status BlockBasedTable : : ReadMetaBlock (
Rep * rep ,
std : : unique_ptr < Block > * meta_block ,
std : : unique_ptr < Iterator > * iter ) {
// TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
// it is an empty block.
// TODO: we never really verify check sum for meta index block
Block * meta = nullptr ;
if ( ! ReadBlock ( rep_ - > file . get ( ) , ReadOptions ( ) , footer . metaindex_handle ( ) ,
& meta , rep_ - > options . env ) . ok ( ) ) {
// Do not propagate errors since meta info is not needed for operation
return ;
}
assert ( meta - > compressionType ( ) = = kNoCompression ) ;
Iterator * iter = meta - > NewIterator ( BytewiseComparator ( ) ) ;
// read filter
if ( rep_ - > options . filter_policy ) {
std : : string key = kFilterBlockPrefix ;
key . append ( rep_ - > options . filter_policy - > Name ( ) ) ;
iter - > Seek ( key ) ;
if ( iter - > Valid ( ) & & iter - > key ( ) = = Slice ( key ) ) {
ReadFilter ( iter - > value ( ) ) ;
}
}
// read stats
iter - > Seek ( kStatsBlock ) ;
if ( iter - > Valid ( ) & & iter - > key ( ) = = Slice ( kStatsBlock ) ) {
auto s = iter - > status ( ) ;
if ( s . ok ( ) ) {
s = ReadStats ( iter - > value ( ) , rep_ ) ;
}
Status s = ReadBlockFromFile (
rep - > file . get ( ) ,
ReadOptions ( ) ,
rep - > metaindex_handle ,
& meta ,
rep - > options . env ) ;
if ( ! s . ok ( ) ) {
auto err_msg =
" [Warning] Encountered error while reading data from stats block " +
s . ToString ( ) ;
Log ( rep_ - > options . info_log , " %s " , err_msg . c_str ( ) ) ;
Log ( rep - > options . info_log , " %s " , err_msg . c_str ( ) ) ;
}
if ( ! s . ok ( ) ) {
delete meta ;
return s ;
}
delete iter ;
delete meta ;
meta_block - > reset ( meta ) ;
// meta block uses bytewise comparator.
iter - > reset ( meta - > NewIterator ( BytewiseComparator ( ) ) ) ;
return Status : : OK ( ) ;
}
void BlockBasedTable : : ReadFilter ( const Slice & filter_handle_value ) {
FilterBlockReader * BlockBasedTable : : ReadFilter (
const Slice & filter_handle_value ,
BlockBasedTable : : Rep * rep ,
size_t * filter_size ) {
Slice v = filter_handle_value ;
BlockHandle filter_handle ;
if ( ! filter_handle . DecodeFrom ( & v ) . ok ( ) ) {
return ;
return nullptr ;
}
// TODO: We might want to unify with ReadBlock() if we start
// requiring checksum verification in BlockBased Table::Open.
// TODO: We might want to unify with ReadBlockFromFile () if we start
// requiring checksum verification in Table::Open.
ReadOptions opt ;
BlockContents block ;
if ( ! ReadBlockContents ( rep_ - > file . get ( ) , opt , filter_handle , & block ,
rep_ - > options . env , false ) . ok ( ) ) {
return ;
if ( ! ReadBlockContents ( rep - > file . get ( ) , opt , filter_handle , & block ,
rep - > options . env , false ) . ok ( ) ) {
return nullptr ;
}
if ( block . heap_allocated ) {
rep_ - > filter_data = block . data . data ( ) ; // Will need to delete later
if ( filter_size ) {
* filter_size = block . data . size ( ) ;
}
rep_ - > filter = new FilterBlockReader ( rep_ - > options , block . data ) ;
return new FilterBlockReader (
rep - > options , block . data , block . heap_allocated ) ;
}
Status BlockBasedTable : : ReadStats ( const Slice & handle_value , Rep * rep ) {
Status BlockBasedTable : : ReadStats (
const Slice & handle_value , Rep * rep , TableStats * table_stats ) {
assert ( table_stats ) ;
Slice v = handle_value ;
BlockHandle handle ;
if ( ! handle . DecodeFrom ( & v ) . ok ( ) ) {
@ -304,15 +443,15 @@ Status BlockBasedTable::ReadStats(const Slice& handle_value, Rep* rep) {
stats_block . NewIterator ( BytewiseComparator ( ) )
) ;
auto & table_stats = rep - > table_stats ;
// All pre-defined stats of type uint64_t
std : : unordered_map < std : : string , uint64_t * > predefined_uint64_stats = {
{ BlockBasedTableStatsNames : : kDataSize , & table_stats . data_size } ,
{ BlockBasedTableStatsNames : : kIndexSize , & table_stats . index_size } ,
{ BlockBasedTableStatsNames : : kRawKeySize , & table_stats . raw_key_size } ,
{ BlockBasedTableStatsNames : : kRawValueSize , & table_stats . raw_value_size } ,
{ BlockBasedTableStatsNames : : kNumDataBlocks , & table_stats . num_data_blocks } ,
{ BlockBasedTableStatsNames : : kNumEntries , & table_stats . num_entries } ,
{ BlockBasedTableStatsNames : : kDataSize , & table_stats - > data_size } ,
{ BlockBasedTableStatsNames : : kIndexSize , & table_stats - > index_size } ,
{ BlockBasedTableStatsNames : : kRawKeySize , & table_stats - > raw_key_size } ,
{ BlockBasedTableStatsNames : : kRawValueSize , & table_stats - > raw_value_size } ,
{ BlockBasedTableStatsNames : : kNumDataBlocks ,
& table_stats - > num_data_blocks } ,
{ BlockBasedTableStatsNames : : kNumEntries , & table_stats - > num_entries } ,
} ;
std : : string last_key ;
@ -346,11 +485,11 @@ Status BlockBasedTable::ReadStats(const Slice& handle_value, Rep* rep) {
}
* ( pos - > second ) = val ;
} else if ( key = = BlockBasedTableStatsNames : : kFilterPolicy ) {
table_stats . filter_policy_name = raw_val . ToString ( ) ;
table_stats - > filter_policy_name = raw_val . ToString ( ) ;
} else {
// handle user-collected
table_stats . user_collected_stats . insert (
std : : make_pair ( iter - > key ( ) . ToString ( ) , raw_val . ToString ( ) )
table_stats - > user_collected_stats . insert (
std : : make_pair ( key , raw_val . ToString ( ) )
) ;
}
}
@ -358,19 +497,81 @@ Status BlockBasedTable::ReadStats(const Slice& handle_value, Rep* rep) {
return s ;
}
static void DeleteBlock ( void * arg , void * ignored ) {
delete reinterpret_cast < Block * > ( arg ) ;
}
Status BlockBasedTable : : GetBlock (
const BlockBasedTable * table ,
const BlockHandle & handle ,
const ReadOptions & options ,
const bool for_compaction ,
const Tickers block_cache_miss_ticker ,
const Tickers block_cache_hit_ticker ,
bool * didIO ,
CachableEntry < Block > * entry ) {
bool no_io = options . read_tier = = kBlockCacheTier ;
Cache * block_cache = table - > rep_ - > options . block_cache . get ( ) ;
auto statistics = table - > rep_ - > options . statistics ;
Status s ;
static void DeleteCachedBlock ( const Slice & key , void * value ) {
Block * block = reinterpret_cast < Block * > ( value ) ;
delete block ;
}
if ( block_cache ! = nullptr ) {
char cache_key [ kMaxCacheKeyPrefixSize + kMaxVarint64Length ] ;
auto key = GetCacheKey (
table - > rep_ - > cache_key_prefix ,
table - > rep_ - > cache_key_prefix_size ,
handle ,
cache_key
) ;
static void ReleaseBlock ( void * arg , void * h ) {
Cache * cache = reinterpret_cast < Cache * > ( arg ) ;
Cache : : Handle * handle = reinterpret_cast < Cache : : Handle * > ( h ) ;
cache - > Release ( handle ) ;
entry - > cache_handle = GetFromBlockCache (
block_cache ,
key ,
block_cache_miss_ticker ,
block_cache_hit_ticker ,
table - > rep_ - > options . statistics
) ;
if ( entry - > cache_handle ! = nullptr ) {
entry - > value =
reinterpret_cast < Block * > ( block_cache - > Value ( entry - > cache_handle ) ) ;
} else if ( no_io ) {
// Did not find in block_cache and can't do IO
return Status : : Incomplete ( " no blocking io " ) ;
} else {
Histograms histogram = for_compaction ?
READ_BLOCK_COMPACTION_MICROS : READ_BLOCK_GET_MICROS ;
{
// block for stop watch
StopWatch sw ( table - > rep_ - > options . env , statistics , histogram ) ;
s = ReadBlockFromFile (
table - > rep_ - > file . get ( ) ,
options ,
handle ,
& entry - > value ,
table - > rep_ - > options . env ,
didIO
) ;
}
if ( s . ok ( ) ) {
if ( options . fill_cache & & entry - > value - > isCachable ( ) ) {
entry - > cache_handle = block_cache - > Insert (
key , entry - > value , entry - > value - > size ( ) , & DeleteCachedBlock ) ;
RecordTick ( statistics , BLOCK_CACHE_ADD ) ;
}
}
}
} else if ( no_io ) {
// Could not read from block_cache and can't do IO
return Status : : Incomplete ( " no blocking io " ) ;
} else {
s = ReadBlockFromFile (
table - > rep_ - > file . get ( ) ,
options ,
handle ,
& entry - > value ,
table - > rep_ - > options . env ,
didIO
) ;
}
return s ;
}
// Convert an index iterator value (i.e., an encoded BlockHandle)
@ -397,49 +598,52 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
// We intentionally allow extra stuff in index_value so that we
// can add more features in the future.
if ( s . ok ( ) ) {
if ( ! s . ok ( ) ) {
return NewErrorIterator ( s ) ;
}
if ( block_cache ! = nullptr | | block_cache_compressed ! = nullptr ) {
char cache_key [ kMaxCacheKeyPrefixSize + kMaxVarint64Length ] ;
char compressed_cache_key [ kMaxCacheKeyPrefixSize + kMaxVarint64Length ] ;
char * end = cache_key ;
Slice key , /* key to the block cache */
ckey /* key to the compressed block cache */ ;
// create key for block cache
if ( block_cache ! = nullptr ) {
assert ( table - > rep_ - > cache_key_prefix_size ! = 0 ) ;
assert ( table - > rep_ - > cache_key_prefix_size < = kMaxCacheKeyPrefixSize ) ;
memcpy ( cache_key , table - > rep_ - > cache_key_prefix ,
table - > rep_ - > cache_key_prefix_size ) ;
end = EncodeVarint64 ( cache_key + table - > rep_ - > cache_key_prefix_size ,
handle . offset ( ) ) ;
key = GetCacheKey (
table - > rep_ - > cache_key_prefix ,
table - > rep_ - > cache_key_prefix_size ,
handle ,
cache_key
) ;
}
Slice key ( cache_key , static_cast < size_t > ( end - cache_key ) ) ;
// create key for compressed block cache
end = compressed_cache_key ;
if ( block_cache_compressed ! = nullptr ) {
assert ( table - > rep_ - > compressed_cache_key_prefix_size ! = 0 ) ;
assert ( table - > rep_ - > compressed_cache_key_prefix_size < =
kMaxCacheKeyPrefixSize ) ;
memcpy ( compressed_cache_key , table - > rep_ - > compressed_cache_key_prefix ,
table - > rep_ - > compressed_cache_key_prefix_size ) ;
end = EncodeVarint64 ( compressed_cache_key +
ckey = GetCacheKey (
table - > rep_ - > compressed_cache_key_prefix ,
table - > rep_ - > compressed_cache_key_prefix_size ,
handle . offset ( ) ) ;
handle ,
compressed_cache_key
) ;
}
Slice ckey ( compressed_cache_key , static_cast < size_t >
( end - compressed_cache_key ) ) ;
// Lookup uncompressed cache first
if ( block_cache ! = nullptr ) {
assert ( ! key . empty ( ) ) ;
cache_handle = block_cache - > Lookup ( key ) ;
if ( cache_handle ! = nullptr ) {
block = reinterpret_cast < Block * > ( block_cache - > Value ( cache_handle ) ) ;
RecordTick ( statistics , BLOCK_CACHE_HIT ) ;
RecordTick ( statistics , BLOCK_CACHE_DATA_HIT ) ;
} else {
RecordTick ( statistics , BLOCK_CACHE_MISS ) ;
RecordTick ( statistics , BLOCK_CACHE_DATA_MISS ) ;
}
}
// If not found in uncompressed cache, lookup compressed cache
if ( block = = nullptr & & block_cache_compressed ! = nullptr ) {
assert ( ! ckey . empty ( ) ) ;
compressed_cache_handle = block_cache_compressed - > Lookup ( ckey ) ;
// if we found in the compressed cache, then uncompress and
@ -479,12 +683,11 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
// Did not find in block_cache and can't do IO
return NewErrorIterator ( Status : : Incomplete ( " no blocking io " ) ) ;
} else {
Histograms histogram = for_compaction ?
READ_BLOCK_COMPACTION_MICROS : READ_BLOCK_GET_MICROS ;
{ // block for stop watch
StopWatch sw ( table - > rep_ - > options . env , statistics , histogram ) ;
s = ReadBlock (
s = ReadBlockFromFile (
table - > rep_ - > file . get ( ) ,
options ,
handle ,
@ -526,7 +729,7 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
if ( block_cache ! = nullptr ) {
cache_handle = block_cache - > Insert (
key , block , block - > size ( ) , & DeleteCachedBlock ) ;
RecordTick ( statistics , BLOCK_CACHE_MISS ) ;
RecordTick ( statistics , BLOCK_CACHE_ADD ) ;
assert ( reinterpret_cast < Block * > ( block_cache - > Value (
cache_handle ) ) = = block ) ;
}
@ -541,9 +744,14 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
// Could not read from block_cache and can't do IO
return NewErrorIterator ( Status : : Incomplete ( " no blocking io " ) ) ;
} else {
s = ReadBlock ( table - > rep_ - > file . get ( ) , options , handle , & block ,
table - > rep_ - > options . env , didIO ) ;
}
s = ReadBlockFromFile (
table - > rep_ - > file . get ( ) ,
options ,
handle ,
& block ,
table - > rep_ - > options . env ,
didIO
) ;
}
Iterator * iter ;
@ -560,6 +768,102 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
return iter ;
}
BlockBasedTable : : CachableEntry < FilterBlockReader >
BlockBasedTable : : GetFilter ( bool no_io ) const {
if ( ! rep_ - > options . filter_policy | | ! rep_ - > options . block_cache ) {
return { rep_ - > filter . get ( ) , nullptr } ;
}
// Fetching from the cache
Cache * block_cache = rep_ - > options . block_cache . get ( ) ;
char cache_key [ kMaxCacheKeyPrefixSize + kMaxVarint64Length ] ;
auto key = GetCacheKey (
rep_ - > cache_key_prefix ,
rep_ - > cache_key_prefix_size ,
rep_ - > metaindex_handle ,
cache_key
) ;
auto cache_handle = GetFromBlockCache (
block_cache ,
key ,
BLOCK_CACHE_FILTER_MISS ,
BLOCK_CACHE_FILTER_HIT ,
rep_ - > options . statistics
) ;
FilterBlockReader * filter = nullptr ;
if ( cache_handle ! = nullptr ) {
filter = reinterpret_cast < FilterBlockReader * > (
block_cache - > Value ( cache_handle ) ) ;
} else if ( no_io ) {
// Do not invoke any io.
return CachableEntry < FilterBlockReader > ( ) ;
} else {
size_t filter_size = 0 ;
std : : unique_ptr < Block > meta ;
std : : unique_ptr < Iterator > iter ;
auto s = ReadMetaBlock ( rep_ , & meta , & iter ) ;
if ( s . ok ( ) ) {
std : : string filter_block_key = kFilterBlockPrefix ;
filter_block_key . append ( rep_ - > options . filter_policy - > Name ( ) ) ;
iter - > Seek ( filter_block_key ) ;
if ( iter - > Valid ( ) & & iter - > key ( ) = = Slice ( filter_block_key ) ) {
filter = ReadFilter ( iter - > value ( ) , rep_ , & filter_size ) ;
assert ( filter ) ;
assert ( filter_size > 0 ) ;
cache_handle = block_cache - > Insert (
key , filter , filter_size , & DeleteCachedFilter ) ;
RecordTick ( rep_ - > options . statistics , BLOCK_CACHE_ADD ) ;
}
}
}
return { filter , cache_handle } ;
}
// Get the iterator from the index block.
Iterator * BlockBasedTable : : IndexBlockReader ( const ReadOptions & options ) const {
if ( rep_ - > index_block ) {
assert ( ! rep_ - > options . block_cache ) ;
return rep_ - > index_block - > NewIterator ( rep_ - > options . comparator ) ;
}
// get index block from cache
assert ( rep_ - > options . block_cache ) ;
bool didIO = false ;
CachableEntry < Block > entry ;
auto s = GetBlock (
this ,
rep_ - > index_handle ,
options ,
false , /* for compaction */
BLOCK_CACHE_INDEX_MISS ,
BLOCK_CACHE_INDEX_HIT ,
& didIO ,
& entry
) ;
Iterator * iter ;
if ( entry . value ! = nullptr ) {
iter = entry . value - > NewIterator ( rep_ - > options . comparator ) ;
if ( entry . cache_handle ) {
iter - > RegisterCleanup (
& ReleaseBlock , rep_ - > options . block_cache . get ( ) , entry . cache_handle
) ;
} else {
iter - > RegisterCleanup ( & DeleteBlock , entry . value , nullptr ) ;
}
} else {
iter = NewErrorIterator ( s ) ;
}
return iter ;
}
Iterator * BlockBasedTable : : BlockReader ( void * arg ,
const ReadOptions & options ,
const EnvOptions & soptions ,
@ -577,22 +881,27 @@ Iterator* BlockBasedTable::BlockReader(void* arg,
// 2) Compare(prefix(key), key) <= 0.
// 3) If Compare(key1, key2) <= 0, then Compare(prefix(key1), prefix(key2)) <= 0
//
// TODO(tylerharter): right now, this won't cause I/O since blooms are
// in memory. When blooms may need to be paged in, we should refactor so that
// this is only ever called lazily. In particular, this shouldn't be called
// while the DB lock is held like it is now.
// Otherwise, this method guarantees no I/O will be incurred.
//
// REQUIRES: this method shouldn't be called while the DB lock is held.
bool BlockBasedTable : : PrefixMayMatch ( const Slice & internal_prefix ) {
FilterBlockReader * filter = rep_ - > filter ;
bool may_match = true ;
Status s ;
if ( filter = = nullptr ) {
if ( ! rep_ - > options . filter_policy ) {
return true ;
}
std : : unique_ptr < Iterator > iiter ( rep_ - > index_block - > NewIterator (
rep_ - > options . comparator ) ) ;
// To prevent any io operation in this method, we set `read_tier` to make
// sure we always read index or filter only when they have already been
// loaded to memory.
ReadOptions no_io_read_options ;
no_io_read_options . read_tier = kBlockCacheTier ;
unique_ptr < Iterator > iiter (
IndexBlockReader ( no_io_read_options )
) ;
iiter - > Seek ( internal_prefix ) ;
if ( ! iiter - > Valid ( ) ) {
// we're past end of file
may_match = false ;
@ -619,7 +928,11 @@ bool BlockBasedTable::PrefixMayMatch(const Slice& internal_prefix) {
BlockHandle handle ;
s = handle . DecodeFrom ( & handle_value ) ;
assert ( s . ok ( ) ) ;
may_match = filter - > PrefixMayMatch ( handle . offset ( ) , internal_prefix ) ;
auto filter_entry = GetFilter ( true /* no io */ ) ;
may_match =
filter_entry . value ! = nullptr & &
filter_entry . value - > PrefixMayMatch ( handle . offset ( ) , internal_prefix ) ;
filter_entry . Release ( rep_ - > options . block_cache . get ( ) ) ;
}
RecordTick ( rep_ - > options . statistics , BLOOM_FILTER_PREFIX_CHECKED ) ;
@ -641,9 +954,12 @@ Iterator* BlockBasedTable::NewIterator(const ReadOptions& options) {
}
return NewTwoLevelIterator (
rep_ - > index_block - > NewIterator ( rep_ - > options . comparator ) ,
& BlockBasedTable : : BlockReader , const_cast < BlockBasedTable * > ( this ) ,
options , rep_ - > soptions ) ;
IndexBlockReader ( options ) ,
& BlockBasedTable : : BlockReader ,
const_cast < BlockBasedTable * > ( this ) ,
options ,
rep_ - > soptions
) ;
}
Status BlockBasedTable : : Get (
@ -654,15 +970,20 @@ Status BlockBasedTable::Get(
const Slice & v , bool didIO ) ,
void ( * mark_key_may_exist_handler ) ( void * handle_context ) ) {
Status s ;
Iterator * iiter = rep_ - > index_block - > NewIterator ( rep_ - > options . comparator ) ;
Iterator * iiter = IndexBlockReader ( readOptions ) ;
auto filter_entry = GetFilter ( readOptions . read_tier = = kBlockCacheTier ) ;
FilterBlockReader * filter = filter_entry . value ;
bool done = false ;
for ( iiter - > Seek ( key ) ; iiter - > Valid ( ) & & ! done ; iiter - > Next ( ) ) {
Slice handle_value = iiter - > value ( ) ;
FilterBlockReader * filter = rep_ - > filter ;
BlockHandle handle ;
if ( filter ! = nullptr & &
bool may_not_exist_in_filter =
filter ! = nullptr & &
handle . DecodeFrom ( & handle_value ) . ok ( ) & &
! filter - > KeyMayMatch ( handle . offset ( ) , key ) ) {
! filter - > KeyMayMatch ( handle . offset ( ) , key ) ;
if ( may_not_exist_in_filter ) {
// Not found
// TODO: think about interaction with Merge. If a user key cannot
// cross one data block, we should be fine.
@ -670,7 +991,7 @@ Status BlockBasedTable::Get(
break ;
} else {
bool didIO = false ;
std : : unique_ptr < Iterator > block_iter (
unique_ptr < Iterator > block_iter (
BlockReader ( this , readOptions , iiter - > value ( ) , & didIO ) ) ;
if ( readOptions . read_tier & & block_iter - > status ( ) . IsIncomplete ( ) ) {
@ -692,6 +1013,8 @@ Status BlockBasedTable::Get(
s = block_iter - > status ( ) ;
}
}
filter_entry . Release ( rep_ - > options . block_cache . get ( ) ) ;
if ( s . ok ( ) ) {
s = iiter - > status ( ) ;
}
@ -714,8 +1037,8 @@ bool BlockBasedTable::TEST_KeyInCache(const ReadOptions& options,
}
uint64_t BlockBasedTable : : ApproximateOffsetOf ( const Slice & key ) {
Iterator * index_iter =
rep_ - > index_block - > NewIterator ( rep_ - > options . comparator ) ;
Iterator * index_iter = IndexBlockReader ( ReadOptions ( ) ) ;
index_iter - > Seek ( key ) ;
uint64_t result ;
if ( index_iter - > Valid ( ) ) {