成员:余明阳 包亦晟
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.
 
 

83 lines
2.6 KiB

#include "leveldb/db.h"
#include "leveldb/my_leveldb.h"
#include <iostream>
using namespace std;
using namespace leveldb;
int main() {
Options op;
op.create_if_missing = true;
MyLevelDB db(op, "testMyDB");
//序列化测试
std::string res1;
FieldArray fields1 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
db.SerializeValue(fields1,res1);
std::cout << "序列化测试结果:" << std::endl << res1 << std::endl;
//反序列化测试
FieldArray fields2;
db.ParseValue(res1,fields2);
std::cout << "反序列化测试结果:" << std::endl ;
for (int i = 0; i < fields2.size(); i++) {
std::cout << fields2[i].first << ":" << fields2[i].second << std::endl;
}
//字段存储
std::cout << "字段存储和查找结果:" << std::endl;
std::string key2 = "k_1";
std::string key3 = "k_2";
std::string key4 = "k_3";
FieldArray field2 = {{"name", "Customer#000000001"},
{"address", "IVhzIApeRb"},
{"phone", "25-989-741-2988"}};
FieldArray field3 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
FieldArray field4 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
db.PutWithFields(WriteOptions(), key2, field2);
db.PutWithFields(WriteOptions(), key3, field3);
db.PutWithFields(WriteOptions(), key4, field4);
//字段查找
FieldArray value_ret;
std::vector<std::string> v;
db.FindKeysByField(ReadOptions(), field2[1], &v);
for (auto s : v) std::cout << s << "\n";
//创建索引
WriteOptions writeOptions;
ReadOptions readOptions;
Options options;
options.create_if_missing = true;
auto db1 = new MyLevelDB(options, "testdb2");
db1->CreateIndexOnField("address");
std::string key8 = "k_8";
std::string key9 = "k_9";
FieldArray fields8 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
FieldArray fields9 = {{"name", "Customer#000000001"},
{"address", "IVhzIApeRb"},
{"phone", "25-989-741-2988"}};
FieldArray fields10 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
FieldArray fields11 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
FieldArray fields12 = {
{"name", "Customer#000000001"}, {"address", "abc"}, {"phone", "def"}};
Field query = {"address", "abc"};
db1->PutWithFields(WriteOptions(), key8, fields8);
db1->PutWithFields(WriteOptions(), key9, fields9);
std::cout << "索引存储与查找:" << std::endl;
std::vector<std::string> keys;
db1->QueryByIndex(readOptions, query,keys);
for (int i = 0; i < keys.size();i++) {
std::cout << keys[i] << std::endl;
}
return 0;
}