-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelDBStore.cc
65 lines (55 loc) · 1.46 KB
/
LevelDBStore.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* LevelDBStore.cc
*
* Created on: Oct 15, 2015
* Author: Prasanna Ponnada
*/
#include "LevelDBStore.h"
bool
LevelDBStore::init(const std::string coordinatorLoc, bool hasDispatchedThread) {
/* there is no co-ordinator in Level DB, nothing to init */
return true;
}
bool
LevelDBStore::open(const std::string dbName, void** handle) {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, dbName, &db);
if (status.ok() == false)
{
std::cerr << "Unable to open/create database " << dbName << std::endl;
std::cerr << status.ToString() << std::endl;
return false;
}
*handle = db;
return true;
}
bool
LevelDBStore::put(void* handle, const std::string& key,
const std::string& value) {
leveldb::DB* db = reinterpret_cast<leveldb::DB*>(handle);
leveldb::Status status = db->Put(leveldb::WriteOptions(), key, value);
if(status.ok())
return true;
else
return false;
}
bool
LevelDBStore::get(void* handle, const std::string&key, std::string& value) {
leveldb::DB* db = reinterpret_cast<leveldb::DB*>(handle);
leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value);
if(status.ok())
return true;
else
return false;
}
bool
LevelDBStore::remove(void* handle, const std::string& key) {
leveldb::DB* db = reinterpret_cast<leveldb::DB*>(handle);
leveldb::Status status = db->Delete(leveldb::WriteOptions(), key);
if(status.ok())
return true;
else
return false;
}