diff --git a/include/rocksdb/utilities/db_ttl.h b/include/rocksdb/utilities/db_ttl.h index 7c9c0cc55..b40919a0f 100644 --- a/include/rocksdb/utilities/db_ttl.h +++ b/include/rocksdb/utilities/db_ttl.h @@ -60,6 +60,10 @@ class DBWithTTL : public StackableDB { DBWithTTL** dbptr, std::vector ttls, bool read_only = false); + virtual void SetTtl(int32_t ttl) = 0; + + virtual void SetTtl(ColumnFamilyHandle *h, int32_t ttl) = 0; + protected: explicit DBWithTTL(DB* db) : StackableDB(db) {} }; diff --git a/utilities/ttl/db_ttl_impl.cc b/utilities/ttl/db_ttl_impl.cc index de3f2995c..1e8d5d0e8 100644 --- a/utilities/ttl/db_ttl_impl.cc +++ b/utilities/ttl/db_ttl_impl.cc @@ -309,5 +309,16 @@ Iterator* DBWithTTLImpl::NewIterator(const ReadOptions& opts, return new TtlIterator(db_->NewIterator(opts, column_family)); } +void DBWithTTLImpl::SetTtl(ColumnFamilyHandle *h, int32_t ttl) { + std::shared_ptr filter; + Options opts; + opts = GetOptions(h); + filter = std::static_pointer_cast( + opts.compaction_filter_factory); + if (!filter) + return; + filter->SetTtl(ttl); +} + } // namespace rocksdb #endif // ROCKSDB_LITE diff --git a/utilities/ttl/db_ttl_impl.h b/utilities/ttl/db_ttl_impl.h index ab2b8cdf7..1148f4f90 100644 --- a/utilities/ttl/db_ttl_impl.h +++ b/utilities/ttl/db_ttl_impl.h @@ -94,6 +94,10 @@ class DBWithTTLImpl : public DBWithTTL { static const int32_t kMinTimestamp = 1368146402; // 05/09/2013:5:40PM GMT-8 static const int32_t kMaxTimestamp = 2147483647; // 01/18/2038:7:14PM GMT-8 + + void SetTtl(int32_t ttl) override { SetTtl(DefaultColumnFamily(), ttl); } + + void SetTtl(ColumnFamilyHandle *h, int32_t ttl) override; }; class TtlIterator : public Iterator { @@ -209,6 +213,10 @@ class TtlCompactionFilterFactory : public CompactionFilterFactory { ttl_, env_, nullptr, std::move(user_comp_filter_from_factory))); } + void SetTtl(int32_t ttl) { + ttl_ = ttl; + } + virtual const char* Name() const override { return "TtlCompactionFilterFactory"; } diff --git a/utilities/ttl/ttl_test.cc b/utilities/ttl/ttl_test.cc index 586d0ce1f..dd1ef6e75 100644 --- a/utilities/ttl/ttl_test.cc +++ b/utilities/ttl/ttl_test.cc @@ -283,6 +283,12 @@ class TtlTest : public testing::Test { delete dbiter; } + // Set ttl on open db + void SetTtl(int32_t ttl, ColumnFamilyHandle* cf = nullptr) { + ASSERT_TRUE(db_ttl_); + cf == nullptr ? db_ttl_->SetTtl(ttl) : db_ttl_->SetTtl(cf, ttl); + } + class TestFilter : public CompactionFilter { public: TestFilter(const int64_t kSampleSize, const std::string& kNewValue) @@ -626,6 +632,17 @@ TEST_F(TtlTest, ColumnFamiliesTest) { db_ttl_ = nullptr; } +// Puts a set of values and checks its absence using Get after ttl +TEST_F(TtlTest, ChangeTtlOnOpenDb) { + MakeKVMap(kSampleSize_); + + OpenTtl(1); // T=0:Open the db with ttl = 2 + SetTtl(3); + PutValues(0, kSampleSize_); // T=0:Insert Set1. Delete at t=2 + SleepCompactCheck(2, 0, kSampleSize_, true); // T=2:Set1 should be there + CloseTtl(); +} + } // namespace rocksdb // A black-box test for the ttl wrapper around rocksdb