
#include "gtest/gtest.h"

#include "leveldb/env.h"
#include "leveldb/db.h"
#include "util/coding.h"

#include <iostream> 

using namespace leveldb;
constexpr int value_size = 2048;
constexpr int data_size = 128 << 20;

// // 根据字段值查找所有包含该字段的 key
// std::vector<std::string> FindKeysByField(leveldb::DB* db, Field &field) {
//   Iterator* iter = db->NewIterator(ReadOptions());
//   std::vector<std::string> ret_keys;
//   int64_t bytes = 0;
//   for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
//     auto fields_ret = ParseValue(iter->value().data());
//     for (Field each_field : fields_ret)
//     {
//       std::cout << each_field.first << " " << each_field.second << std::endl;
//       if (field.first.compare(each_field.first) == 0) {
//         if (field.second.compare(each_field.second)==0)
//         {
//           ret_keys.push_back(iter->key().data());
//         }
//         else
//           break;
//       }
//     }
//   }
//   delete iter;
//   return ret_keys;
// }


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

TEST(TestField, Read) {
    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"}
    };

    // 序列化并插入，ckx
    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);
    std::cout << "第一个字段名："<< fields_ret[0].first << "第一个字段值" << fields_ret[0].second<< std::endl;
    delete db;
}

TEST(TestField, Find) {
    DB *db;
    if(OpenDB("testdb", &db).ok() == false) {
        std::cerr << "open db failed" << std::endl;
        abort();
    }
    std::vector<std::string> keys = {"s_1", "s_2", "s_3", "s_4"};
    
    // 构造一组字段数组
    std::vector<FieldArray> FieldArrays = {
        {
            {"name", "Sarah"},{"sex", "f"},{"age", "20"}
        },
        {
            {"name", "Mike"},{"sex", "m"},{"age", "19"},{"hobby", "badminton"}
        },
        {
            {"name", "Amy"},{"sex", "f"},{"age", "21"},{"talent", "sing"}
        },
        {
            {"name", "John"}, {"sex", "m"},{"age", "20"}
        }
    };
    

    // 序列化并插入
    for (int i=0; i<FieldArrays.size(); i++)
    {
        std::string key = keys[i];
        FieldArray fields = FieldArrays[i];
        std::string value = SerializeValue(fields);
        db->Put(WriteOptions(), key, value);
    }
  
    // 构建目标字段
    Field field = {"sex", "f"};
    std::vector<std::string> key_ret;
    // 查询得到对应的key
    key_ret = db->FindKeysByField(db, field);
    for (int i = 0; i < key_ret.size(); i++) {
      std::cout << "找到的键：" << key_ret[i] << std::endl;
    }
    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();
}
