From 2927716e49a37d8dbe117c18e09c1283b099b725 Mon Sep 17 00:00:00 2001 From: Yechi Ma <2662511702@qq.com> Date: Wed, 6 Nov 2024 18:12:33 +0800 Subject: [PATCH] TestTTL.ReadTTL passed --- db/db_impl.cc | 31 +++++++++++++++++++++++++++++++ db/db_impl.h | 3 +++ include/leveldb/db.h | 8 ++++---- test/ttl_test.cc | 10 +++++----- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index f96d245..2497871 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1161,6 +1161,24 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, mem->Unref(); if (imm != nullptr) imm->Unref(); current->Unref(); + /** parse ttl from value **/ + size_t pos = value->find_last_of('_'); + if (pos != std::string::npos) { + std::string substring = value->substr(pos + 1); + auto ttl = static_cast(std::stoll(substring)); + auto now = std::chrono::system_clock::now(); + auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); + auto microsecondsTimestamp = static_cast(timestamp); + if (ttl <= microsecondsTimestamp) { + value->clear(); + Slice msg1("value not found!"); + Slice msg2("value has expired!"); + s = leveldb::Status::NotFound(msg1, msg2); + } else { + value->resize(pos); + } + } + /** parse ttl from value **/ return s; } @@ -1198,6 +1216,19 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { return DB::Put(o, key, val); } +Status DBImpl::Put(const WriteOptions& opt, const Slice& key, + const Slice& value, uint64_t ttl) { + WriteBatch batch; + auto now = std::chrono::system_clock::now(); + auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); + auto microsecondsTimestamp = static_cast(timestamp) + ttl*1000000; + std::string value_ttl = value.ToString(); + value_ttl += "_" + std::to_string(microsecondsTimestamp); + Slice new_value(value_ttl.c_str(), value_ttl.size()); + batch.Put(key, new_value); + return Write(opt, &batch); +} + Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { return DB::Delete(options, key); } diff --git a/db/db_impl.h b/db/db_impl.h index c7b0172..f36fdd7 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -38,6 +38,9 @@ class DBImpl : public DB { // Implementations of the DB interface Status Put(const WriteOptions&, const Slice& key, const Slice& value) override; + // For TTL + Status Put(const WriteOptions& opt, const Slice& key, + const Slice& value, uint64_t ttl) override; Status Delete(const WriteOptions&, const Slice& key) override; Status Write(const WriteOptions& options, WriteBatch* updates) override; Status Get(const ReadOptions& options, const Slice& key, diff --git a/include/leveldb/db.h b/include/leveldb/db.h index 0618d48..60cf9db 100644 --- a/include/leveldb/db.h +++ b/include/leveldb/db.h @@ -146,10 +146,10 @@ class LEVELDB_EXPORT DB { // db->CompactRange(nullptr, nullptr); virtual void CompactRange(const Slice* begin, const Slice* end) = 0; - // // ----------------------------For TTL----------------------------- - // // 为当前key设置ttl,过期后自动失效 - // virtual Status Put(const WriteOptions& options, const Slice& key, - // const Slice& value, uint64_t ttl) = 0; + /// ----------------------------For TTL----------------------------- + /// 为当前key设置ttl,过期后自动失效 + virtual Status Put(const WriteOptions& options, const Slice& key, + const Slice& value, uint64_t ttl) = 0; }; // Destroy the contents of the specified database. diff --git a/test/ttl_test.cc b/test/ttl_test.cc index 4bb524a..7402893 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -99,11 +99,11 @@ TEST(TestTTL, CompactionTTL) { db->CompactRange(nullptr, nullptr); - leveldb::Range ranges[1]; - ranges[0] = leveldb::Range("-", "A"); - uint64_t sizes[1]; - db->GetApproximateSizes(ranges, 1, sizes); - ASSERT_EQ(sizes[0], 0); + leveldb::Range ranges_[1]; + ranges_[0] = leveldb::Range("-", "A"); + uint64_t sizes_[1]; + db->GetApproximateSizes(ranges_, 1, sizes_); + ASSERT_EQ(sizes_[0], 0); }