You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

преди 2 седмици
  1. #include <leveldb/db.h>
  2. #include "leveldb/env.h"
  3. #include <leveldb/options.h>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7. #include <iostream>
  8. #include "gtest/gtest.h"
  9. using namespace leveldb;
  10. using Field = std::pair<std::string, std::string>; // field_name:field_value
  11. using FieldArray = std::vector<std::pair<std::string, std::string>>;
  12. // 序列化为字符串
  13. std::string SerializeValue(const FieldArray& fields) {
  14. std::ostringstream oss;
  15. for (const auto& field : fields) {
  16. oss << field.first << ":" << field.second << ";";
  17. }
  18. return oss.str();
  19. }
  20. // 反序列化为字段数组
  21. FieldArray ParseValue(const std::string& value_str) {
  22. FieldArray fields;
  23. std::istringstream iss(value_str);
  24. std::string field_str;
  25. while (std::getline(iss, field_str, ';')) {
  26. size_t delimiter_pos = field_str.find(':');
  27. if (delimiter_pos != std::string::npos) {
  28. std::string field_name = field_str.substr(0, delimiter_pos);
  29. std::string field_value = field_str.substr(delimiter_pos + 1);
  30. fields.emplace_back(field_name, field_value);
  31. }
  32. }
  33. return fields;
  34. }
  35. // 根据字段值查找所有包含该字段的 key
  36. std::vector<std::string> FindKeysByField(leveldb::DB* db, Field &field) {
  37. std::vector<std::string> keys;
  38. leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
  39. for (it->SeekToFirst(); it->Valid(); it->Next()) {
  40. std::string key = it->key().ToString();
  41. std::string value;
  42. db->Get(leveldb::ReadOptions(), key, &value);
  43. FieldArray fields = ParseValue(value);
  44. for (const auto& f : fields) {
  45. if (f.first == field.first && f.second == field.second) {
  46. keys.push_back(key);
  47. break; // 假设每个key中每个字段值唯一,如果允许重复,可以移除这行
  48. }
  49. }
  50. }
  51. delete it;
  52. return keys;
  53. }
  54. Status OpenDB(std::string dbName, DB **db) {
  55. Options options;
  56. options.create_if_missing = true;
  57. return DB::Open(options, dbName, db);
  58. }
  59. TEST(TestSchema, Basic) {
  60. DB *db;
  61. WriteOptions writeOptions;
  62. ReadOptions readOptions;
  63. if(OpenDB("testdb", &db).ok() == false) {
  64. std::cerr << "open db failed" << std::endl;
  65. abort();
  66. }
  67. std::string key1 = "k_1";
  68. std::string key2 = "k_2";
  69. FieldArray fields1 = {
  70. {"name", "Customer#000000001"},
  71. {"address", "IVhzIApeRb"},
  72. {"phone", "25-989-741-2988"}
  73. };
  74. FieldArray fields2 = {
  75. {"name", "Customer#000000001"},
  76. {"address", "ecnu"},
  77. {"phone", "123456789"}
  78. };
  79. // 序列化并插入
  80. std::string value1 = SerializeValue(fields1);
  81. std::string value2 = SerializeValue(fields2);
  82. db->Put(leveldb::WriteOptions(), key1, value1);
  83. db->Put(leveldb::WriteOptions(), key2, value2);
  84. // 读取并反序列化
  85. std::string value_ret;
  86. db->Get(leveldb::ReadOptions(), key1, &value_ret);
  87. auto fields_ret = ParseValue(value_ret);
  88. // 检查反序列化结果
  89. ASSERT_EQ(fields_ret.size(), fields1.size());
  90. for (size_t i = 0; i < fields_ret.size(); ++i) {
  91. ASSERT_EQ(fields_ret[i].first, fields1[i].first);
  92. ASSERT_EQ(fields_ret[i].second, fields1[i].second);
  93. }
  94. // 测试查找功能
  95. Field query_field = {"name", "Customer#000000001"};
  96. std::vector<std::string> found_keys = FindKeysByField(db, query_field);
  97. std::cout << "找到的key有:" << found_keys.size() << "" << std::endl;
  98. ASSERT_EQ(found_keys[0], key1);
  99. // 关闭数据库
  100. delete db;
  101. }
  102. int main(int argc, char **argv) {
  103. testing::InitGoogleTest(&argc, argv);
  104. return RUN_ALL_TESTS();
  105. }