|
@ -11,6 +11,10 @@ |
|
|
#include <set>
|
|
|
#include <set>
|
|
|
#include <string>
|
|
|
#include <string>
|
|
|
#include <vector>
|
|
|
#include <vector>
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
#include <numeric>
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
#include "db/builder.h"
|
|
|
#include "db/builder.h"
|
|
|
#include "db/db_iter.h"
|
|
|
#include "db/db_iter.h"
|
|
@ -1184,6 +1188,7 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, |
|
|
slot_page_->get_slot(slot_num, &sc); |
|
|
slot_page_->get_slot(slot_num, &sc); |
|
|
vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value); |
|
|
vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value); |
|
|
*value = vlog_value; |
|
|
*value = vlog_value; |
|
|
|
|
|
std::cout << "value from value_log: " << vlog_value << std::endl; |
|
|
// TODO(end)
|
|
|
// TODO(end)
|
|
|
return s; |
|
|
return s; |
|
|
} |
|
|
} |
|
@ -1218,30 +1223,36 @@ void DBImpl::ReleaseSnapshot(const Snapshot* snapshot) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Convenience methods
|
|
|
// Convenience methods
|
|
|
Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { |
|
|
|
|
|
|
|
|
Status DBImpl::Put_Fields(const WriteOptions& opt, const Slice& key, |
|
|
|
|
|
const FieldArray& fields) { |
|
|
// TODO(begin): allocate slot_num in slotpage and put value in vlog
|
|
|
// TODO(begin): allocate slot_num in slotpage and put value in vlog
|
|
|
|
|
|
|
|
|
|
|
|
// 将字段数组序列化
|
|
|
|
|
|
std::string serialized_value = SerializeValue(fields); |
|
|
|
|
|
std::cout << "Put_Fields: " << key.ToString() << " " << serialized_value << std::endl; |
|
|
size_t slot_num = slot_page_->alloc_slot(); |
|
|
size_t slot_num = slot_page_->alloc_slot(); |
|
|
struct slot_content sc; |
|
|
struct slot_content sc; |
|
|
vlog_set_->put_value(&sc.vlog_num, &sc.value_offset, val); |
|
|
|
|
|
|
|
|
vlog_set_->put_value(&sc.vlog_num, &sc.value_offset, serialized_value); |
|
|
slot_page_->set_slot(slot_num, &sc); |
|
|
slot_page_->set_slot(slot_num, &sc); |
|
|
|
|
|
|
|
|
char data[sizeof(size_t)]; |
|
|
char data[sizeof(size_t)]; |
|
|
memcpy(data, &slot_num, sizeof(size_t)); |
|
|
memcpy(data, &slot_num, sizeof(size_t)); |
|
|
Slice slot_val(data, sizeof(data)); |
|
|
Slice slot_val(data, sizeof(data)); |
|
|
|
|
|
|
|
|
return DB::Put(o, key, slot_val); |
|
|
|
|
|
|
|
|
return DB::Put(opt, key, slot_val); |
|
|
// TODO(end)
|
|
|
// TODO(end)
|
|
|
} |
|
|
} |
|
|
|
|
|
Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { |
|
|
|
|
|
return DB::Put(o, key, val); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { |
|
|
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { |
|
|
// TODO(begin)
|
|
|
|
|
|
// size_t slot_num = *(size_t *)value->c_str();
|
|
|
// size_t slot_num = *(size_t *)value->c_str();
|
|
|
// struct slot_content sc;
|
|
|
// struct slot_content sc;
|
|
|
// std::string vlog_value;
|
|
|
// std::string vlog_value;
|
|
|
// slot_page_->get_slot(slot_num, &sc);
|
|
|
// slot_page_->get_slot(slot_num, &sc);
|
|
|
// vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value);
|
|
|
// vlog_set_->get_value(sc.vlog_num, sc.value_offset, &vlog_value);
|
|
|
// *value = vlog_value;
|
|
|
// *value = vlog_value;
|
|
|
|
|
|
// TODO(begin)
|
|
|
ReadOptions ro; |
|
|
ReadOptions ro; |
|
|
ro.verify_checksums = true; |
|
|
ro.verify_checksums = true; |
|
|
ro.fill_cache = false; |
|
|
ro.fill_cache = false; |
|
@ -1537,7 +1548,32 @@ void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) { |
|
|
|
|
|
|
|
|
v->Unref(); |
|
|
v->Unref(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string DBImpl::SerializeValue(const FieldArray& fields) { |
|
|
|
|
|
// 创建并初始化一个字符串流 oss,用于逐步构建最终的序列化字符串
|
|
|
|
|
|
std::ostringstream oss_temp; |
|
|
|
|
|
std::string slot_num = "slot_num"; |
|
|
|
|
|
oss_temp << std::setw(sizeof(size_t)) << std::setfill('0') << slot_num; |
|
|
|
|
|
// 写入属性个数(定长,16比特),使用std::setw(16)设置宽度,使用std::setfull(0)设置填充字符,将字段数组的大小写入oss中
|
|
|
|
|
|
oss_temp << std::setw(16) << std::setfill('0') << fields.size(); |
|
|
|
|
|
for (const auto& field : fields) { |
|
|
|
|
|
// 写入属性名长度(定长,16比特)
|
|
|
|
|
|
oss_temp << std::setw(16) << std::setfill('0') << field.name.size(); |
|
|
|
|
|
// 写入属性名(变长)
|
|
|
|
|
|
oss_temp << field.name; |
|
|
|
|
|
// 写入属性值长度(定长,16比特)
|
|
|
|
|
|
oss_temp << std::setw(16) << std::setfill('0') << field.value.size(); |
|
|
|
|
|
// 写入属性值(变长)
|
|
|
|
|
|
oss_temp << field.value; |
|
|
|
|
|
} |
|
|
|
|
|
std::string temp_str = oss_temp.str(); |
|
|
|
|
|
size_t value_length = temp_str.size(); |
|
|
|
|
|
|
|
|
|
|
|
std::ostringstream oss; |
|
|
|
|
|
oss << std::setw(16) << std::setfill('0') << value_length; |
|
|
|
|
|
oss << temp_str; |
|
|
|
|
|
|
|
|
|
|
|
return oss.str(); |
|
|
|
|
|
} |
|
|
// Default implementations of convenience methods that subclasses of DB
|
|
|
// Default implementations of convenience methods that subclasses of DB
|
|
|
// can call if they wish
|
|
|
// can call if they wish
|
|
|
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) { |
|
|
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) { |
|
|