| 131 | } // namespace |
| 132 | |
| 133 | TEST(Compact, Filter) { |
| 134 | Config config; |
| 135 | config.db_dir = "compactdb"; |
| 136 | config.slot_id_encoded = false; |
| 137 | |
| 138 | auto storage = std::make_unique<engine::Storage>(&config); |
| 139 | Status s = storage->Open(); |
| 140 | assert(s.IsOK()); |
| 141 | |
| 142 | uint64_t ret = 0; |
| 143 | std::string ns = "test_compact"; |
| 144 | auto hash = std::make_unique<redis::Hash>(storage.get(), ns); |
| 145 | std::string expired_hash_key = "expire_hash_key"; |
| 146 | std::string live_hash_key = "live_hash_key"; |
| 147 | |
| 148 | engine::Context ctx(storage.get()); |
| 149 | |
| 150 | hash->Set(ctx, expired_hash_key, "f1", "v1", &ret); |
| 151 | hash->Set(ctx, expired_hash_key, "f2", "v2", &ret); |
| 152 | auto st = hash->Expire(ctx, expired_hash_key, 1); // expired |
| 153 | usleep(10000); |
| 154 | hash->Set(ctx, live_hash_key, "f1", "v1", &ret); |
| 155 | hash->Set(ctx, live_hash_key, "f2", "v2", &ret); |
| 156 | |
| 157 | auto status = storage->Compact(nullptr, nullptr, nullptr); |
| 158 | assert(status.ok()); |
| 159 | // Compact twice to workaround issue fixed by: https://github.com/facebook/rocksdb/pull/11468 |
| 160 | status = storage->Compact(nullptr, nullptr, nullptr); |
| 161 | assert(status.ok()); |
| 162 | |
| 163 | rocksdb::DB *db = storage->GetDB(); |
| 164 | rocksdb::ReadOptions read_options; |
| 165 | read_options.snapshot = db->GetSnapshot(); |
| 166 | read_options.fill_cache = false; |
| 167 | |
| 168 | auto new_iterator = [db, read_options, &storage](ColumnFamilyID column_family_id) { |
| 169 | return std::unique_ptr<rocksdb::Iterator>(db->NewIterator(read_options, storage->GetCFHandle(column_family_id))); |
| 170 | }; |
| 171 | |
| 172 | auto iter = new_iterator(ColumnFamilyID::Metadata); |
| 173 | for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { |
| 174 | auto [user_ns, user_key] = ExtractNamespaceKey(iter->key(), storage->IsSlotIdEncoded()); |
| 175 | EXPECT_EQ(user_key.ToString(), live_hash_key); |
| 176 | } |
| 177 | |
| 178 | iter = new_iterator(ColumnFamilyID::PrimarySubkey); |
| 179 | for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { |
| 180 | InternalKey ikey(iter->key(), storage->IsSlotIdEncoded()); |
| 181 | EXPECT_EQ(ikey.GetKey().ToString(), live_hash_key); |
| 182 | } |
| 183 | |
| 184 | auto zset = std::make_unique<redis::ZSet>(storage.get(), ns); |
| 185 | std::string expired_zset_key = "expire_zset_key"; |
| 186 | std::vector<MemberScore> member_scores = {MemberScore{"z1", 1.1}, MemberScore{"z2", 0.4}}; |
| 187 | zset->Add(ctx, expired_zset_key, ZAddFlags::Default(), &member_scores, &ret); |
| 188 | st = zset->Expire(ctx, expired_zset_key, 1); // expired |
| 189 | usleep(10000); |
| 190 |
nothing calls this directly
no test coverage detected