Browse Source

TestTTL.ReadTTL passed

master
马也驰 1 month ago
parent
commit
2927716e49
4 changed files with 43 additions and 9 deletions
  1. +31
    -0
      db/db_impl.cc
  2. +3
    -0
      db/db_impl.h
  3. +4
    -4
      include/leveldb/db.h
  4. +5
    -5
      test/ttl_test.cc

+ 31
- 0
db/db_impl.cc View File

@ -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<uint64_t>(std::stoll(substring));
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
auto microsecondsTimestamp = static_cast<uint64_t>(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<std::chrono::microseconds>(now.time_since_epoch()).count();
auto microsecondsTimestamp = static_cast<uint64_t>(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);
}

+ 3
- 0
db/db_impl.h View File

@ -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,

+ 4
- 4
include/leveldb/db.h View File

@ -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.

+ 5
- 5
test/ttl_test.cc View File

@ -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);
}

Loading…
Cancel
Save