about summary refs log tree commit diff
path: root/src/db.cc
blob: 4178f6ebe29fe3af34bb79c5f7e0637a290acd03 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "db.hh"
#include "util.hh"

#include <memory>

#include <db_cxx.h>


/* Wrapper classes that ensures that the database is closed upon
   object destruction. */
class Db2 : public Db 
{
public:
    Db2(DbEnv *env, u_int32_t flags) : Db(env, flags) { }
    ~Db2() { close(0); }
};


class DbcClose 
{
    Dbc * cursor;
public:
    DbcClose(Dbc * c) : cursor(c) { }
    ~DbcClose() { cursor->close(); }
};


static auto_ptr<Db2> openDB(const string & filename, const string & dbname,
    bool readonly)
{
    auto_ptr<Db2> db(new Db2(0, 0));

    db->open(filename.c_str(), dbname.c_str(),
        DB_HASH, readonly ? DB_RDONLY : DB_CREATE, 0666);

    return db;
}


static void rethrow(DbException & e)
{
    throw Error(e.what());
}


void createDB(const string & filename, const string & dbname)
{
    try {
        openDB(filename, dbname, false);
    } catch (DbException e) { rethrow(e); }
}


bool queryDB(const string & filename, const string & dbname,
    const string & key, string & data)
{
    try {

        int err;
        auto_ptr<Db2> db = openDB(filename, dbname, true);

        Dbt kt((void *) key.c_str(), key.length());
        Dbt dt;

        err = db->get(0, &kt, &dt, 0);
        if (err) return false;

        data = string((char *) dt.get_data(), dt.get_size());
    
    } catch (DbException e) { rethrow(e); }

    return true;
}


void setDB(const string & filename, const string & dbname,
    const string & key, const string & data)
{
    try {
        auto_ptr<Db2> db = openDB(filename, dbname, false);
        Dbt kt((void *) key.c_str(), key.length());
        Dbt dt((void *) data.c_str(), data.length());
        db->put(0, &kt, &dt, 0);
    } catch (DbException e) { rethrow(e); }
}


void delDB(const string & filename, const string & dbname,
    const string & key)
{
    try {
        auto_ptr<Db2> db = openDB(filename, dbname, false);
        Dbt kt((void *) key.c_str(), key.length());
        db->del(0, &kt, 0);
    } catch (DbException e) { rethrow(e); }
}


void enumDB(const string & filename, const string & dbname,
    DBPairs & contents)
{
    try {

        auto_ptr<Db2> db = openDB(filename, dbname, false);

        Dbc * cursor;
        db->cursor(0, &cursor, 0);
        DbcClose cursorCloser(cursor);

        Dbt kt, dt;
        while (cursor->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND) {
            string key((char *) kt.get_data(), kt.get_size());
            string data((char *) dt.get_data(), dt.get_size());
            contents.push_back(DBPair(key, data));
        }

    } catch (DbException e) { rethrow(e); }
}