diff --git a/db/db_impl.cc b/db/db_impl.cc index 62824ed..35490a6 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -11,6 +11,9 @@ #include #include #include +#include // For memcpy +#include // For std::time + #include "db/builder.h" #include "db/db_iter.h" @@ -34,11 +37,41 @@ #include "util/coding.h" #include "util/logging.h" #include "util/mutexlock.h" +#include "db_impl.h" namespace leveldb { const int kNumNonTableCacheFiles = 10; +//TTL ToDo : add func for TTL Put + +void AppendExpirationTime(std::string* value, uint64_t expiration_time) { + // 将过期时间戳(64位整数)附加到值的前面 + value->append(reinterpret_cast(&expiration_time), sizeof(expiration_time)); +} + +uint64_t GetCurrentTime() { + // 返回当前的Unix时间戳 + return static_cast(time(nullptr)); +} + +// 解析过期时间戳 +uint64_t ParseExpirationTime(const std::string& value) { + // 假设过期时间戳在值的前 8 字节,以大端序存储 + assert(value.size() >= sizeof(uint64_t)); + uint64_t expiration_time; + memcpy(&expiration_time, value.data(), sizeof(uint64_t)); + return expiration_time; +} + +// 解析出实际的值(去掉前面的过期时间戳部分) +std::string ParseActualValue(const std::string& value) { + // 去掉前 8 字节(存储过期时间戳),返回实际值 + return value.substr(sizeof(uint64_t)); +} + +//finish modify + // Information kept for every waiting writer struct DBImpl::Writer { explicit Writer(port::Mutex* mu) @@ -1650,33 +1683,5 @@ Status DestroyDB(const std::string& dbname, const Options& options) { return result; } -// TTL ToDo: add func for TTL get -uint64_t ParseExpirationTime(const std::string& value) { - // 假设时间戳存储在值的前8个字节(64位整数) - uint64_t expiration_time; - memcpy(&expiration_time, value.data(), sizeof(uint64_t)); - return expiration_time; -} - -std::string ParseActualValue(const std::string& value) { - // 假设实际值存储在过期时间戳之后 - return value.substr(sizeof(uint64_t)); -} - -//finish modify - -//TTL ToDo : add func for TTL Put - -void AppendExpirationTime(std::string* value, uint64_t expiration_time) { - // 将过期时间戳(64位整数)附加到值的前面 - value->append(reinterpret_cast(&expiration_time), sizeof(expiration_time)); -} - -uint64_t GetCurrentTime() { - // 返回当前的Unix时间戳 - return static_cast(time(nullptr)); -} - -//finish modify -} // namespace leveldb +} // namespace leveldb \ No newline at end of file diff --git a/db/db_impl.h b/db/db_impl.h index c7b0172..9c3085f 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; + //TTL ToDo + Status Put(const WriteOptions& o, const Slice& key, const Slice& val, + uint64_t ttl); Status Delete(const WriteOptions&, const Slice& key) override; Status Write(const WriteOptions& options, WriteBatch* updates) override; Status Get(const ReadOptions& options, const Slice& key,