diff --git a/db/db_impl.cc b/db/db_impl.cc index 16687f8..1434df4 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1241,7 +1241,6 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, return s; } Slice value_log_slice = Slice(value->c_str() + 1, value->length()); - Slice new_key; Slice new_value; int value_offset = sizeof(uint64_t) * 2; // 16 uint64_t file_id, valuelog_offset; @@ -1250,7 +1249,7 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, res = GetVarint64(&value_log_slice, &valuelog_offset); if (!res) return Status::Corruption("can't decode valuelog offset"); // std::cout<<"file_id: "<> DBImpl::WriteValueLog( std::vector> res; for (const auto& [key_slice, value_slice] : kv) { - // 写入 key 的长度 - uint64_t key_len = key_slice.size(); - valueFile.write(reinterpret_cast(&key_len), sizeof(uint64_t)); + + + // 写入 value 的长度 + uint64_t value_len = value_slice.size(); + valueFile.write(reinterpret_cast(&value_len), + sizeof(uint64_t)); if (!valueFile.good()) { valueFile.close(); assert(0); } - // 写入 key 本身 - valueFile.write(key_slice.data(), key_len); + // 写入 value 本身 + valueFile.write(value_slice.data(), value_len); if (!valueFile.good()) { valueFile.close(); assert(0); } - - // 写入 value 的长度 - uint64_t value_len = value_slice.size(); - valueFile.write(reinterpret_cast(&value_len), - sizeof(uint64_t)); + + // 写入 key 的长度 + uint64_t key_len = key_slice.size(); + valueFile.write(reinterpret_cast(&key_len), sizeof(uint64_t)); if (!valueFile.good()) { valueFile.close(); assert(0); } - // 写入 value 本身 - valueFile.write(value_slice.data(), value_len); + // 写入 key 本身 + valueFile.write(key_slice.data(), key_len); if (!valueFile.good()) { valueFile.close(); assert(0); @@ -1705,7 +1706,7 @@ void DBImpl::addNewValueLog() { } } -Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset, Slice* key, +Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset, Slice* value) { Status s = Status::OK(); std::string file_name_ = ValueLogFileName(dbname_, file_id); @@ -1718,63 +1719,33 @@ Status DBImpl::ReadValueLog(uint64_t file_id, uint64_t offset, Slice* key, return Status::Corruption("Failed to open file for reading!"); } - // Seek to the position of key length - inFile.seekg(offset); - - // Read the length of the key - char* key_buf_len = new char[sizeof(uint64_t)]; - inFile.read(key_buf_len, sizeof(uint64_t)); - uint64_t key_len = 0; - std::memcpy(&key_len, key_buf_len, sizeof(uint64_t)); - - if (!inFile.good()) { - delete[] key_buf_len; - inFile.close(); - return Status::Corruption("Failed to read key length from file!"); - } - - // Now seek to the actual key position and read the key - inFile.seekg(offset + sizeof(uint64_t)); - char* key_buf = new char[key_len]; - inFile.read(key_buf, key_len); - if (!inFile.good()) { - delete[] key_buf; - delete[] key_buf_len; - inFile.close(); - return Status::Corruption("Failed to read key from file!"); - } - - // Assign the read key data to the Slice - *key = Slice(key_buf, key_len); - // Read the length of the value - inFile.seekg(offset + sizeof(uint64_t) + key_len); + inFile.seekg(offset); char* value_buf_len = new char[sizeof(uint64_t)]; inFile.read(value_buf_len, sizeof(uint64_t)); uint64_t val_len = 0; std::memcpy(&val_len, value_buf_len, sizeof(uint64_t)); if (!inFile.good()) { - delete[] key_buf; - delete[] key_buf_len; delete[] value_buf_len; inFile.close(); return Status::Corruption("Failed to read value length from file!"); } // Now seek to the actual data position and read the value - inFile.seekg(offset + sizeof(uint64_t) + key_len + sizeof(uint64_t)); + inFile.seekg(offset + sizeof(uint64_t)); char* value_buf = new char[val_len]; inFile.read(value_buf, val_len); if (!inFile.good()) { - delete[] key_buf; - delete[] key_buf_len; delete[] value_buf_len; delete[] value_buf; inFile.close(); return Status::Corruption("Failed to read value from file!"); } + // Seek to the position of key length + inFile.seekg(offset + sizeof(uint64_t) + val_len); + // Close the file after reading inFile.close(); @@ -1841,6 +1812,33 @@ void DBImpl::GarbageCollect() { Status s = Status::OK(); + // Read the length of the value + cur_valuelog.seekg(current_offset); + char* value_buf_len = new char[sizeof(uint64_t)]; + cur_valuelog.read(value_buf_len, sizeof(uint64_t)); + + if (cur_valuelog.eof()) { + delete[] value_buf_len; + break; // 正常退出条件:到达文件末尾 + } + + uint64_t val_len = 0; + std::memcpy(&val_len, value_buf_len, sizeof(uint64_t)); + + if (!cur_valuelog.good()) { + delete[] value_buf_len; + cur_valuelog.close(); + std::cerr << "3Failed to read file: " << valuelog_name << std::endl; + break; + } + // 更新当前偏移 + current_offset += sizeof(uint64_t); + // std::cout << cnt <<" "< getNewValuelog(); // use for compaction // Status ReadValueLog(uint64_t file_id, uint64_t offset,Slice* // value)override; - Status ReadValueLog(uint64_t file_id, uint64_t offset, Slice* key, + Status ReadValueLog(uint64_t file_id, uint64_t offset, Slice* value) override; // Extra methods (for testing) that are not in the public DB interface diff --git a/db/db_iter.cc b/db/db_iter.cc index e27f1a4..5995f86 100644 --- a/db/db_iter.cc +++ b/db/db_iter.cc @@ -83,7 +83,7 @@ class DBIter : public Iterator { // res=GetVarint64(&tmp_value,&valuelog_len); // if(!res)assert(0); // db_->ReadValueLog(file_id,valuelog_offset,valuelog_len,&tmp_value); - db_->ReadValueLog(file_id,valuelog_offset, &key, &tmp_value); + db_->ReadValueLog(file_id,valuelog_offset, &tmp_value); return tmp_value; } Status status() const override { diff --git a/include/leveldb/db.h b/include/leveldb/db.h index cefa79c..6118f31 100644 --- a/include/leveldb/db.h +++ b/include/leveldb/db.h @@ -119,7 +119,7 @@ class LEVELDB_EXPORT DB { // assert(0); // Not implemented // return Status::Corruption("not imp"); // } - virtual Status ReadValueLog(uint64_t file_id, uint64_t offset, Slice* key, Slice* value){ + virtual Status ReadValueLog(uint64_t file_id, uint64_t offset, Slice* value){ assert(0); // Not implemented return Status::Corruption("not imp"); }