From 78185e9e9c0ae7bdc165313228be4de85a7fca93 Mon Sep 17 00:00:00 2001 From: ArcueidType <981354012@qq.com> Date: Thu, 31 Oct 2024 21:25:24 +0800 Subject: [PATCH] optimize codes structure and paa ReadTTL test --- db/db_impl.cc | 19 +++++-------------- test/db_test1.cc | 3 +-- util/coding.h | 11 +++++++++++ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index d521441..9c9f3f5 100644 --- a/db/db_impl.cc +++ b/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); } diff --git a/test/db_test1.cc b/test/db_test1.cc index 373d107..10717db 100644 --- a/test/db_test1.cc +++ b/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<Put(WriteOptions(), "002", "world"); string s1; diff --git a/util/coding.h b/util/coding.h index f0bb57b..85e2ca7 100644 --- a/util/coding.h +++ b/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_