@ -5,9 +5,10 @@
# include "db/table_cache.h"
# include "db/table_cache.h"
# include "db/filename.h"
# include "db/filename.h"
# include "db/db_statistics.h"
# include "leveldb/env.h"
# include "leveldb/env.h"
# include "leveldb/table.h"
# include "leveldb/table.h"
# include "leveldb/statistics.h"
# include "util/coding.h"
# include "util/coding.h"
namespace leveldb {
namespace leveldb {
@ -17,13 +18,11 @@ struct TableAndFile {
unique_ptr < Table > table ;
unique_ptr < Table > table ;
} ;
} ;
static class DB Statistics* dbstatistics ;
static class Statistics * dbstatistics ;
static void DeleteEntry ( const Slice & key , void * value ) {
static void DeleteEntry ( const Slice & key , void * value ) {
TableAndFile * tf = reinterpret_cast < TableAndFile * > ( value ) ;
TableAndFile * tf = reinterpret_cast < TableAndFile * > ( value ) ;
if ( dbstatistics ) {
RecordTick ( dbstatistics , NO_FILE_CLOSES ) ;
dbstatistics - > incNumFileCloses ( ) ;
}
delete tf ;
delete tf ;
}
}
@ -40,7 +39,7 @@ TableCache::TableCache(const std::string& dbname,
dbname_ ( dbname ) ,
dbname_ ( dbname ) ,
options_ ( options ) ,
options_ ( options ) ,
cache_ ( NewLRUCache ( entries , options - > table_cache_numshardbits ) ) {
cache_ ( NewLRUCache ( entries , options - > table_cache_numshardbits ) ) {
dbstatistics = ( DBStatistics * ) options - > statistics ;
dbstatistics = options - > statistics ;
}
}
TableCache : : ~ TableCache ( ) {
TableCache : : ~ TableCache ( ) {
@ -52,24 +51,23 @@ Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
char buf [ sizeof ( file_number ) ] ;
char buf [ sizeof ( file_number ) ] ;
EncodeFixed64 ( buf , file_number ) ;
EncodeFixed64 ( buf , file_number ) ;
Slice key ( buf , sizeof ( buf ) ) ;
Slice key ( buf , sizeof ( buf ) ) ;
DBStatistics * stats = ( DBStatistics * ) options_ - > statistics ;
* handle = cache_ - > Lookup ( key ) ;
* handle = cache_ - > Lookup ( key ) ;
if ( * handle = = NULL ) {
if ( * handle = = nullptr ) {
if ( tableIO ! = NULL ) {
if ( tableIO ! = nullptr ) {
* tableIO = true ; // we had to do IO from storage
* tableIO = true ; // we had to do IO from storage
}
}
std : : string fname = TableFileName ( dbname_ , file_number ) ;
std : : string fname = TableFileName ( dbname_ , file_number ) ;
unique_ptr < RandomAccessFile > file ;
unique_ptr < RandomAccessFile > file ;
unique_ptr < Table > table ;
unique_ptr < Table > table ;
s = env_ - > NewRandomAccessFile ( fname , & file ) ;
s = env_ - > NewRandomAccessFile ( fname , & file ) ;
stats ? stats - > incNumFileOpens ( ) : ( void ) 0 ;
RecordTick ( options_ - > statistics , NO_FILE_OPENS ) ;
if ( s . ok ( ) ) {
if ( s . ok ( ) ) {
s = Table : : Open ( * options_ , std : : move ( file ) , file_size , & table ) ;
s = Table : : Open ( * options_ , std : : move ( file ) , file_size , & table ) ;
}
}
if ( ! s . ok ( ) ) {
if ( ! s . ok ( ) ) {
assert ( table = = NULL ) ;
assert ( table = = nullptr ) ;
stats ? stats - > incNumFileErrors ( ) : ( void ) 0 ;
RecordTick ( options_ - > statistics , NO_FILE_ERRORS ) ;
// We do not cache error results so that if the error is transient,
// We do not cache error results so that if the error is transient,
// or somebody repairs the file, we recover automatically.
// or somebody repairs the file, we recover automatically.
} else {
} else {
@ -86,11 +84,11 @@ Iterator* TableCache::NewIterator(const ReadOptions& options,
uint64_t file_number ,
uint64_t file_number ,
uint64_t file_size ,
uint64_t file_size ,
Table * * tableptr ) {
Table * * tableptr ) {
if ( tableptr ! = NULL ) {
if ( tableptr ! = nullptr ) {
* tableptr = NULL ;
* tableptr = nullptr ;
}
}
Cache : : Handle * handle = NULL ;
Cache : : Handle * handle = nullptr ;
Status s = FindTable ( file_number , file_size , & handle ) ;
Status s = FindTable ( file_number , file_size , & handle ) ;
if ( ! s . ok ( ) ) {
if ( ! s . ok ( ) ) {
return NewErrorIterator ( s ) ;
return NewErrorIterator ( s ) ;
@ -100,7 +98,7 @@ Iterator* TableCache::NewIterator(const ReadOptions& options,
reinterpret_cast < TableAndFile * > ( cache_ - > Value ( handle ) ) - > table . get ( ) ;
reinterpret_cast < TableAndFile * > ( cache_ - > Value ( handle ) ) - > table . get ( ) ;
Iterator * result = table - > NewIterator ( options ) ;
Iterator * result = table - > NewIterator ( options ) ;
result - > RegisterCleanup ( & UnrefEntry , cache_ . get ( ) , handle ) ;
result - > RegisterCleanup ( & UnrefEntry , cache_ . get ( ) , handle ) ;
if ( tableptr ! = NULL ) {
if ( tableptr ! = nullptr ) {
* tableptr = table ;
* tableptr = table ;
}
}
return result ;
return result ;
@ -113,7 +111,7 @@ Status TableCache::Get(const ReadOptions& options,
void * arg ,
void * arg ,
void ( * saver ) ( void * , const Slice & , const Slice & , bool ) ,
void ( * saver ) ( void * , const Slice & , const Slice & , bool ) ,
bool * tableIO ) {
bool * tableIO ) {
Cache : : Handle * handle = NULL ;
Cache : : Handle * handle = nullptr ;
Status s = FindTable ( file_number , file_size , & handle , tableIO ) ;
Status s = FindTable ( file_number , file_size , & handle , tableIO ) ;
if ( s . ok ( ) ) {
if ( s . ok ( ) ) {
Table * t =
Table * t =