Browse Source

修改memtable读取逻辑-陈予曈

main
陈予曈 3 weeks ago
parent
commit
dfd006a405
1 changed files with 6 additions and 8 deletions
  1. +6
    -8
      leveldb_base/db/memtable.cc

+ 6
- 8
leveldb_base/db/memtable.cc View File

@ -106,7 +106,7 @@ void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
table_.Insert(buf);
}
//修改memtable读取逻辑-陈予曈
// 修改memtable读取逻辑-陈予曈
bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
Slice memkey = key.memtable_key();
Table::Iterator iter(&table_);
@ -129,37 +129,35 @@ bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
// Correct user key
const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
switch (static_cast<ValueType>(tag & 0xff)) {
// 数据过期则读取不到-陈予曈
case kTypeValue: {
Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
// 数据过期则读取不到-陈予曈
std::string value_with_ttl(v.data(), v.size());
if (value_with_ttl.size() >= 19) {
std::string expiration_time_str = value_with_ttl.substr(value_with_ttl.size() - 19); // 提取过期时间戳
std::tm tm = {};
char* res = strptime(expiration_time_str.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
if (res == nullptr) { //解析时间戳失败
if (res == nullptr) { // 解析时间戳失败
value->assign(v.data(), v.size()-19);
return true;
} else {
std::time_t expiration_time = std::mktime(&tm);
std::time_t current_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
if (expiration_time <= current_time) { //数据过期
if (expiration_time <= current_time) { // 数据过期
//std::cerr << "notfound_mem" << std::endl;
*s = Status::NotFound(Slice());
value->assign(v.data(), 0);
}
else //数据未过期
else // 数据未过期
{
value->assign(v.data(), v.size()-19);
}
return true;
}
} else { //时间戳信息不存在
} else { // 时间戳信息不存在
value->assign(v.data(), v.size());
return true;
}
value->assign(v.data(), v.size());
return true;
}
case kTypeDeletion:
*s = Status::NotFound(Slice());

Loading…
Cancel
Save