10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.
 
 

86 lines
2.6 KiB

#include "leveldb/db.h"
#include "leveldb/options.h"
#include "util/random.h"
#include <bits/stdc++.h>
#include <string>
using namespace std;
using namespace leveldb;
int main() {
DB* db = nullptr;
Options op;
op.create_if_missing = true;
DestroyDB("testdb_kv", Options());
Status status = DB::Open(op, "testdb_kv", &db);
// assert(status.ok());
// string key = "leveldb",value = to_string(0);
// string res;
// for(int i = 0; i < 5; i++) {
// db->Put(WriteOptions(),key,to_string(i));
// }
// db->Put(WriteOptions(),key+key,to_string(0));
// db->Put(WriteOptions(),key+key,to_string(1));
// sleep(1);
// auto snapshot = db->GetSnapshot();
// auto readopts = ReadOptions();
// readopts.snapshot = snapshot;
// db->Put(WriteOptions(),key,to_string(10));
// db->CompactRange(nullptr,nullptr);
// db->Get(readopts,key,&res);
// cout<<"with snapshot:"<<res<<endl;
// res.clear();
// db->Get(ReadOptions(),key,&res);
// cout<<"without snapshot:"<<res<<endl;
// string value = "leveldb",key = "abc";
// db->Delete(WriteOptions(),key);
// db->Put(WriteOptions(),key,value);
// const Snapshot *snapshot = db->GetSnapshot();
// ReadOptions read_op = ReadOptions();
// read_op.snapshot = snapshot;
// string result;
// db->Get(read_op,key,&result);
// cout<<result<<endl;
// db->Delete(WriteOptions(),key);
// result.clear();
// db->Get(read_op,key,&result);
// cout<<"with snapshot:"<<result<<endl;
// result.clear();
// db->Get(ReadOptions(),key,&result);
// cout<<"without snapshot:"<<result<<endl;
// db->ReleaseSnapshot(snapshot);
string key,value = "leveldb";
key.resize(1000,'a');
// value.resize(10,'b');
value += "index";
Random ran(0);
for(int i = 0; i < 100000; i++) {
for(int j = 0; j < 20; j++) {
int rand = ran.Uniform(200 * 10);
string tk = key + std::to_string(rand);
string tv = value + std::to_string(rand) + "_" + std::to_string(i);
db->Put(WriteOptions(),tk,tv);
}
if(i && i%1000 ==0) {
std::cout << "iteration:" << i << std::endl;
}
}
std::string res;
for(int i = 0; i < 10; i++) {
db->Get(ReadOptions(), key + std::to_string(i), &res);
std::cout << i << " " << res << std::endl;
}
delete db;
// DB::Open(op,"testdb",&db);
// key = "abc", value = "";
// db->Get(ReadOptions(),key,&value);
// cout<<value;
// // DestroyDB("testdb",op);
// delete db;
return 0;
}