| 21 | using namespace zvec::core; |
| 22 | |
| 23 | TEST(BufferStorage, General) { |
| 24 | std::string file_path = "buffer_storage_test_file"; |
| 25 | ailego::File::Delete(file_path); |
| 26 | |
| 27 | auto write_storage = IndexFactory::CreateStorage("MMapFileStorage"); |
| 28 | ASSERT_TRUE(write_storage); |
| 29 | std::cout << file_path << std::endl; |
| 30 | EXPECT_NE(0, write_storage->open(file_path, false)); |
| 31 | |
| 32 | ailego::Params params; |
| 33 | EXPECT_EQ(0, write_storage->init(params)); |
| 34 | std::cout << file_path << std::endl; |
| 35 | EXPECT_EQ(0, write_storage->open(file_path, true)); |
| 36 | |
| 37 | IndexMeta meta; |
| 38 | meta.set_trainer("trainer", 111, ailego::Params()); |
| 39 | meta.set_searcher("searcher", 222, ailego::Params()); |
| 40 | meta.set_builder("builder", 333, ailego::Params()); |
| 41 | |
| 42 | EXPECT_EQ(0, IndexHelper::SerializeToStorage(meta, write_storage.get())); |
| 43 | EXPECT_EQ(0, write_storage->append("AAAA", 1234)); |
| 44 | EXPECT_EQ(0, write_storage->append("BBBB", 1234)); |
| 45 | auto aaaa = write_storage->get("AAAA"); |
| 46 | ASSERT_TRUE(aaaa); |
| 47 | auto aaaa1 = aaaa->clone(); |
| 48 | ASSERT_TRUE(aaaa1); |
| 49 | std::string hello = "Hello world!!!"; |
| 50 | EXPECT_EQ(hello.size(), aaaa1->write(0, hello.data(), hello.size())); |
| 51 | EXPECT_EQ(0, write_storage->close()); |
| 52 | |
| 53 | // Reopen it |
| 54 | auto read_storage = IndexFactory::CreateStorage("BufferStorage"); |
| 55 | EXPECT_EQ(0, read_storage->open(file_path, false)); |
| 56 | |
| 57 | IndexMeta meta2; |
| 58 | EXPECT_EQ(0, IndexHelper::DeserializeFromStorage(read_storage.get(), &meta2)); |
| 59 | EXPECT_EQ("trainer", meta2.trainer_name()); |
| 60 | EXPECT_EQ("searcher", meta2.searcher_name()); |
| 61 | EXPECT_EQ("builder", meta2.builder_name()); |
| 62 | auto aaaa2 = read_storage->get("AAAA"); |
| 63 | ASSERT_TRUE(aaaa2); |
| 64 | IndexStorage::MemoryBlock data; |
| 65 | EXPECT_EQ(hello.size(), aaaa2->read(0, data, hello.size())); |
| 66 | auto aaaa3 = aaaa2->clone(); |
| 67 | ASSERT_TRUE(aaaa3); |
| 68 | EXPECT_EQ(hello.size(), aaaa3->read(0, data, hello.size())); |
| 69 | EXPECT_EQ(hello, std::string((const char *)data.data(), hello.size())); |
| 70 | } |
nothing calls this directly
no test coverage detected