Browse Source

add valuelog cache

pull/3/head
alexfisher 9 months ago
parent
commit
5347fdee65
3 changed files with 25 additions and 34 deletions
  1. +21
    -24
      db/db_impl.cc
  2. +1
    -7
      db/db_impl.h
  3. +3
    -3
      db/dbformat.h

+ 21
- 24
db/db_impl.cc View File

@ -154,11 +154,13 @@ DBImpl::DBImpl(const Options& raw_options, const std::string& dbname)
background_compaction_scheduled_(false),
background_garbage_collect_scheduled_(false),
manual_compaction_(nullptr),
valuelog_cache(NewLRUCache(config::mem_value_log_number)),
versions_(new VersionSet(dbname_, &options_, table_cache_,
&internal_comparator_)) {
InitializeExistingLogs();
// std::cout<<"init map"<<std::endl;
}
DBImpl::~DBImpl() {
// Wait for background work to finish.
@ -188,6 +190,7 @@ DBImpl::~DBImpl() {
delete log_;
delete logfile_;
delete table_cache_;
delete valuelog_cache;
if (owns_info_log_) {
delete options_.info_log;
@ -1746,12 +1749,19 @@ void DBImpl::addNewValueLog() {
}
}
static void valuelog_cache_deleter(const leveldb::Slice &key, void *value){
delete (RandomAccessFile*)value;
}
Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset,
std::string* value) {
std::string file_name_ = ValueLogFileName(dbname_, file_id);
mutex_.Lock();
if(file_id==valuelogfile_number_){
mutex_.Unlock();
std::string file_name_ = ValueLogFileName(dbname_, file_id);
std::ifstream inFile(file_name_, std::ios::in | std::ios::binary);
uint64_t value_len;
inFile.seekg(offset);
@ -1767,25 +1777,18 @@ Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset,
Status s = Status::OK();
leveldb::RandomAccessFile* valuelog_file;
mem_valuelog_mutex.lock_shared();
if(mem_valuelogs.count(file_id)){
valuelog_file=mem_valuelogs[file_id].file;
//mem_valuelogs[file_id].ref++;
mem_valuelog_mutex.unlock_shared();
Cache::Handle* handler=nullptr;
if(handler=valuelog_cache->Lookup(file_name_)){
//
}
else{
mem_valuelog_mutex.unlock_shared();
std::string file_name_ = ValueLogFileName(dbname_, file_id);
env_->NewRandomAccessFile(file_name_,&valuelog_file);
mem_valuelog tmp;
tmp.file=valuelog_file;
tmp.ref=1;
mem_valuelog_mutex.lock();
mem_valuelogs[file_id]=tmp;
mem_valuelog_mutex.unlock();
RandomAccessFile* new_file;
s=env_->NewRandomAccessFile(file_name_,&new_file);
assert(s.ok());
handler=valuelog_cache->Insert(file_name_,new_file,1,&valuelog_cache_deleter);
}
leveldb::RandomAccessFile* valuelog_file=(RandomAccessFile*)(valuelog_cache->Value(handler));
char buf[sizeof(uint64_t)];
Slice res;
s=valuelog_file->Read(offset,sizeof(uint64_t),&res,buf);
@ -1795,15 +1798,9 @@ Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset,
char value_buf[value_len];
s=valuelog_file->Read(offset+sizeof(uint64_t),value_len,&res,value_buf);
assert(s.ok());
valuelog_cache->Release(handler);
*value=std::string(res.data(),res.size());
// mem_valuelog_mutex.Lock();
// mem_valuelogs[file_id].ref--;
// if(mem_valuelogs.size()>100&&mem_valuelogs[file_id].ref==0){
// delete mem_valuelogs[file_id].file;
// mem_valuelogs.erase(file_id);
// }
// mem_valuelog_mutex.Unlock();
return s;
}

+ 1
- 7
db/db_impl.h View File

@ -235,13 +235,7 @@ class DBImpl : public DB {
log::Writer* log_;
std::map<uint64_t, uint64_t> oldvaluelog_ids;
struct mem_valuelog{
RandomAccessFile* file;
int ref=0;
};
std::shared_mutex mem_valuelog_mutex;
std::unordered_map<uint64_t,mem_valuelog> mem_valuelogs; GUARDED_BY(mem_valuelog_mutex);
Cache* valuelog_cache;
std::map<uint64_t, uint64_t> valuelog_usage;
std::map<uint64_t, uint64_t> valuelog_origin;

+ 3
- 3
db/dbformat.h View File

@ -45,9 +45,9 @@ static const int kMaxMemCompactLevel = 2;
static const int kReadBytesPeriod = 1048576;
// maximum size of value_log file
static const int value_log_size=64<<20;
static const int mem_value_log_number=(2<<30)/(value_log_size);
static const int value_log_size=1<<26;
//1<<33/1<<26=1<<7
static const int mem_value_log_number=1<<9;//8GB
} // namespace config

Loading…
Cancel
Save