Last active
August 29, 2015 14:03
-
-
Save yuikns/8c1fab105d2829f73cfb to your computer and use it in GitHub Desktop.
leveldb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get start from http://leveldb.googlecode.com/svn/trunk/doc/index.html | |
#include <iostream> | |
#include <algorithm> | |
#include <string> | |
#include "leveldb/db.h" | |
#include "leveldb/cache.h" | |
#include "leveldb/options.h" | |
int main() | |
{ | |
leveldb::DB* db; | |
leveldb::Options options; | |
options.create_if_missing = true; // 如果没有则创建 | |
options.block_cache = leveldb::NewLRUCache(1000 * 1048576L); // 1000MB cache | |
leveldb::Status status = leveldb::DB::Open(options, "index", &db); | |
db->Put(leveldb::WriteOptions(),std::string("key::0:"),std::string("")); // init | |
std::string key = "key::0:0"; | |
std::string value = "value789"; | |
//leveldb::WriteOptions write_options; | |
//write_options.sync = true; | |
//if((db->Put(write_options, key, value)).ok()) | |
if((db->Put(leveldb::WriteOptions(), key, value)).ok()) | |
{ | |
std::cout << "put ok" << std::endl; | |
} | |
std::string value1; | |
if(!(db->Get(leveldb::ReadOptions(), key, &value1)).ok()) | |
{ | |
std::cerr << "read failed " << std::endl; | |
} | |
std::cout << "[R] k0:" << key << " v0:" << value1 << std::endl; | |
std::string key1 = "key::0:1"; | |
value1.assign("value of value1"); | |
if(!(db->Put(leveldb::WriteOptions(), key1, value1)).ok()){ | |
std::cerr << "sth wrong ?? " << __FILE__<< ":" << std::to_string(__LINE__) << std::endl; | |
return -1; | |
} | |
leveldb::ReadOptions snap_read_opt; | |
snap_read_opt.snapshot = db->GetSnapshot(); | |
leveldb::Iterator* it = db->NewIterator(snap_read_opt); | |
//leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); | |
// it all | |
std::cout << "iterator :: all " << std::endl; | |
for (it->SeekToFirst(); it->Valid(); it->Next()) | |
{ | |
std::cout << it->key().ToString() << ": " << it->value().ToString() << std::endl; | |
} | |
// it sth | |
std::string key_base("key::0:"); | |
leveldb::Slice key_vect = key_base; | |
std::cout << "iterator :: prefix of " << key_vect.ToString() << std::endl; | |
for (it->Seek(key_vect),it->Next(); | |
//it->Valid() && .substr(0, key_vect.size()-2) == key_vect.ToString().substr(0, key_vect.size()-2); | |
it->Valid() && std::mismatch(key_base.begin(),key_base.end(),it->key().ToString().begin()).first == key_base.end(); | |
it->Next()) { | |
std::cout << it->key().ToString() << "\t" << it->value().ToString() << std::endl; | |
} | |
// release snapshot | |
delete it; | |
db->ReleaseSnapshot(snap_read_opt.snapshot); | |
if(!(db->Delete(leveldb::WriteOptions(), key1)).ok()){ | |
std::cerr << "sth wrong ?? " << __FILE__<< ":" << std::to_string(__LINE__) << std::endl; | |
return -1; | |
} | |
//batch | |
//leveldb::WriteBatch batch; | |
//batch.Delete(key1); | |
//batch.Put(key2, value); | |
//db->Write(leveldb::WriteOptions(), &batch); | |
delete db; | |
delete options.block_cache; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment