

#include "gtest/gtest.h"

#include "leveldb/env.h"
#include "leveldb/db.h"
// 这个头文件是为了EncodeFixed32
#include "util/coding.h"

using namespace leveldb;
using Field = std::pair<std::string, std::string>;
using FieldArray = std::vector<std::pair<std::string, std::string>>;
constexpr int value_size = 2048;
constexpr int data_size = 128 << 20;

Status OpenDB(std::string dbName, DB **db) {
  Options options;
  options.create_if_missing = true;
  return DB::Open(options, dbName, db);
}

std::string SerializeValue(const FieldArray& fields){
  std::string value_str;
  for(const auto& pair : fields){
    std::string field = pair.first + ":" + pair.second;
    uint32_t field_size = field.size();
    char buffer[4];
    EncodeFixed32(buffer, field_size);
    value_str.append(buffer, 4);
    value_str.append(field);
  }
  return value_str;
}

FieldArray ParseValue(const std::string& value_str){
  FieldArray fields;
  const char* data = value_str.data();
  size_t length = value_str.size();

  while (length >= 4) {
    uint32_t field_size = DecodeFixed32(data);
    if (length < 4 + field_size) {
      break;
    }

    std::string field(data + 4, field_size);
    //转义
    size_t colon_pos = field.find(':');

    std::string field_name = field.substr(0, colon_pos);
    std::string field_value = field.substr(colon_pos + 1);

    fields.push_back(std::make_pair(field_name,field_value));

    data += 4 + field_size;
    length -= 4 + field_size;
  }

  return fields;
}

std::string ExtractField(const std::string& value, const std::string& field_name) {
  FieldArray fields = ParseValue(value);
  for(const auto& field : fields){
    if(field.first == field_name){
     return field.second;
    }
  }
  return "No such field_name";
}
std::vector<std::string> FindKeysByField(leveldb::DB* db, Field &field) {
  std::vector<std::string> matching_keys;
  leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());

  for (it->SeekToFirst(); it->Valid(); it->Next()) {
    std::string key = it->key().ToString();
    std::string value = it->value().ToString();
    if (ExtractField(value, field.first) == field.second) {
      matching_keys.push_back(key);
    }
  }

  delete it;
  return matching_keys;
}


//测试多字段-橙
TEST(TestSchema, Basic) {
    DB *db;
    if(OpenDB("testdb", &db).ok() == false) {
      std::cerr << "open db failed" << std::endl;
      abort();
    }
    std::string key = "k_1";
    
    FieldArray fields = {
        {"name", "Customer#000000001"},
        {"address", "IVhzIApeRb"}, 
        {"phone", "25-989-741-2988"}
    };

    // 序列化并插入
    std::string value = SerializeValue(fields);
    db->Put(WriteOptions(), key, value);

    // 读取并反序列化
    std::string value_ret;
    db->Get(ReadOptions(), key, &value_ret);
    auto fields_ret = ParseValue(value_ret);
    
    //清理数据库
    delete db;
}
// 测试 FindKeysByField 函数
  /*
  1.在测试中创建多个键值对,验证 FindKeysByField 能否正确地查找并返回匹配的键。
  2.使用 FindKeysByField 查找字段值，检查它是否能够找到匹配的键。
  */
TEST(TestSchema, FindKeysByFieldTest) {
    DB *db;
    if (OpenDB("testdb", &db).ok() == false) {
        std::cerr << "open db failed" << std::endl;
        abort();
    }

    // 插入多个键值对
    std::string key1 = "k_1";
    FieldArray fields1 = {
        {"name", "Customer#000000001"},
        {"address", "IVhzIApeRb"}, 
        {"phone", "25-989-741-2988"}
    };
    std::string value1 = SerializeValue(fields1);
    db->Put(WriteOptions(), key1, value1);

    std::string key2 = "k_2";
    FieldArray fields2 = {
        {"name", "Customer#000000002"},
        {"address", "IVhzIApeRb"},
        {"phone", "25-123-456-7890"}
    };
    std::string value2 = SerializeValue(fields2);
    db->Put(WriteOptions(), key2, value2);

    std::string key3 = "k_3";
    FieldArray fields3 = {
        {"name", "Customer#000000003"},
        {"address", "TXkjZEdIrZ"},
        {"phone", "25-555-888-1234"}
    };
    std::string value3 = SerializeValue(fields3);
    db->Put(WriteOptions(), key3, value3);

    // 测试 FindKeysByField
    Field search_field = {"address", "IVhzIApeRb"};
    std::vector<std::string> matching_keys = FindKeysByField(db, search_field);

    EXPECT_EQ(matching_keys.size(), 2); 
    EXPECT_TRUE(std::find(matching_keys.begin(), matching_keys.end(), key1) != matching_keys.end());
    EXPECT_TRUE(std::find(matching_keys.begin(), matching_keys.end(), key2) != matching_keys.end());

    // 清理数据库
    delete db;
}
int main(int argc, char** argv) {
  // All tests currently run with the same read-only file limits.
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
