diff --git a/test/kv_seperate_test.cc b/test/kv_seperate_test.cc index ef69179..88b59fc 100644 --- a/test/kv_seperate_test.cc +++ b/test/kv_seperate_test.cc @@ -1,119 +1,119 @@ -#include "gtest/gtest.h" -#include "leveldb/env.h" -#include "leveldb/db.h" -#include "table/blob_file.h" // 假设 BlobFile 的头文件 - -using namespace leveldb; - -constexpr int value_size = 2048; // 单个值的大小 -constexpr int data_size = 128 << 20; // 总数据大小 -constexpr int min_blob_size = 1024; // KV 分离的阈值 - -Status OpenDB(std::string dbName, DB** db) { - Options options; - options.create_if_missing = true; - options.key_value_separated = true; // 启用 KV 分离 - return DB::Open(options, dbName, db); -} - -// 插入数据,模拟 KV 分离 -void InsertData(DB* db) { - WriteOptions writeOptions; - int key_num = data_size / value_size; - srand(static_cast(time(0))); - - for (int i = 0; i < key_num; i++) { - int key_ = rand() % key_num + 1; - std::string key = std::to_string(key_); - std::string value(value_size, 'a'); // 大 value - db->Put(writeOptions, key, value); // 使用标准 Put 接口插入 - } -} - -// 检查数据是否被正确存入 BlobFile -void VerifyBlobFile(const std::string& blob_file_path, int expected_entries) { - BlobFile blobfile(blob_file_path, BlobFile::kReadMode); - Status status = blobfile.Open(); - ASSERT_TRUE(status.ok()); - - int entry_count = 0; - BlobFile::Iterator it = blobfile.NewIterator(); - for (it.SeekToFirst(); it.Valid(); it.Next()) { - ++entry_count; - const Slice& key = it.key(); - const Slice& value = it.value(); - ASSERT_GT(value.size(), min_blob_size); // 确认 value 大于阈值 - } - - ASSERT_EQ(entry_count, expected_entries); // 确认条目数是否正确 - blobfile.Close(); -} - -// KV 分离读写测试 -TEST(TestKVSeparation, WriteAndRead) { - DB* db; - if (OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - // 插入数据 - InsertData(db); - - // 验证 BlobFile 内容 - VerifyBlobFile("blob_data", data_size / value_size); - - // 随机点查数据 - ReadOptions readOptions; - srand(static_cast(time(0))); - int key_num = data_size / value_size; - for (int i = 0; i < 100; i++) { - int key_ = rand() % key_num + 1; - std::string key = std::to_string(key_); - std::string value; - Status status = db->Get(readOptions, key, &value); - ASSERT_TRUE(status.ok()); // 验证是否成功读取 - if (value.size() > min_blob_size) { - ASSERT_TRUE(value == std::string(value_size, 'a')); // 验证大 value 的内容 - } - } - - delete db; -} - -// KV 分离压缩测试 -TEST(TestKVSeparation, Compaction) { - DB* db; - - if (OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - // 插入数据 - InsertData(db); - - leveldb::Range ranges[1]; - ranges[0] = leveldb::Range("-", "A"); - uint64_t sizes[1]; - db->GetApproximateSizes(ranges, 1, sizes); - ASSERT_GT(sizes[0], 0); - - // 执行压缩 - db->CompactRange(nullptr, nullptr); - - // 验证压缩后主数据区的大小 - ranges[0] = leveldb::Range("-", "A"); - db->GetApproximateSizes(ranges, 1, sizes); - ASSERT_EQ(sizes[0], 0); - - // 验证 BlobFile 内容仍然有效 - VerifyBlobFile("blob_data", data_size / value_size); - - delete db; -} - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +#include "gtest/gtest.h" +#include "leveldb/env.h" +#include "leveldb/db.h" +#include "table/blob_file.h" // 假设 BlobFile 的头文件 + +using namespace leveldb; + +constexpr int value_size = 2048; // 单个值的大小 +constexpr int data_size = 128 << 20; // 总数据大小 +constexpr int min_blob_size = 1024; // KV 分离的阈值 + +Status OpenDB(std::string dbName, DB** db) { + Options options; + options.create_if_missing = true; + options.key_value_separated = true; // 启用 KV 分离 + return DB::Open(options, dbName, db); +} + +// 插入数据,模拟 KV 分离 +void InsertData(DB* db) { + WriteOptions writeOptions; + int key_num = data_size / value_size; + srand(static_cast(time(0))); + + for (int i = 0; i < key_num; i++) { + int key_ = rand() % key_num + 1; + std::string key = std::to_string(key_); + std::string value(value_size, 'a'); // 大 value + db->Put(writeOptions, key, value); // 使用标准 Put 接口插入 + } +} + +// 检查数据是否被正确存入 BlobFile +void VerifyBlobFile(const std::string& blob_file_path, int expected_entries) { + BlobFile blobfile(blob_file_path, BlobFile::kReadMode); + Status status = blobfile.Open(); + ASSERT_TRUE(status.ok()); + + int entry_count = 0; + BlobFile::Iterator it = blobfile.NewIterator(); + for (it.SeekToFirst(); it.Valid(); it.Next()) { + ++entry_count; + const Slice& key = it.key(); + const Slice& value = it.value(); + ASSERT_GT(value.size(), min_blob_size); // 确认 value 大于阈值 + } + + ASSERT_EQ(entry_count, expected_entries); // 确认条目数是否正确 + blobfile.Close(); +} + +// KV 分离读写测试 +TEST(TestKVSeparation, WriteAndRead) { + DB* db; + if (OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } + + // 插入数据 + InsertData(db); + + // 验证 BlobFile 内容 + VerifyBlobFile("blob_data", data_size / value_size); + + // 随机点查数据 + ReadOptions readOptions; + srand(static_cast(time(0))); + int key_num = data_size / value_size; + for (int i = 0; i < 100; i++) { + int key_ = rand() % key_num + 1; + std::string key = std::to_string(key_); + std::string value; + Status status = db->Get(readOptions, key, &value); + ASSERT_TRUE(status.ok()); // 验证是否成功读取 + if (value.size() > min_blob_size) { + ASSERT_TRUE(value == std::string(value_size, 'a')); // 验证大 value 的内容 + } + } + + delete db; +} + +// KV 分离压缩测试 +TEST(TestKVSeparation, Compaction) { + DB* db; + + if (OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } + + // 插入数据 + InsertData(db); + + leveldb::Range ranges[1]; + ranges[0] = leveldb::Range("-", "A"); + uint64_t sizes[1]; + db->GetApproximateSizes(ranges, 1, sizes); + ASSERT_GT(sizes[0], 0); + + // 执行压缩 + db->CompactRange(nullptr, nullptr); + + // 验证压缩后主数据区的大小 + ranges[0] = leveldb::Range("-", "A"); + db->GetApproximateSizes(ranges, 1, sizes); + ASSERT_EQ(sizes[0], 0); + + // 验证 BlobFile 内容仍然有效 + VerifyBlobFile("blob_data", data_size / value_size); + + delete db; +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}