diff --git a/db/db_impl.cc b/db/db_impl.cc index 21bf63a..6f203ac 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -594,6 +594,8 @@ void DBImpl::CompactRange(const Slice* begin, const Slice* end) { for (int level = 0; level < max_level_with_files; level++) { TEST_CompactRange(level, begin, end); } + /// For TTL + TEST_CompactRange(max_level_with_files, begin, end); } void DBImpl::TEST_CompactRange(int level, const Slice* begin, @@ -735,21 +737,21 @@ void DBImpl::BackgroundCompaction() { if (c == nullptr) { // Nothing to do } else if (!is_manual && c->IsTrivialMove()) { - // Move file to next level - assert(c->num_input_files(0) == 1); - FileMetaData* f = c->input(0, 0); - c->edit()->RemoveFile(c->level(), f->number); - c->edit()->AddFile(c->level() + 1, f->number, f->file_size, f->smallest, - f->largest); - status = versions_->LogAndApply(c->edit(), &mutex_); - if (!status.ok()) { - RecordBackgroundError(status); - } - VersionSet::LevelSummaryStorage tmp; - Log(options_.info_log, "Moved #%lld to level-%d %lld bytes %s: %s\n", - static_cast(f->number), c->level() + 1, - static_cast(f->file_size), - status.ToString().c_str(), versions_->LevelSummary(&tmp)); +// // Move file to next level +// assert(c->num_input_files(0) == 1); +// FileMetaData* f = c->input(0, 0); +// c->edit()->RemoveFile(c->level(), f->number); +// c->edit()->AddFile(c->level() + 1, f->number, f->file_size, f->smallest, +// f->largest); +// status = versions_->LogAndApply(c->edit(), &mutex_); +// if (!status.ok()) { +// RecordBackgroundError(status); +// } +// VersionSet::LevelSummaryStorage tmp; +// Log(options_.info_log, "Moved #%lld to level-%d %lld bytes %s: %s\n", +// static_cast(f->number), c->level() + 1, +// static_cast(f->file_size), +// status.ToString().c_str(), versions_->LevelSummary(&tmp)); } else { CompactionState* compact = new CompactionState(c); status = DoCompactionWork(compact); @@ -979,6 +981,21 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { drop = true; } + /// For TTL + Slice value_ddl = input->value(); + std::string value = value_ddl.ToString(); + size_t pos = value.find_last_of('_'); + if (pos != std::string::npos) { + std::string substring = value.substr(pos + 1); + auto ddl = static_cast(std::stoll(substring)); + auto now = std::chrono::system_clock::now(); + auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); + auto microsecondsTimestamp = static_cast(timestamp); + if (ddl <= microsecondsTimestamp) { + drop = true; + } + } + last_sequence_for_key = ikey.sequence; } #if 0 diff --git a/test/ttl_test.cc b/test/ttl_test.cc index 7402893..2df8067 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -81,7 +81,7 @@ TEST(TestTTL, ReadTTL) { TEST(TestTTL, CompactionTTL) { DB *db; - if(OpenDB("testdb", &db).ok() == false) { + if(OpenDB("testdb_", &db).ok() == false) { std::cerr << "open db failed" << std::endl; abort(); }