diff --git a/db/db_impl.cc b/db/db_impl.cc index c311208..f3e6646 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -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"<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; } diff --git a/db/db_impl.h b/db/db_impl.h index 1bc2886..2b6be55 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -235,13 +235,7 @@ class DBImpl : public DB { log::Writer* log_; std::map oldvaluelog_ids; - struct mem_valuelog{ - RandomAccessFile* file; - int ref=0; - }; - - std::shared_mutex mem_valuelog_mutex; - std::unordered_map mem_valuelogs; GUARDED_BY(mem_valuelog_mutex); + Cache* valuelog_cache; std::map valuelog_usage; std::map valuelog_origin; diff --git a/db/dbformat.h b/db/dbformat.h index 2466307..c6ab98f 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -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