diff --git a/db/db_impl.cc b/db/db_impl.cc index 35490a6..06752aa 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -11,9 +11,6 @@ #include #include #include -#include // For memcpy -#include // For std::time - #include "db/builder.h" #include "db/db_iter.h" @@ -37,7 +34,6 @@ #include "util/coding.h" #include "util/logging.h" #include "util/mutexlock.h" -#include "db_impl.h" namespace leveldb { @@ -1012,6 +1008,20 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { drop = true; } + // TTL ToDo: add expiration time check + if (!drop) { // 如果还未被标记为丢弃 + Slice value = input->value(); + if (value.size() >= sizeof(uint64_t)) { + const char* ptr = value.data(); + uint64_t expiration_time = DecodeFixed64(ptr); + uint64_t current_time = env_->NowMicros() / 1000000; + + if (current_time > expiration_time) { + drop = true; // 过期的键值对,标记为丢弃 + } + } + } + last_sequence_for_key = ikey.sequence; } #if 0 @@ -1024,32 +1034,6 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { (int)last_sequence_for_key, (int)compact->smallest_snapshot); #endif - //TTL ToDo: modify to add TTL check - // 添加过期时间的检查逻辑 - if (!drop) { - // 获取值 - Slice value = input->value(); - - // 检查值是否包含过期时间戳(假设过期时间戳存储在值的前8个字节) - if (value.size() >= sizeof(uint64_t)) { - const char* ptr = value.data(); - uint64_t expiration_time = DecodeFixed64(ptr); - - // 获取当前时间(单位:秒) - uint64_t current_time = env_->NowMicros() / 1000000; - - // 如果当前时间超过过期时间,则丢弃该键值对 - if (current_time > expiration_time) { - drop = true; - } else { - // 未过期,继续处理 - } - } else { - // 值中没有过期时间戳,视为未过期,继续处理 - } - } - //finish modify - if (!drop) { // Open output file if necessary if (compact->builder == nullptr) { @@ -1176,9 +1160,6 @@ int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes() { return versions_->MaxNextLevelOverlappingBytes(); } - -// TTL ToDo: modify Get function to implement TTL check - Status DBImpl::Get(const ReadOptions& options, const Slice& key, std::string* value) { Status s; @@ -1598,7 +1579,6 @@ Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value, ui } //finish modify - Status DB::Delete(const WriteOptions& opt, const Slice& key) { WriteBatch batch; batch.Delete(key); @@ -1683,5 +1663,4 @@ Status DestroyDB(const std::string& dbname, const Options& options) { return result; } - -} // namespace leveldb \ No newline at end of file +} // namespace leveldb diff --git a/db/db_impl.h b/db/db_impl.h index 9c3085f..61e13ee 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -38,9 +38,8 @@ class DBImpl : public DB { // Implementations of the DB interface Status Put(const WriteOptions&, const Slice& key, const Slice& value) override; - //TTL ToDo - Status Put(const WriteOptions& o, const Slice& key, const Slice& val, - uint64_t ttl); + Status Put(const WriteOptions& , 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/db/db_test.cc b/db/db_test.cc index a4a84cd..c2edde8 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -2117,6 +2117,13 @@ class ModelDB : public DB { Status Put(const WriteOptions& o, const Slice& k, const Slice& v) override { return DB::Put(o, k, v); } + // TTL ToDo: add DBImpl for Put + // 新增支持TTL的Put方法 + Status Put(const WriteOptions& o, const Slice& key, const Slice& val, uint64_t ttl) { + return DB::Put(o, key, val, ttl); + } + + //finish modify Status Delete(const WriteOptions& o, const Slice& key) override { return DB::Delete(o, key); } diff --git a/test/ttl_test.cc b/test/ttl_test.cc index 06f4cda..a8d611e 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -99,9 +99,7 @@ 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); }