@ -208,8 +208,7 @@ class BlockIter : public InternalIterator {
public :
void InitializeBase ( const Comparator * comparator , const char * data ,
uint32_t restarts , uint32_t num_restarts ,
SequenceNumber global_seqno , bool key_includes_seq ,
bool block_contents_pinned ) {
SequenceNumber global_seqno , bool block_contents_pinned ) {
assert ( data_ = = nullptr ) ; // Ensure it is called only once
assert ( num_restarts > 0 ) ; // Ensure the param is valid
@ -220,13 +219,12 @@ class BlockIter : public InternalIterator {
current_ = restarts_ ;
restart_index_ = num_restarts_ ;
global_seqno_ = global_seqno ;
key_includes_seq_ = key_includes_seq ;
block_contents_pinned_ = block_contents_pinned ;
}
// Makes Valid() return false, status() return `s`, and Seek()/Prev()/etc do
// nothing. Calls cleanup functions.
void Invalidate ( Status s ) {
void InvalidateBase ( Status s ) {
// Assert that the BlockIter is never deleted while Pinning is Enabled.
assert ( ! pinned_iters_mgr_ | |
( pinned_iters_mgr_ & & ! pinned_iters_mgr_ - > PinningEnabled ( ) ) ) ;
@ -237,32 +235,19 @@ class BlockIter : public InternalIterator {
// Call cleanup callbacks.
Cleanable : : Reset ( ) ;
// Clear prev entries cache.
prev_entries_keys_buff_ . clear ( ) ;
prev_entries_ . clear ( ) ;
prev_entries_idx_ = - 1 ;
}
virtual bool Valid ( ) const override { return current_ < restarts_ ; }
virtual Status status ( ) const override { return status_ ; }
virtual Slice key ( ) const override {
assert ( Valid ( ) ) ;
return key_ . GetInternal Key ( ) ;
return key_ . GetKey ( ) ;
}
virtual Slice value ( ) const override {
assert ( Valid ( ) ) ;
return value_ ;
}
virtual void Next ( ) override ;
virtual void Prev ( ) override ;
virtual void SeekToFirst ( ) override ;
virtual void SeekToLast ( ) override ;
# ifndef NDEBUG
virtual ~ BlockIter ( ) {
// Assert that the BlockIter is never deleted while Pinning is Enabled.
@ -305,34 +290,8 @@ class BlockIter : public InternalIterator {
bool key_pinned_ ;
// whether the block data is guaranteed to outlive this iterator
bool block_contents_pinned_ ;
// Key is in InternalKey format
bool key_includes_seq_ ;
SequenceNumber global_seqno_ ;
struct CachedPrevEntry {
explicit CachedPrevEntry ( uint32_t _offset , const char * _key_ptr ,
size_t _key_offset , size_t _key_size , Slice _value )
: offset ( _offset ) ,
key_ptr ( _key_ptr ) ,
key_offset ( _key_offset ) ,
key_size ( _key_size ) ,
value ( _value ) { }
// offset of entry in block
uint32_t offset ;
// Pointer to key data in block (nullptr if key is delta-encoded)
const char * key_ptr ;
// offset of key in prev_entries_keys_buff_ (0 if key_ptr is not nullptr)
size_t key_offset ;
// size of key
size_t key_size ;
// value slice pointing to data in block
Slice value ;
} ;
std : : string prev_entries_keys_buff_ ;
std : : vector < CachedPrevEntry > prev_entries_ ;
int32_t prev_entries_idx_ = - 1 ;
public :
// Return the offset in data_ just past the end of the current entry.
inline uint32_t NextEntryOffset ( ) const {
@ -357,7 +316,6 @@ class BlockIter : public InternalIterator {
void CorruptionError ( ) ;
bool ParseNextKey ( ) ;
bool BinarySeek ( const Slice & target , uint32_t left , uint32_t right ,
uint32_t * index , const Comparator * comp ) ;
} ;
@ -380,9 +338,9 @@ class DataBlockIter final : public BlockIter {
SequenceNumber global_seqno ,
BlockReadAmpBitmap * read_amp_bitmap ,
bool block_contents_pinned ) {
const bool kKeyIncludesSeq = true ;
InitializeBase ( comparator , data , restarts , num_restarts , global_seqno ,
kKeyIncludesSeq , block_contents_pinned ) ;
block_contents_pinned ) ;
key_ . SetIsUserKey ( false ) ;
read_amp_bitmap_ = read_amp_bitmap ;
last_bitmap_offset_ = current_ + 1 ;
}
@ -402,11 +360,52 @@ class DataBlockIter final : public BlockIter {
virtual void SeekForPrev ( const Slice & target ) override ;
virtual void Prev ( ) override ;
virtual void Next ( ) override ;
virtual void SeekToFirst ( ) override ;
virtual void SeekToLast ( ) override ;
void Invalidate ( Status s ) {
InvalidateBase ( s ) ;
// Clear prev entries cache.
prev_entries_keys_buff_ . clear ( ) ;
prev_entries_ . clear ( ) ;
prev_entries_idx_ = - 1 ;
}
private :
// read-amp bitmap
BlockReadAmpBitmap * read_amp_bitmap_ ;
// last `current_` value we report to read-amp bitmp
mutable uint32_t last_bitmap_offset_ ;
struct CachedPrevEntry {
explicit CachedPrevEntry ( uint32_t _offset , const char * _key_ptr ,
size_t _key_offset , size_t _key_size , Slice _value )
: offset ( _offset ) ,
key_ptr ( _key_ptr ) ,
key_offset ( _key_offset ) ,
key_size ( _key_size ) ,
value ( _value ) { }
// offset of entry in block
uint32_t offset ;
// Pointer to key data in block (nullptr if key is delta-encoded)
const char * key_ptr ;
// offset of key in prev_entries_keys_buff_ (0 if key_ptr is not nullptr)
size_t key_offset ;
// size of key
size_t key_size ;
// value slice pointing to data in block
Slice value ;
} ;
std : : string prev_entries_keys_buff_ ;
std : : vector < CachedPrevEntry > prev_entries_ ;
int32_t prev_entries_idx_ = - 1 ;
bool ParseNextDataKey ( ) ;
inline int Compare ( const IterKey & ikey , const Slice & b ) const {
return comparator_ - > Compare ( ikey . GetInternalKey ( ) , b ) ;
@ -419,7 +418,7 @@ class IndexBlockIter final : public BlockIter {
virtual Slice key ( ) const override {
assert ( Valid ( ) ) ;
return key_includes_seq_ ? key_ . GetInternalKey ( ) : key_ . GetUser Key ( ) ;
return key_ . GetKey ( ) ;
}
IndexBlockIter ( const Comparator * comparator ,
const Comparator * user_comparator , const char * data ,
@ -436,10 +435,11 @@ class IndexBlockIter final : public BlockIter {
uint32_t restarts , uint32_t num_restarts ,
BlockPrefixIndex * prefix_index , bool key_includes_seq ,
bool block_contents_pinned ) {
user_comparator_ = user_comparator ;
InitializeBase ( comparator , data , restarts , num_restarts ,
kDisableGlobalSequenceNumber , key_includes_seq ,
block_contents_pinned ) ;
kDisableGlobalSequenceNumber , block_contents_pinned ) ;
key_includes_seq_ = key_includes_seq ;
active_comparator_ = key_includes_seq_ ? comparator_ : user_comparator ;
key_ . SetIsUserKey ( ! key_includes_seq_ ) ;
prefix_index_ = prefix_index ;
}
@ -450,12 +450,22 @@ class IndexBlockIter final : public BlockIter {
current_ = restarts_ ;
restart_index_ = num_restarts_ ;
status_ = Status : : InvalidArgument (
" RocksDB internal error: should never all SeekForPrev() for index "
" RocksDB internal error: should never call SeekForPrev() on index "
" blocks " ) ;
key_ . Clear ( ) ;
value_ . clear ( ) ;
}
virtual void Prev ( ) override ;
virtual void Next ( ) override ;
virtual void SeekToFirst ( ) override ;
virtual void SeekToLast ( ) override ;
void Invalidate ( Status s ) { InvalidateBase ( s ) ; }
private :
bool PrefixSeek ( const Slice & target , uint32_t * index ) ;
bool BinaryBlockIndexSeek ( const Slice & target , uint32_t * block_ids ,
@ -464,23 +474,19 @@ class IndexBlockIter final : public BlockIter {
int CompareBlockKey ( uint32_t block_index , const Slice & target ) ;
inline int Compare ( const Slice & a , const Slice & b ) const {
if ( key_includes_seq_ ) {
return comparator_ - > Compare ( a , b ) ;
} else {
return user_comparator_ - > Compare ( a , b ) ;
}
return active_comparator_ - > Compare ( a , b ) ;
}
inline int Compare ( const IterKey & ikey , const Slice & b ) const {
if ( key_includes_seq_ ) {
return comparator_ - > Compare ( ikey . GetInternalKey ( ) , b ) ;
} else {
return user_comparator_ - > Compare ( ikey . GetUserKey ( ) , b ) ;
}
return active_comparator_ - > Compare ( ikey . GetKey ( ) , b ) ;
}
// Same as comparator_ if comparator_ is not InernalKeyComparator
const Comparator * user_comparator_ ;
bool ParseNextIndexKey ( ) ;
// Key is in InternalKey format
bool key_includes_seq_ ;
// key_includes_seq_ ? comparator_ : user_comparator_
const Comparator * active_comparator_ ;
BlockPrefixIndex * prefix_index_ ;
} ;