浏览代码

optimize codes structure and paa ReadTTL test

naive_version
ArcueidType 3 周前
父节点
当前提交
78185e9e9c
共有 3 个文件被更改,包括 17 次插入16 次删除
  1. +5
    -14
      db/db_impl.cc
  2. +1
    -2
      test/db_test1.cc
  3. +11
    -0
      util/coding.h

+ 5
- 14
db/db_impl.cc 查看文件

@ -1157,18 +1157,15 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key,
}
// TTL: Get the true value and make sure the data is still living
if(!value->empty()) {
auto separator = value->find_first_of("|");
std::string ddl_str = value->substr(0, separator);
uint64_t dead_line = std::atoll(ddl_str.c_str());
uint64_t dead_line;
DecodeDeadLineValue(value, dead_line);
if (dead_line != 0) {
// use TTL
if (std::time(nullptr) >= dead_line) {
// data expired
*value = "";
s = Status::NotFound("Data expired");
}
else {
*value = value->substr(separator + 1);
}
} else {
// TTL not set
}
@ -1514,10 +1511,7 @@ void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) {
// TTL: Update TTL Encode
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
WriteBatch batch;
// char * ttl_encode = new char[8];
// EncodeFixed64(ttl_encode, 0);
std::string ttl_value = "0|" + value.ToString();
batch.Put(key, ttl_value);
batch.Put(key, EncodeDeadLine(0, value));
return Write(opt, &batch);
}
@ -1525,11 +1519,8 @@ Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
Status DB::Put(const WriteOptions& options, const Slice& key,
const Slice& value, uint64_t ttl) {
WriteBatch batch;
// char * ttl_encode = new char[8];
// EncodeFixed64(ttl_encode, std::time(nullptr) + ttl);
auto dead_line = std::time(nullptr) + ttl;
std::string ttl_value = std::to_string(dead_line) + "|" + value.ToString();
batch.Put(key, ttl_value);
batch.Put(key, EncodeDeadLine(dead_line, value));
return Write(options, &batch);
}

+ 1
- 2
test/db_test1.cc 查看文件

@ -10,12 +10,11 @@ int main() {
op.create_if_missing = true;
Status status = DB::Open(op, "testdb", &db);
assert(status.ok());
db->Put(WriteOptions(), "001", "leveldb", 20);
db->Put(WriteOptions(), "001", "leveldb", 5);
string s;
auto stat = db->Get(ReadOptions(), "001", &s);
cout<<s<<endl;
cout<<stat.ok()<<endl;
db->Put(WriteOptions(), "002", "world");
string s1;

+ 11
- 0
util/coding.h 查看文件

@ -117,6 +117,17 @@ inline const char* GetVarint32Ptr(const char* p, const char* limit,
return GetVarint32PtrFallback(p, limit, value);
}
inline std::string EncodeDeadLine(uint64_t ddl, const Slice& value) {
return std::to_string(ddl) + "|" + value.ToString();
}
inline void DecodeDeadLineValue(std::string* value, uint64_t& ddl) {
auto separator = value->find_first_of("|");
std::string ddl_str = value->substr(0, separator);
ddl = std::atoll(ddl_str.c_str());
*value = value->substr(separator + 1);
}
} // namespace leveldb
#endif // STORAGE_LEVELDB_UTIL_CODING_H_

正在加载...
取消
保存