#include "db/NewDB.h"
#include "util/coding.h"
#include "db/write_batch_internal.h"
#include "db/version_set.h"
#include "db/db_impl.h"
#include <string>
#include <sstream>
#include <unordered_map>
#include <map>
#include <iostream>
#include <mutex>
#include <thread>
#include <ctime>
#include <condition_variable>
#include <unordered_set>

namespace leveldb{
NewDB::~NewDB() {

}

std::string NewDB::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;

    //   std::string s;
    //   for(auto& field : fields){
    //     PutLengthPrefixedSlice(&s, Slice(field.first));
    //     PutLengthPrefixedSlice(&s, Slice(field.second));
    //   }
    //   return s;
}

FieldArray NewDB::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;

    //   Slice input(value_str);
    //   Slice v1,v2;
    //   FieldArray fields;
    //   while(v1!=Slice()){
    //     GetLengthPrefixedSlice(&input, &v1);
    //     GetLengthPrefixedSlice(&input, &v2);
    //     fields.push_back(std::make_pair(v1.ToString(), v2.ToString()));
    //   }
    //   return fields; 
}

Status NewDB::Open(const Options& options, const std::string& name, NewDB** dbptr) {

}


Status NewDB::Put_fields(const WriteOptions& options, const Slice& key, const FieldArray& fields){

}

Status NewDB::Get_fields(const ReadOptions& options, const Slice& key, FieldArray* fields){

}

std::vector<std::string> NewDB::FindKeysByField(Field &field){  

}

bool NewDB::Delete(const WriteOptions& options, const Slice& key){  

}



bool NewDB::CreateIndexOnField(const std::string& field_name) {

}

std::vector<std::string> NewDB::QueryByIndex(Field &field){   

}

bool NewDB::DeleteIndex(const std::string& field_name){    

}

}





