#include "gtest/gtest.h"
|
|
#include "leveldb/env.h"
|
|
#include "leveldb/db.h"
|
|
using namespace leveldb;
|
|
using Field = std::pair<std::string, std::string>; // field_name:field_value
|
|
using FieldArray = std::vector<std::pair<std::string, std::string>>;
|
|
|
|
Status OpenDB(std::string dbName, DB **db) {
|
|
Options options;
|
|
options.create_if_missing = true;
|
|
return DB::Open(options, dbName, db);
|
|
}
|
|
|
|
TEST(TestLab1, Basic) {
|
|
DestroyDB("testdb",Options());
|
|
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"}
|
|
};
|
|
|
|
// 序列化并插入
|
|
db->PutFields(WriteOptions(), key, fields);
|
|
|
|
// 读取并反序列化
|
|
FieldArray fields_ret;
|
|
db->GetFields(ReadOptions(), key, &fields_ret);
|
|
|
|
Field field = {"name", "Customer#000000001"};
|
|
std::vector<std::string> resKeys = db->FindKeysByField(field);
|
|
}
|
|
|
|
|
|
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();
|
|
}
|