4 * Sleepycat (Berkeley) DB driver for Citadel/UX
19 #include "citserver.h"
21 #include "sysdep_decls.h"
25 * This array holds one DB handle for each Citadel database.
31 DBC *MYCURSOR; /* FIXME !! */
34 * Reclaim unused space in the databases. We need to do each one of
35 * these discretely, rather than in a loop.
37 void defrag_databases(void)
39 /* FIXME ... do we even need this? If not, we'll just keep it as
40 * a stub function to keep the API consistent.
46 * Open the various databases we'll be using. Any database which
47 * does not exist should be created. Note that we don't need an S_DATABASE
48 * critical section here, because there aren't any active threads manipulating
49 * the database yet -- and besides, it causes problems on BSDI.
51 void open_databases(void)
58 * Silently try to create the database subdirectory. If it's
59 * already there, no problem.
61 system("exec mkdir data 2>/dev/null");
63 lprintf(9, "Setting up DB environment\n");
64 ret = db_env_create(&dbenv, 0);
66 lprintf(1, "db_env_create: %s\n", db_strerror(ret));
69 dbenv->set_errfile(dbenv, stderr); /* FIXME */
70 dbenv->set_errpfx(dbenv, "citserver");
73 * We want to specify the shared memory buffer pool cachesize,
74 * but everything else is the default.
76 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
78 lprintf(1, "set_cachesize: %s\n", db_strerror(ret));
79 dbenv->close(dbenv, 0);
84 * We have multiple processes reading/writing these files, so
85 * we need concurrency control and a shared buffer pool, but
86 * not logging or transactions.
88 /* (void)dbenv->set_data_dir(dbenv, "/database/files"); */
89 ret = dbenv->open(dbenv, "./data",
90 DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_THREAD, 0);
92 lprintf(1, "dbenv->open: %s\n", db_strerror(ret));
93 dbenv->close(dbenv, 0);
97 lprintf(7, "Starting up DB\n");
99 for (i = 0; i < MAXCDB; ++i) {
101 /* Create a database handle */
102 ret = db_create(&dbp[i], dbenv, 0);
104 lprintf(1, "db_create: %s\n", db_strerror(ret));
109 /* Arbitrary names for our tables -- we reference them by
110 * number, so we don't have string names for them.
112 sprintf(dbfilename, "cdb.%02x", i);
114 ret = dbp[i]->open(dbp[i],
121 lprintf(1, "db_open[%d]: %s\n", i, db_strerror(ret));
131 * Close all of the gdbm database files we've opened. This can be done
132 * in a loop, since it's just a bunch of closes.
134 void close_databases(void)
139 begin_critical_section(S_DATABASE);
140 for (a = 0; a < MAXCDB; ++a) {
141 lprintf(7, "Closing database %d\n", a);
142 ret = dbp[a]->close(dbp[a], 0);
144 lprintf(1, "db_close: %s\n", db_strerror(ret));
151 /* Close the handle. */
152 ret = dbenv->close(dbenv, 0);
154 lprintf(1, "DBENV->close: %s\n", db_strerror(ret));
158 end_critical_section(S_DATABASE);
164 * Store a piece of data. Returns 0 if the operation was successful. If a
165 * key already exists it should be overwritten.
167 int cdb_store(int cdb,
168 void *ckey, int ckeylen,
169 void *cdata, int cdatalen)
175 memset(&dkey, 0, sizeof(DBT));
176 memset(&ddata, 0, sizeof(DBT));
179 ddata.size = cdatalen;
182 begin_critical_section(S_DATABASE);
183 ret = dbp[cdb]->put(dbp[cdb], /* db */
184 NULL, /* transaction ID (hmm...) */
188 end_critical_section(S_DATABASE);
189 lprintf(9, "put ( to file %d) returned %3d (%d bytes)\n",
190 cdb, ret, ddata.size);
192 lprintf(1, "cdb_store: %s\n", db_strerror(ret));
200 * Delete a piece of data. Returns 0 if the operation was successful.
202 int cdb_delete(int cdb, void *key, int keylen)
211 begin_critical_section(S_DATABASE);
212 ret = dbp[cdb]->del(dbp[cdb], NULL, &dkey, 0);
213 end_critical_section(S_DATABASE);
222 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
223 * a struct cdbdata which it is the caller's responsibility to free later on
224 * using the cdb_free() routine.
226 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
229 struct cdbdata *tempcdb;
233 memset(&dkey, 0, sizeof(DBT));
234 memset(&dret, 0, sizeof(DBT));
237 dret.flags = DB_DBT_MALLOC;
239 begin_critical_section(S_DATABASE);
240 ret = dbp[cdb]->get(dbp[cdb], NULL, &dkey, &dret, 0);
241 end_critical_section(S_DATABASE);
242 lprintf(9, "get (from file %d) returned %3d (%d bytes)\n",
243 cdb, ret, dret.size);
244 if ((ret != 0) && (ret != DB_NOTFOUND)) {
245 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
247 if (ret != 0) return NULL;
248 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
249 if (tempcdb == NULL) {
250 lprintf(2, "Cannot allocate memory!\n");
252 tempcdb->len = dret.size;
253 tempcdb->ptr = dret.data;
259 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
260 * more complex stuff with other database managers in the future).
262 void cdb_free(struct cdbdata *cdb)
270 * Prepare for a sequential search of an entire database. (In the DB model,
271 * use per-session key. There is guaranteed to be no more than one traversal in
272 * progress per session at any given time.)
274 void cdb_rewind(int cdb)
278 begin_critical_section(S_DATABASE);
279 ret = dbp[cdb]->cursor(dbp[cdb], NULL, &MYCURSOR, 0);
281 lprintf(1, "db_cursor: %s\n", db_strerror(ret));
283 end_critical_section(S_DATABASE);
288 * Fetch the next item in a sequential search. Returns a pointer to a
289 * cdbdata structure, or NULL if we've hit the end.
291 struct cdbdata *cdb_next_item(int cdb)
294 struct cdbdata *cdbret;
297 /* Initialize the key/data pair so the flags aren't set. */
298 memset(&key, 0, sizeof(key));
299 memset(&data, 0, sizeof(data));
300 data.flags = DB_DBT_MALLOC;
302 begin_critical_section(S_DATABASE);
303 ret = MYCURSOR->c_get(MYCURSOR,
304 &key, &data, DB_NEXT);
305 end_critical_section(S_DATABASE);
307 if (ret) return NULL; /* presumably, end of file */
309 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
310 cdbret->len = data.size;
311 cdbret->ptr = data.data;