2003-05-26 13:45:00 +00:00
|
|
|
#include "db.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
2003-05-26 15:09:10 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
/* Wrapper class to ensure proper destruction. */
|
|
|
|
class DestroyDb
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db;
|
2003-05-26 13:45:00 +00:00
|
|
|
public:
|
2003-07-31 13:47:13 +00:00
|
|
|
DestroyDb(Db * _db) : db(_db) { }
|
|
|
|
~DestroyDb() { db->close(0); delete db; }
|
2003-05-26 13:45:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
class DestroyDbc
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
2003-07-31 13:47:13 +00:00
|
|
|
Dbc * dbc;
|
2003-05-26 13:45:00 +00:00
|
|
|
public:
|
2003-07-31 13:47:13 +00:00
|
|
|
DestroyDbc(Dbc * _dbc) : dbc(_dbc) { }
|
|
|
|
~DestroyDbc() { dbc->close(); /* close() frees dbc */ }
|
2003-05-26 13:45:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
static void rethrow(DbException & e)
|
|
|
|
{
|
|
|
|
throw Error(e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Transaction::Transaction()
|
|
|
|
: txn(0)
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
2003-07-31 13:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Transaction::Transaction(Database & db)
|
|
|
|
{
|
|
|
|
db.requireEnv();
|
|
|
|
db.env->txn_begin(0, &txn, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Transaction::~Transaction()
|
|
|
|
{
|
|
|
|
if (txn) {
|
|
|
|
txn->abort();
|
|
|
|
txn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Transaction::commit()
|
|
|
|
{
|
|
|
|
if (!txn) throw Error("commit called on null transaction");
|
|
|
|
txn->commit(0);
|
|
|
|
txn = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Database::requireEnv()
|
|
|
|
{
|
|
|
|
if (!env) throw Error("database environment not open");
|
|
|
|
}
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
|
|
|
|
Db * Database::openDB(const Transaction & txn,
|
|
|
|
const string & table, bool create)
|
|
|
|
{
|
|
|
|
requireEnv();
|
|
|
|
|
|
|
|
Db * db = new Db(env, 0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// !!! fixme when switching to BDB 4.1: use txn.
|
|
|
|
db->open(table.c_str(), 0,
|
|
|
|
DB_HASH, create ? DB_CREATE : 0, 0666);
|
|
|
|
} catch (...) {
|
|
|
|
delete db;
|
|
|
|
throw;
|
|
|
|
}
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
Database::Database()
|
|
|
|
: env(0)
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
Database::~Database()
|
|
|
|
{
|
|
|
|
if (env) {
|
|
|
|
env->close(0);
|
|
|
|
delete env;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Database::open(const string & path)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (env) throw Error(format("environment already open"));
|
|
|
|
|
|
|
|
env = new DbEnv(0);
|
|
|
|
|
|
|
|
debug("foo" + path);
|
|
|
|
env->open(path.c_str(),
|
|
|
|
DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
|
|
|
|
DB_CREATE,
|
|
|
|
0666);
|
|
|
|
|
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Database::createTable(const string & table)
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
|
|
|
try {
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db = openDB(noTxn, table, true);
|
|
|
|
DestroyDb destroyDb(db);
|
2003-05-26 13:45:00 +00:00
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
bool Database::queryString(const Transaction & txn, const string & table,
|
2003-05-26 13:45:00 +00:00
|
|
|
const string & key, string & data)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db = openDB(txn, table, false);
|
|
|
|
DestroyDb destroyDb(db);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
|
|
|
Dbt dt;
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
int err = db->get(txn.txn, &kt, &dt, 0);
|
2003-05-26 13:45:00 +00:00
|
|
|
if (err) return false;
|
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
if (!dt.get_data())
|
|
|
|
data = "";
|
|
|
|
else
|
|
|
|
data = string((char *) dt.get_data(), dt.get_size());
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
bool Database::queryStrings(const Transaction & txn, const string & table,
|
2003-07-07 09:25:26 +00:00
|
|
|
const string & key, Strings & data)
|
|
|
|
{
|
|
|
|
string d;
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
if (!queryString(txn, table, key, d))
|
2003-07-07 09:25:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
string::iterator it = d.begin();
|
|
|
|
|
|
|
|
while (it != d.end()) {
|
|
|
|
|
|
|
|
if (it + 4 > d.end())
|
|
|
|
throw Error(format("short db entry: `%1%'") % d);
|
|
|
|
|
|
|
|
unsigned int len;
|
|
|
|
len = (unsigned char) *it++;
|
|
|
|
len |= ((unsigned char) *it++) << 8;
|
|
|
|
len |= ((unsigned char) *it++) << 16;
|
|
|
|
len |= ((unsigned char) *it++) << 24;
|
|
|
|
|
|
|
|
if (it + len > d.end())
|
|
|
|
throw Error(format("short db entry: `%1%'") % d);
|
|
|
|
|
|
|
|
string s;
|
|
|
|
while (len--) s += *it++;
|
|
|
|
|
|
|
|
data.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void Database::setString(const Transaction & txn, const string & table,
|
2003-05-26 13:45:00 +00:00
|
|
|
const string & key, const string & data)
|
|
|
|
{
|
|
|
|
try {
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db = openDB(txn, table, false);
|
|
|
|
DestroyDb destroyDb(db);
|
|
|
|
|
2003-05-26 13:45:00 +00:00
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
|
|
|
Dbt dt((void *) data.c_str(), data.length());
|
2003-07-31 13:47:13 +00:00
|
|
|
db->put(txn.txn, &kt, &dt, 0);
|
2003-05-26 13:45:00 +00:00
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void Database::setStrings(const Transaction & txn, const string & table,
|
2003-07-07 09:25:26 +00:00
|
|
|
const string & key, const Strings & data)
|
|
|
|
{
|
|
|
|
string d;
|
|
|
|
|
|
|
|
for (Strings::const_iterator it = data.begin();
|
|
|
|
it != data.end(); it++)
|
|
|
|
{
|
|
|
|
string s = *it;
|
|
|
|
unsigned int len = s.size();
|
|
|
|
|
|
|
|
d += len & 0xff;
|
|
|
|
d += (len >> 8) & 0xff;
|
|
|
|
d += (len >> 16) & 0xff;
|
|
|
|
d += (len >> 24) & 0xff;
|
|
|
|
|
|
|
|
d += s;
|
|
|
|
}
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
setString(txn, table, key, d);
|
2003-07-07 09:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void Database::delPair(const Transaction & txn, const string & table,
|
2003-05-26 13:45:00 +00:00
|
|
|
const string & key)
|
|
|
|
{
|
|
|
|
try {
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db = openDB(txn, table, false);
|
|
|
|
DestroyDb destroyDb(db);
|
2003-05-26 13:45:00 +00:00
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
2003-07-31 13:47:13 +00:00
|
|
|
db->del(txn.txn, &kt, 0);
|
2003-05-26 13:45:00 +00:00
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void Database::enumTable(const Transaction & txn, const string & table,
|
2003-07-17 12:27:55 +00:00
|
|
|
Strings & keys)
|
2003-05-26 13:45:00 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
Db * db = openDB(txn, table, false);
|
|
|
|
DestroyDb destroyDb(db);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
Dbc * dbc;
|
|
|
|
db->cursor(0, &dbc, 0);
|
|
|
|
DestroyDbc destroyDbc(dbc);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
Dbt kt, dt;
|
2003-07-31 13:47:13 +00:00
|
|
|
while (dbc->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND)
|
2003-07-17 12:27:55 +00:00
|
|
|
keys.push_back(
|
|
|
|
string((char *) kt.get_data(), kt.get_size()));
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
} catch (DbException e) { rethrow(e); }
|
|
|
|
}
|