#include "leveldb/db.h" #include "leveldb/options.h" #include "util/random.h" #include #include 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:"<Get(ReadOptions(),key,&res); // cout<<"without snapshot:"<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<Delete(WriteOptions(),key); // result.clear(); // db->Get(read_op,key,&result); // cout<<"with snapshot:"<Get(ReadOptions(),key,&result); // cout<<"without snapshot:"<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<