* Copyright 2002 Project Purple
*/
+#include <assert.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <ctype.h>
#include "onak-conf.h"
#include "parsekey.h"
+/**
+ * dbenv - our database environment.
+ */
+static DB_ENV *dbenv = NULL;
+
/**
* dbconn - our connection to the key database.
*/
*/
static DB *worddb = NULL;
+/**
+ * txn - our current transaction id.
+ */
+static DB_TXN *txn = NULL;
+
/**
* makewordlist - Takes a string and splits it into a set of unique words.
* @wordlist: The current word list.
char buf[1024];
int ret = 0;
- strcpy(buf, config.db_dir);
- strcat(buf, "/keydb.db");
-
- ret = db_create(&dbconn, NULL, 0);
+ ret = db_env_create(&dbenv, 0);
+ if (ret != 0) {
+ fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
+ exit(1);
+ }
+
+ ret = dbenv->open(dbenv, config.db_dir,
+ DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
+ DB_INIT_TXN |
+ DB_RECOVER | DB_CREATE,
+ 0);
+ if (ret != 0) {
+ dbenv->err(dbenv, ret, "%s", config.db_dir);
+ exit(1);
+ }
+
+ ret = db_create(&dbconn, dbenv, 0);
if (ret != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit(1);
}
- ret = dbconn->open(dbconn, buf, NULL, DB_HASH,
+ ret = dbconn->open(dbconn, "keydb.db",
+ NULL,
+ DB_HASH,
DB_CREATE,
0664);
if (ret != 0) {
- dbconn->err(dbconn, ret, "%s", buf);
+ dbconn->err(dbconn, ret, "keydb.db");
exit(1);
}
- strcpy(buf, config.db_dir);
- strcat(buf, "/worddb");
-
- ret = db_create(&worddb, NULL, 0);
+ ret = db_create(&worddb, dbenv, 0);
if (ret != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit(1);
}
ret = worddb->set_flags(worddb, DB_DUP);
- ret = worddb->open(worddb, buf, NULL, DB_BTREE,
+ ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
DB_CREATE,
0664);
if (ret != 0) {
- worddb->err(worddb, ret, "%s", buf);
+ worddb->err(worddb, ret, "worddb");
exit(1);
}
worddb = NULL;
dbconn->close(dbconn, 0);
dbconn = NULL;
+ dbenv->close(dbenv, 0);
+ dbenv = NULL;
}
/**
*/
bool starttrans(void)
{
+ int ret;
+
+ assert(txn == NULL);
+
+ ret = txn_begin(dbenv,
+ NULL, /* No parent transaction */
+ &txn,
+ 0);
+ if (ret != 0) {
+ dbenv->err(dbenv, ret, "starttrans():");
+ exit(1);
+ }
+
return true;
}
*/
void endtrans(void)
{
+ int ret;
+
+ assert(txn != NULL);
+
+ ret = txn_commit(txn,
+ 0);
+ if (ret != 0) {
+ dbenv->err(dbenv, ret, "endtrans():");
+ exit(1);
+ }
+ txn = NULL;
+
return;
}
key.data = &keyid;
keyid &= 0xFFFFFFFF;
+ if (!intrans) {
+ starttrans();
+ }
+
ret = dbconn->get(dbconn,
- NULL, /* txn id */
+ txn,
&key,
&data,
0); /* flags*/
dbconn->err(dbconn, ret, "Problem retrieving key");
}
+ if (!intrans) {
+ endtrans();
+ }
+
return (numkeys);
}
searchtext = strdup(search);
wordlist = makewordlist(wordlist, searchtext);
+ starttrans();
ret = worddb->cursor(worddb,
- NULL, /* txn */
+ txn,
&cursor,
0); /* flags */
numkeys += fetch_key(keyid,
publickey,
- false);
+ true);
}
llfree(keylist, free);
keylist = NULL;
ret = cursor->c_close(cursor);
cursor = NULL;
+
+ endtrans();
return (numkeys);
}
keyid = get_keyid(publickey);
+ if (!intrans) {
+ starttrans();
+ }
+
/*
* Delete the key if we already have it.
*
data.data = storebuf.buffer;
ret = dbconn->put(dbconn,
- NULL, /* txn id */
+ txn,
&key,
&data,
0); /* flags*/
worddb_data[10] = (keyid >> 8) & 0xFF;
worddb_data[11] = keyid & 0xFF;
ret = worddb->put(worddb,
- 0,
+ txn,
&key,
&data,
0);
uids = NULL;
}
+ if (!intrans) {
+ endtrans();
+ }
+
return 0;
}
keyid &= 0xFFFFFFFF;
- fetch_key(keyid, &publickey, intrans);
+ if (!intrans) {
+ starttrans();
+ }
+
+ fetch_key(keyid, &publickey, true);
/*
* Walk through the uids removing the words from the worddb.
}
ret = worddb->cursor(worddb,
- NULL, /* txn */
+ txn,
&cursor,
0); /* flags */
&key,
&data,
DB_GET_BOTH);
+
if (ret == 0) {
ret = cursor->c_del(cursor, 0);
+ if (ret != 0) {
+ worddb->err(worddb, ret,
+ "Problem deleting word.");
+ }
}
if (ret != 0) {
key.size = sizeof(keyid);
dbconn->del(dbconn,
- NULL, /* txn id */
+ txn,
&key,
0); /* flags */
+ if (!intrans) {
+ endtrans();
+ }
+
return (ret == DB_NOTFOUND);
}