LevelDB二级索引实现 姚凯文(kevinyao0901) 姜嘉祺
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.

93 lines
3.2 KiB

  1. #include <iostream>
  2. #include <cassert>
  3. #include <vector>
  4. #include "leveldb/db.h"
  5. // 为字段创建索引
  6. void TestCreateIndexOnField(leveldb::DB* db) {
  7. // 创建索引
  8. leveldb::Status status = db->CreateIndexOnField("name");
  9. assert(status.ok() && "Failed to create index on field 'name'");
  10. std::cout << "Index on 'name' created successfully." << std::endl;
  11. }
  12. // 按照索引查询
  13. void TestQueryByIndex(leveldb::DB* db) {
  14. // 插入一些数据
  15. leveldb::WriteOptions write_options;
  16. leveldb::Status status;
  17. // 插入记录,格式为 "name:Customer#000000001|address:IVhzIApeRb|phone:25-989-741-2988"
  18. status = db->Put(write_options, "k_1", "name:Customer#000000001|address:IVhzIApeRb|phone:25-989-741-2988");
  19. assert(status.ok() && "Failed to insert data for k_1");
  20. status = db->Put(write_options, "k_2", "name:Customer#000000002|address:XSTf4,NCwDVaW|phone:23-768-687-3665");
  21. assert(status.ok() && "Failed to insert data for k_2");
  22. status = db->Put(write_options, "k_3", "name:Customer#000000003|address:MG9kdTD2WBHm|phone:11-719-748-3364");
  23. assert(status.ok() && "Failed to insert data for k_3");
  24. status = db->Put(write_options, "k_4", "name:Customer#000000004|address:XxVSJsLAGtn|phone:14-128-190-5944");
  25. assert(status.ok() && "Failed to insert data for k_4");
  26. status = db->Put(write_options, "k_5", "name:Customer#000000005|address:KvpyuHCplrB84Wg|phone:13-750-942-6364");
  27. assert(status.ok() && "Failed to insert data for k_5");
  28. std::cout << "Data inserted successfully." << std::endl;
  29. // 查询索引(我们假设查询字段值为 name:Customer#000000001)
  30. std::vector<std::string> results = db->QueryByIndex("name:Customer#000000001");
  31. assert(!results.empty() && "Query by index returned no results");
  32. std::cout << "Query by index results for name=Customer#000000001: ";
  33. for (const auto& result : results) {
  34. std::cout << result << ", ";
  35. }
  36. std::cout << std::endl;
  37. // 验证查询结果
  38. assert(results.size() == 1 && results[0] == "k_1");
  39. std::cout << "Query by index test passed." << std::endl;
  40. }
  41. // 删除索引测试
  42. void TestDeleteIndex(leveldb::DB* db) {
  43. // 删除索引
  44. leveldb::Status status = db->DeleteIndex("name");
  45. assert(status.ok() && "Failed to delete index on field 'name'");
  46. std::cout << "Index on 'name' deleted successfully." << std::endl;
  47. // 尝试查询已删除的索引
  48. std::vector<std::string> results = db->QueryByIndex("name:Customer#000000001");
  49. assert(results.empty() && "Query on deleted index should return no results");
  50. std::cout << "Query after deleting index returned no results as expected." << std::endl;
  51. }
  52. int main() {
  53. // 配置数据库选项
  54. leveldb::Options options;
  55. options.create_if_missing = true;
  56. // 打开数据库
  57. leveldb::DB* db = nullptr;
  58. leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
  59. assert(status.ok() && "Failed to open database");
  60. // 测试创建索引
  61. TestCreateIndexOnField(db);
  62. // 测试查询索引
  63. TestQueryByIndex(db);
  64. // 测试删除索引
  65. TestDeleteIndex(db);
  66. // 关闭数据库
  67. delete db;
  68. std::cout << "All tests passed." << std::endl;
  69. return 0;
  70. }