From 85d42da3cfe3c093a2658d9f99dc49dcc917fda1 Mon Sep 17 00:00:00 2001 From: kevinyao0901 Date: Thu, 31 Oct 2024 16:59:05 +0800 Subject: [PATCH] finish --- db/db_impl.cc | 10 +++++++++- test/ttl_test.cc | 17 +++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index ba61336..f289461 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -621,7 +621,7 @@ void DBImpl::CompactRange(const Slice* begin, const Slice* end) { } } TEST_CompactMemTable(); // TODO(sanjay): Skip if memtable does not overlap - for (int level = 0; level < max_level_with_files; level++) { + for (int level = 0; level <= max_level_with_files; level++) { TEST_CompactRange(level, begin, end); } } @@ -935,6 +935,12 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { int dropped_keys_count = 0; // 初始化计数器 int total_keys_count = 0; + std::cout << "Level 0: "; + for(int i=0;icompaction->num_input_files(0);i++) { + auto f = compact->compaction->input(0, i); + std::cout << f->number << " "; + } + Log(options_.info_log, "Compacting %d@%d + %d@%d files", compact->compaction->num_input_files(0), compact->compaction->level(), compact->compaction->num_input_files(1), @@ -1065,6 +1071,8 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { last_sequence_for_key = ikey.sequence; } + input->SeekToLast(); + std::cout << "Compation last key: " << input->key().ToString() << std::endl; Log(options_.info_log, "Total dropped keys in compaction: %d\n", dropped_keys_count); // 输出统计结果 total_keys_count++; diff --git a/test/ttl_test.cc b/test/ttl_test.cc index f71a306..0271c9e 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -21,12 +21,15 @@ void PrintAllKeys(DB *db) { // 创建迭代器 std::unique_ptr it(db->NewIterator(readOptions)); + int cnt = 20; + // 遍历所有键 - for (it->SeekToFirst(); it->Valid(); it->Next()) { - // std::string key = it->key().ToString(); - // std::string value = it->value().ToString(); - // std::cout << "Key: " << key << std::endl; + for (it->SeekToFirst(); it->Valid()&&cnt; it->Next()) { + std::string key = it->key().ToString(); + std::string value = it->value().ToString(); + std::cout << "Key: " << key << std::endl; LeftKeyCount++; + cnt--; } // 检查迭代器的有效性 @@ -82,6 +85,8 @@ void InsertData(DB *db, uint64_t ttl/* second */) { Iterator* iter = db->NewIterator(ReadOptions()); iter->SeekToFirst(); std::cout << "Data base First key: " << iter->key().ToString() << std::endl; + iter->SeekToLast(); + std::cout << "Data base last key: " << iter->key().ToString() << std::endl; delete iter; // 打印成功写入的唯一键的数量 @@ -115,7 +120,7 @@ void GetData(DB *db, int size = (1 << 30)) { } -#if 0 + TEST(TestTTL, ReadTTL) { DB *db; if(OpenDB("testdb", &db).ok() == false) { @@ -165,7 +170,7 @@ TEST(TestTTL, ReadTTL) { delete db; } -#endif + TEST(TestTTL, CompactionTTL) { DB *db;