-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_db.cpp
69 lines (57 loc) · 1.31 KB
/
rec_db.cpp
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
66
67
68
#include "rec_db.hpp"
#include "BDBIDX.h"
#include "bdb/bdb.hpp"
#include <set>
namespace rec_db {
db::db(BDB::Config const& conf)
{
store_ = new BDB::BehaviorDB(conf);
idx_ = new BDBIDX("rec_idx");
}
db::~db()
{
delete idx_;
delete store_;
}
bool
db::put(std::string const& key, std::string const& data)
{
BDB::AddrType addr;
if(-1 == (addr = store_->put(data)))
return false;
if(false == idx_->put_key(key, addr))
return false;
return true;
}
bool
db::get(std::string *output, std::string key)
{
assert(0 != output && "string is not proper inited");
std::set<BDB::AddrType> addrs;
if(1 != idx_->get_value(key, &addrs))
return false;
output->clear();
store_->get(output, -1, *addrs.begin());
return true;
}
bool
db::del(std::string const& key)
{
std::set<BDB::AddrType> addrs;
if(1 != idx_->get_value(key, &addrs))
return false;
if(0 != store_->del(*addrs.begin()))
return false;
return idx_->del_key(key, *addrs.begin());
}
bool
db::update(std::string const& key, std::string const& data)
{
std::set<BDB::AddrType> addrs;
if(1 != idx_->get_value(key, &addrs))
return false;
if(-1 == store_->update(data, *addrs.begin()))
return false;
return true;
}
} // namespace rec_db