bool CompositedSSTableWriter::Flush() { if (curr_byte_ >= 0) { if (!(builder_->Flush())) { DeleteTmpFiles(); return false; } } MergedSSTableReader sstable; UnsortedSSTableWriter builder(option_); if (!sstable.Open(paths_, SSTableReader::ON_DISK, true)) { LOG(ERROR)<< "error open sstable!"; DeleteTmpFiles(); return false; } toft::scoped_ptr<SSTableReader::Iterator> iter(sstable.NewIterator()); for (; iter->Valid(); iter->Next()) { if (!(builder.Add(iter->key(), iter->value()))) { DeleteTmpFiles(); return false; } } std::map<std::string, std::string>::iterator it_fi_meta = file_info_meta_.begin(); for (; it_fi_meta != file_info_meta_.end(); ++it_fi_meta) { builder.AddMetaData(it_fi_meta->first, it_fi_meta->second); } builder.Flush(); DeleteTmpFiles(); return true; }
TEST(MergedSSTableReader, ReadMultiFiles) { SSTableWriteOption option; std::string path = "/tmp/test_single_lzo_disk.sstable"; option.set_path(path); option.set_compress_type(CompressType_kSnappy); SingleSSTableWriter builder(option); SSTableWriteOption option1; std::string path1 = "/tmp/test_unsorted_lzo_disk.sstable"; option1.set_path(path1); option1.set_compress_type(CompressType_kSnappy); UnsortedSSTableWriter builder1(option1); LOG(INFO)<< "Start build sstable :" << path; LOG(INFO)<< "Start build sstable :" << path1; for (int i = 0; i < kTestNum; ++i) { std::string key = GenKey(i, kMaxLength); std::string value = GenValue(i, kMaxLength); builder.Add(key, value); builder1.Add(key, value); if (i % 1000 == 0) { builder.AddMetaData(key, value); builder1.AddMetaData(key, value); } } LOG(INFO)<< "Start flush ..."; EXPECT_TRUE(builder.Flush()); EXPECT_TRUE(builder1.Flush()); LOG(INFO)<< "Finish flush!"; // open a sstable std::vector<std::string> paths; paths.push_back(path); paths.push_back(path1); MergedSSTableReader sstable; if (!sstable.Open(paths, SSTableReader::ON_DISK, true)) { LOG(INFO)<< "error open sstable!"; } EXPECT_EQ(sstable.EntryCount(), 2 * kTestNum); toft::scoped_ptr<SSTableReader::Iterator> iter(sstable.NewIterator()); LOG(INFO)<< "start iteration"; for (int i = 0; i < kTestNum; ++i) { std::string key = GenKey(i, kMaxLength); std::string value = GenValue(i, kMaxLength); iter.reset(sstable.Seek(key)); EXPECT_TRUE(iter->Valid()) << i; EXPECT_EQ(key, iter->key()) << i; EXPECT_EQ(key + "_value", iter->value()) << i; iter->Next(); EXPECT_TRUE(iter->Valid()) << i; EXPECT_EQ(key, iter->key()) << i; EXPECT_EQ(key + "_value", iter->value()) << i; iter->Next(); } ASSERT_FALSE(iter->Valid()); LOG(INFO)<< "finish iteration"; LOG(INFO)<< "start meta iteration"; toft::Closure<bool(const std::string &, const std::string &)> *callback = // NOLINT toft::NewPermanentClosure(TestMetaData); sstable.IterateMetaData(callback); delete callback; LOG(INFO)<< "finish meta iteration"; }