From b2553a86541168a00699962235bdee6ae11280ab Mon Sep 17 00:00:00 2001 From: Yechi Ma <2662511702@qq.com> Date: Wed, 1 Jan 2025 13:00:32 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- db/db_impl.cc | 30 +++++++++--------------------- db/vlog.h | 2 +- db/vlog_set.cpp | 4 ++-- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 4ddc9a1..9dc5220 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1190,27 +1190,22 @@ Status DBImpl::get_slot_num(const ReadOptions& options, const Slice& key, Status DBImpl::Get_Fields(const ReadOptions& options, const Slice& key, FieldArray* fields) { // Todo(begin) - std::string value; - Status s = Get(options, key, &value); + size_t slot_num; + // 从value中提取slot_num + auto s = get_slot_num(options, key, &slot_num); if (!s.ok()) { return s; } - // 从value中提取slot_num - size_t slot_num; - slot_num = *(size_t *)value.c_str(); // 这里假设value的前几个字节存储了slot_num + struct slot_content sc; std::string vlog_value; - // 从slot_page中获取slot内容 slot_page_->get_slot(slot_num, &sc); - // 从vlog_set中获取实际的日志值 vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value); - // 更新value为从vlog获取的值 - value = vlog_value; - std::cout << "value from value_log: " << key.ToString() << value << std::endl; - *fields = DeserializeValue(value); + std::cout << "value from value_log: " << key.ToString() << vlog_value << std::endl; + *fields = DeserializeValue(vlog_value); return Status::OK(); - // TODO(end) + // Todo(end) } Status DBImpl::Get(const ReadOptions& options, const Slice& key, @@ -1277,8 +1272,7 @@ Status DBImpl::Put_Fields(const WriteOptions& opt, const Slice& key, // TODO(end) } -// Convenience methods -Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { +Status DBImpl::Put(const WriteOptions& opt, const Slice& key, const Slice& val) { // TODO: allocate slot_num in slotpage and put value in vlog size_t slot_num = slot_page_->alloc_slot(); @@ -1290,16 +1284,10 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { memcpy(data, &slot_num, sizeof(size_t)); Slice slot_val(data, sizeof(data)); - return DB::Put(o, key, slot_val); + return DB::Put(opt, key, slot_val); } Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { -// size_t slot_num = *(size_t *)value->c_str(); -// struct slot_content sc; -// std::string vlog_value; -// slot_page_->get_slot(slot_num, &sc); -// vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value); -// *value = vlog_value; size_t slot_num; auto s = get_slot_num(ReadOptions(), key, &slot_num); diff --git a/db/vlog.h b/db/vlog.h index 54ac5a0..20c1ceb 100644 --- a/db/vlog.h +++ b/db/vlog.h @@ -19,7 +19,7 @@ // config file: | vlog_nums_(size_t) | vlog_1_name | vlog_2_name | ... | // vlog: | curr_size(size_t) | value_nums(size_t) | value1 | value2 | ... | -// value: value_len(uint16_t) | slot_num(size_t) | value | +// value: | value_size(uint16_t) | field_nums(uint16_t) | slot_num(size_t) | value | #define vlog_num_size sizeof(size_t) #define vlog_name_size 256 diff --git a/db/vlog_set.cpp b/db/vlog_set.cpp index 506dc79..e598c03 100644 --- a/db/vlog_set.cpp +++ b/db/vlog_set.cpp @@ -216,14 +216,13 @@ void VlogSet::register_inconfig_file(size_t vlog_num) { void VlogSet::remove_from_config_file(size_t vlog_num) { char tmp[vlog_nums_*vlog_num_size]; - size_t index = 0; config_file_->seekp(sizeof(size_t)); config_file_->read(tmp, sizeof(tmp)); size_t *vlog_num_ptr = reinterpret_cast(tmp); for (auto i = 0; i < vlog_nums_; i++) { size_t curr_vlog_num = *vlog_num_ptr; if (!(curr_vlog_num & CONFIG_FILE_DELE_MASK) && curr_vlog_num == vlog_num) { - curr_vlog_num &= CONFIG_FILE_DELE_MASK; + curr_vlog_num |= CONFIG_FILE_DELE_MASK; config_file_->seekp(sizeof(size_t) + i*sizeof(size_t)); config_file_->write(reinterpret_cast(&curr_vlog_num), sizeof(size_t)); config_file_->flush(); @@ -291,6 +290,7 @@ struct vlog_handler * VlogSet::get_vlog_handler(size_t vlog_num) { return vlog_handler_map_[get_vlog_name(vlog_num)]; } +// value: | field_nums(uint16_t) | slot_num(size_t) | value | void VlogSet::read_vlog_value(uint32_t vlog_num, uint32_t value_offset, std::string *value) { auto vlog_name = get_vlog_name(vlog_num); auto handler = std::fstream(vlog_name, std::ios::in | std::ios::out);