@ -242,47 +242,17 @@ class IterKey {
public :
public :
IterKey ( ) : key_ ( space_ ) , buf_size_ ( sizeof ( space_ ) ) , key_size_ ( 0 ) { }
IterKey ( ) : key_ ( space_ ) , buf_size_ ( sizeof ( space_ ) ) , key_size_ ( 0 ) { }
~ IterKey ( ) { Clea r( ) ; }
~ IterKey ( ) { ResetBuffe r( ) ; }
Slice GetKey ( ) const {
Slice GetKey ( ) const { return Slice ( key_ , key_size_ ) ; }
if ( key_ ! = nullptr ) {
return Slice ( key_ , key_size_ ) ;
} else {
return Slice ( ) ;
}
}
bool Valid ( ) const { return key_ ! = nullptr ; }
void Clear ( ) {
if ( key_ ! = nullptr & & key_ ! = space_ ) {
delete [ ] key_ ;
}
key_ = space_ ;
buf_size_ = sizeof ( buf_size_ ) ;
}
// Enlarge the buffer size if needed based on key_size.
void Clear ( ) { key_size_ = 0 ; }
// By default, static allocated buffer is used. Once there is a key
// larger than the static allocated buffer, another buffer is dynamically
// allocated, until a larger key buffer is requested. In that case, we
// reallocate buffer and delete the old one.
void EnlargeBufferIfNeeded ( size_t key_size ) {
// If size is smaller than buffer size, continue using current buffer,
// or the static allocated one, as default
if ( key_size > buf_size_ ) {
// Need to enlarge the buffer.
Clear ( ) ;
key_ = new char [ key_size ] ;
buf_size_ = key_size ;
}
key_size_ = key_size ;
}
void SetUserKey ( const Slice & user_key ) {
void SetUserKey ( const Slice & user_key ) {
size_t size = user_key . size ( ) ;
size_t size = user_key . size ( ) ;
EnlargeBufferIfNeeded ( size ) ;
EnlargeBufferIfNeeded ( size ) ;
memcpy ( key_ , user_key . data ( ) , size ) ;
memcpy ( key_ , user_key . data ( ) , size ) ;
key_size_ = size ;
}
}
void SetInternalKey ( const Slice & user_key , SequenceNumber s ,
void SetInternalKey ( const Slice & user_key , SequenceNumber s ,
@ -291,6 +261,7 @@ class IterKey {
EnlargeBufferIfNeeded ( usize + sizeof ( uint64_t ) ) ;
EnlargeBufferIfNeeded ( usize + sizeof ( uint64_t ) ) ;
memcpy ( key_ , user_key . data ( ) , usize ) ;
memcpy ( key_ , user_key . data ( ) , usize ) ;
EncodeFixed64 ( key_ + usize , PackSequenceAndType ( s , value_type ) ) ;
EncodeFixed64 ( key_ + usize , PackSequenceAndType ( s , value_type ) ) ;
key_size_ = usize + sizeof ( uint64_t ) ;
}
}
void SetInternalKey ( const ParsedInternalKey & parsed_key ) {
void SetInternalKey ( const ParsedInternalKey & parsed_key ) {
@ -303,6 +274,31 @@ class IterKey {
size_t key_size_ ;
size_t key_size_ ;
char space_ [ 32 ] ; // Avoid allocation for short keys
char space_ [ 32 ] ; // Avoid allocation for short keys
void ResetBuffer ( ) {
if ( key_ ! = nullptr & & key_ ! = space_ ) {
delete [ ] key_ ;
}
key_ = space_ ;
buf_size_ = sizeof ( buf_size_ ) ;
key_size_ = 0 ;
}
// Enlarge the buffer size if needed based on key_size.
// By default, static allocated buffer is used. Once there is a key
// larger than the static allocated buffer, another buffer is dynamically
// allocated, until a larger key buffer is requested. In that case, we
// reallocate buffer and delete the old one.
void EnlargeBufferIfNeeded ( size_t key_size ) {
// If size is smaller than buffer size, continue using current buffer,
// or the static allocated one, as default
if ( key_size > buf_size_ ) {
// Need to enlarge the buffer.
ResetBuffer ( ) ;
key_ = new char [ key_size ] ;
buf_size_ = key_size ;
}
}
// No copying allowed
// No copying allowed
IterKey ( const IterKey & ) = delete ;
IterKey ( const IterKey & ) = delete ;
void operator = ( const IterKey & ) = delete ;
void operator = ( const IterKey & ) = delete ;