void Database::CompactIndex(const string& index_name, lsn_t snapshot_lsn) { if (snapshot_lsn == indices[index_name]->GetLastPersistentLSN()) { return; // nothing to do } LookupIterator snapshot = indices[index_name]->GetSnapshot(snapshot_lsn); ImmutableIndexWriter* writer = ImmutableIndex::Create( GetIndexPrefix(name, index_name), snapshot_lsn, 64*1024); while (snapshot.hasMore()) { writer->Add((*snapshot).first, (*snapshot).second); ++snapshot; } writer->Finalize(); }
TEST_TMPDIR(ImmutableIndex, babudb) { ImmutableIndexWriter* writer = ImmutableIndex::Create( testPath("testdb-testidx"), 4, 64 * 1024); Buffer k1("key1"), v1("value1"), k2("key2"), v2("value2"); Buffer k3("key3"), v3("value3"), k4("key4"), v4("value4"); writer->Add(k1,v1); writer->Add(k2,v2); writer->Add(k3,v3); writer->Add(k4,v4); writer->Finalize(); delete writer; StringOrder myorder; ImmutableIndex::DiskIndices indices = ImmutableIndex::FindIndices( testPath("testdb-testidx")); ImmutableIndex* index = ImmutableIndex::LoadLatestIntactIndex( indices, myorder); EXPECT_TRUE(index->Find(Buffer(k1)) != index->end()); EXPECT_TRUE(index->Find(Buffer(k3)) != index->end()); EXPECT_TRUE(index->Find(Buffer(k2)) != index->end()); EXPECT_TRUE(index->Find(Buffer(k4)) != index->end()); Buffer prefix("ke"); EXPECT_TRUE(index->Find(Buffer(prefix)) != index->end()); // search keys directly EXPECT_TRUE(!index->Lookup(k1).isEmpty()); EXPECT_TRUE(!index->Lookup(k2).isEmpty()); EXPECT_TRUE(index->Lookup(k2) == v2); EXPECT_TRUE(!index->Lookup(k3).isEmpty()); EXPECT_TRUE(!index->Lookup(k4).isEmpty()); // not founds Buffer before("a"); EXPECT_TRUE(index->Lookup(before).isNotExists()); Buffer middle("key21"); EXPECT_TRUE(index->Lookup(middle).isNotExists()); Buffer after("x"); EXPECT_TRUE(index->Lookup(after).isNotExists()); // and iteration ImmutableIndex::iterator i = index->begin(); EXPECT_TRUE(!(*i).first.isEmpty()); EXPECT_TRUE(i != index->end()); ++i; EXPECT_TRUE(!(*i).first.isEmpty()); EXPECT_TRUE(i != index->end()); ++i; EXPECT_TRUE(!(*i).first.isEmpty()); EXPECT_TRUE(i != index->end()); ++i; EXPECT_TRUE(!(*i).first.isEmpty()); EXPECT_TRUE(i != index->end()); ++i; EXPECT_TRUE(i == index->end()); // matches ImmutableIndex::iterator pi = index->Find(prefix); EXPECT_TRUE(pi != index->end()); EXPECT_TRUE((*pi).first == "key1"); EXPECT_TRUE(pi != index->end()); EXPECT_TRUE(index->Find(Buffer("kez")) == index->end() ); }