From 9a270859a7a77d9d0673c5464e80775422598271 Mon Sep 17 00:00:00 2001 From: dgy Date: Sat, 28 Dec 2024 16:24:41 +0000 Subject: [PATCH] update unorderediter --- db/db_impl.cc | 3 ++- db/dbformat.h | 4 ++-- db/unordered_iter.cc | 36 ++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 1bda06b..c26edbe 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1766,7 +1766,7 @@ Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset, std::string file_name_ = ValueLogFileName(dbname_, file_id); mutex_.Lock(); - if(file_id==valuelogfile_number_){ + if(file_id==valuelogfile_number_||config::mem_value_log_number==0){ mutex_.Unlock(); std::ifstream inFile(file_name_, std::ios::in | std::ios::binary); @@ -1776,6 +1776,7 @@ Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset, char buf[value_len]; inFile.read(buf,value_len); + inFile.close(); *value=std::string(buf,value_len); return Status::OK(); } diff --git a/db/dbformat.h b/db/dbformat.h index 61ad581..e0dce4a 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -47,9 +47,9 @@ static const int kReadBytesPeriod = 1048576; // maximum size of value_log file static const int value_log_size=1<<26; //1<<33/1<<26=1<<7 -static const int mem_value_log_number=1<<8;//8GB +static const int mem_value_log_number=0;//8GB -static const int max_unorder_iter_memory_usage=32<<20; //32MB +static const int max_unorder_iter_memory_usage=64<<20; //32MB } // namespace config diff --git a/db/unordered_iter.cc b/db/unordered_iter.cc index 247b6cd..55ca072 100644 --- a/db/unordered_iter.cc +++ b/db/unordered_iter.cc @@ -52,6 +52,8 @@ class UnorderedIter : public Iterator { current_file->close(); delete current_file; } + if(buf_for_now_key)delete buf_for_now_key; + if(buf_for_now_value)delete buf_for_now_value; delete iter_; } bool Valid() const override { return mode!=2; } @@ -91,18 +93,25 @@ class UnorderedIter : public Iterator { current_file->seekg(offset); current_file->read((char*)(&value_len),sizeof(uint64_t)); - char buf[value_len]; - current_file->read(buf,value_len); - buf_for_now_value=std::string(buf,value_len); + if(value_len>buf_for_now_value_size){ + buf_for_now_value_size=value_len; + if(buf_for_now_value)delete buf_for_now_value; + buf_for_now_value=new char[value_len]; + } + current_file->read(buf_for_now_value,value_len); current_file->read((char*)(&key_len),sizeof(uint64_t)); - char key_buf[key_len]; - current_file->read(key_buf,key_len); - buf_for_now_key=std::string(key_buf,key_len); + if(key_len>buf_for_now_key_size){ + buf_for_now_key_size=key_len; + if(buf_for_now_key)delete buf_for_now_key; + buf_for_now_key=new char[key_len]; + } + + current_file->read(buf_for_now_key,key_len); - now_key=Slice(buf_for_now_key); - now_value=Slice(buf_for_now_value); + now_value=Slice(buf_for_now_value,value_len); + now_key=Slice(buf_for_now_key,key_len); } @@ -110,8 +119,10 @@ class UnorderedIter : public Iterator { Iterator* const iter_; Slice now_value; Slice now_key; - std::string buf_for_now_key; - std::string buf_for_now_value; + int buf_for_now_key_size=0; + char* buf_for_now_key=nullptr; + int buf_for_now_value_size=0; + char* buf_for_now_value=nullptr; bool iter_valid=false; std::map> valuelog_map; int memory_usage=0; @@ -156,6 +167,11 @@ void UnorderedIter::Next() { valuelog_map_iter=valuelog_map.begin(); if(valuelog_map_iter!=valuelog_map.end()){ + + for(auto it=valuelog_map.begin();it!=valuelog_map.end();it++){ + std::sort(it->second.begin(),it->second.end()); + } + std::string file_name_ = ValueLogFileName(db_name_, valuelog_map_iter->first); assert(!current_file); current_file=new std::ifstream(file_name_, std::ios::in | std::ios::binary);