diff --git a/utilities/ttl/db_ttl.cc b/utilities/ttl/db_ttl.cc index 27ca0433e..8f5536c7d 100644 --- a/utilities/ttl/db_ttl.cc +++ b/utilities/ttl/db_ttl.cc @@ -158,7 +158,13 @@ bool DBWithTTL::KeyMayExist(const ReadOptions& options, const Slice& key, std::string* value, bool* value_found) { - return db_->KeyMayExist(options, key, value, value_found); + bool ret = db_->KeyMayExist(options, key, value, value_found); + if (ret && value != nullptr && value_found != nullptr && *value_found) { + if (!SanityCheckTimestamp(*value).ok() || !StripTS(value).ok()) { + return false; + } + } + return ret; } Status DBWithTTL::Delete(const WriteOptions& wopts, const Slice& key) { diff --git a/utilities/ttl/ttl_test.cc b/utilities/ttl/ttl_test.cc index fdd555d45..c97e46775 100644 --- a/utilities/ttl/ttl_test.cc +++ b/utilities/ttl/ttl_test.cc @@ -139,6 +139,26 @@ class TtlTest { db_ttl_->CompactRange(nullptr, nullptr); } + // checks the whole kvmap_ to return correct values using KeyMayExist + void SimpleKeyMayExistCheck() { + static ReadOptions ropts; + bool value_found; + std::string val; + for(auto &kv : kvmap_) { + bool ret = db_ttl_->KeyMayExist(ropts, kv.first, &val, &value_found); + if (ret == false || value_found == false) { + fprintf(stderr, "KeyMayExist could not find key=%s in the database but" + " should have\n", kv.first.c_str()); + assert(false); + } else if (val.compare(kv.second) != 0) { + fprintf(stderr, " value for key=%s present in database is %s but" + " should be %s\n", kv.first.c_str(), val.c_str(), + kv.second.c_str()); + assert(false); + } + } + } + // Sleeps for slp_tim then runs a manual compaction // Checks span starting from st_pos from kvmap_ in the db and // Gets should return true if check is true and false otherwise @@ -461,6 +481,19 @@ TEST(TtlTest, CompactionFilter) { CloseTtl(); } +// Insert some key-values which KeyMayExist should be able to get and check that +// values returned are fine +TEST(TtlTest, KeyMayExist) { + MakeKVMap(kSampleSize_); + + OpenTtl(); + PutValues(0, kSampleSize_); + + SimpleKeyMayExistCheck(); + + CloseTtl(); +} + } // namespace leveldb // A black-box test for the ttl wrapper around rocksdb