2 Commits

3 changed files with 12 additions and 18 deletions
Split View
  1. +9
    -15
      db/db_impl.cc
  2. +1
    -1
      db/vlog.h
  3. +2
    -2
      db/vlog_set.cpp

+ 9
- 15
db/db_impl.cc View File

@ -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,7 +1284,7 @@ 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) {

+ 1
- 1
db/vlog.h View File

@ -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_sizelass="p">(uint16_t) |n> field_nums(uint16_t) | slot_num(size_t) | value |
#define vlog_num_size sizeof(size_t)
#define vlog_name_size 256

+ 2
- 2
db/vlog_set.cpp View File

@ -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<size_t*>(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<const char*>(&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);

Loading…
Cancel
Save