From c8e0d2b2c67b22d67766649d9adb983dd3fef867 Mon Sep 17 00:00:00 2001 From: xxy <3237539022@qq.com> Date: Thu, 31 Oct 2024 18:40:34 +0800 Subject: [PATCH] ttl in the end --- db/db_impl.cc | 4 ++++ db/version_set.cc | 7 ++++++- test/db_test1.cc | 26 ++++++++++++++++++++++++++ third_party/benchmark | 2 +- third_party/googletest | 2 +- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test/db_test1.cc diff --git a/db/db_impl.cc b/db/db_impl.cc index f96d245..a42e75c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -924,6 +924,10 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { bool has_current_user_key = false; SequenceNumber last_sequence_for_key = kMaxSequenceNumber; while (input->Valid() && !shutting_down_.load(std::memory_order_acquire)) { + auto x=input->value(); + uint64_t ttl=*(uint64_t*)(x.data()+x.size()-sizeof(uint64_t)); + time_t now = time(nullptr); + if(ttl < static_cast(now))return; // Prioritize immutable compaction work if (has_imm_.load(std::memory_order_relaxed)) { const uint64_t imm_start = env_->NowMicros(); diff --git a/db/version_set.cc b/db/version_set.cc index 4e37bf9..a7d2cce 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -266,9 +266,14 @@ static void SaveValue(void* arg, const Slice& ikey, const Slice& v) { s->state = kCorrupt; } else { if (s->ucmp->Compare(parsed_key.user_key, s->user_key) == 0) { + if(parsed_key.type == kTypeValue){ + time_t now = time(nullptr); + uint64_t ttl=*(uint64_t*)(v.data()+v.size()-sizeof(uint64_t)); + if(ttl < static_cast(now))return; + } s->state = (parsed_key.type == kTypeValue) ? kFound : kDeleted; if (s->state == kFound) { - s->value->assign(v.data(), v.size()); + s->value->assign(v.data(), v.size()-sizeof(uint64_t)); } } } diff --git a/test/db_test1.cc b/test/db_test1.cc new file mode 100644 index 0000000..7e5eb3a --- /dev/null +++ b/test/db_test1.cc @@ -0,0 +1,26 @@ +#include "leveldb/db.h" +#include + +using namespace std; +using namespace leveldb; + +int main() { + DB* db = nullptr; + Options op; + op.create_if_missing = true; + Status status = DB::Open(op, "testdb", &db); + assert(status.ok()); + db->Put(WriteOptions(), "001", "leveldb"); + string s; + db->Get(ReadOptions(), "001", &s); + cout<Put(WriteOptions(), "002", "world"); + string s1; + db->Delete(WriteOptions(), "002"); + db->Get(ReadOptions(), "002", &s1); + cout<