benchmark silo added
[c11concurrency-benchmarks.git] / silo / benchmarks / bdb_wrapper.cc
1 #include <limits>
2
3 #include "bdb_wrapper.h"
4 #include "../macros.h"
5
6 using namespace std;
7
8 bdb_wrapper::bdb_wrapper(const string &envdir, const string &dbfile)
9   : env(0)
10 {
11   env = new DbEnv(0);
12   ALWAYS_ASSERT(env->log_set_config(DB_LOG_IN_MEMORY, 1) == 0);
13   ALWAYS_ASSERT(env->set_lg_max(numeric_limits<uint32_t>::max()) == 0);
14   ALWAYS_ASSERT(env->set_lg_regionmax(numeric_limits<uint32_t>::max()) == 0);
15   //ALWAYS_ASSERT(env->set_lg_bsize(numeric_limits<uint32_t>::max()) == 0);
16   ALWAYS_ASSERT(env->set_flags(DB_TXN_NOSYNC, 1) == 0);
17   ALWAYS_ASSERT(env->set_cachesize(4, 0, 1) == 0);
18   ALWAYS_ASSERT(env->open(envdir.c_str(), DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_THREAD | DB_CREATE, 0) == 0);
19 }
20
21 bdb_wrapper::~bdb_wrapper()
22 {
23   delete env;
24 }
25
26 void *
27 bdb_wrapper::new_txn(
28     uint64_t txn_flags,
29     str_arena &arena,
30     void *buf, TxnProfileHint hint)
31 {
32   DbTxn *txn = NULL;
33   ALWAYS_ASSERT(env->txn_begin(NULL, &txn, 0) == 0);
34   ALWAYS_ASSERT(txn != NULL);
35   return (void *) txn;
36 }
37
38 bool
39 bdb_wrapper::commit_txn(void *p)
40 {
41   return ((DbTxn *) p)->commit(0) == 0;
42 }
43
44 void
45 bdb_wrapper::abort_txn(void *p)
46 {
47   ALWAYS_ASSERT(((DbTxn *) p)->abort() == 0);
48 }
49
50 abstract_ordered_index *
51 bdb_wrapper::open_index(const string &name, size_t value_size_hint, bool mostly_append)
52 {
53   Db *db = new Db(env, 0);
54   ALWAYS_ASSERT(db->set_flags(DB_TXN_NOT_DURABLE) == 0);
55   DbTxn *txn = NULL;
56   ALWAYS_ASSERT(env->txn_begin(NULL, &txn, 0) == 0);
57   ALWAYS_ASSERT(db->open(txn, name.c_str(), NULL, DB_BTREE, DB_CREATE, 0) == 0);
58   ALWAYS_ASSERT(txn->commit(0) == 0);
59   return new bdb_ordered_index(db);
60 }
61
62 void
63 bdb_wrapper::close_index(abstract_ordered_index *idx)
64 {
65   bdb_ordered_index *bidx = static_cast<bdb_ordered_index *>(idx);
66   delete bidx;
67 }
68
69 bdb_ordered_index::~bdb_ordered_index()
70 {
71   delete db;
72 }
73
74 bool
75 bdb_ordered_index::get(
76     void *txn,
77     const string &key,
78     string &value,
79     size_t max_bytes_read)
80 {
81   Dbt kdbt((void *) key.data(), key.size());
82   Dbt vdbt;
83   //vdbt.set_flags(DB_DBT_MALLOC);
84   int retno = db->get((DbTxn *) txn, &kdbt, &vdbt, 0);
85   ALWAYS_ASSERT(retno == 0 || retno == DB_NOTFOUND);
86   // XXX(stephentu): do a better job implementing this
87   value.assign((char *) vdbt.get_data(), min(static_cast<size_t>(vdbt.get_size()), max_bytes_read));
88   return retno == 0;
89 }
90
91 const char *
92 bdb_ordered_index::put(
93     void *txn,
94     const string &key,
95     const string &value)
96 {
97   Dbt kdbt((void *) key.data(), key.size());
98   Dbt vdbt((void *) value.data(), value.size());
99   ALWAYS_ASSERT(db->put((DbTxn *) txn, &kdbt, &vdbt, 0) == 0);
100   return 0;
101 }