Re: cygwin strangeness -
12-30-2005
, 07:31 PM
I definitely am not using DB_SYSTEM_MEM. In fact, I tried to to see if
that would change anything and it just resulted in a hard error upon
trying to open the environment - i.e. it's not supported.
Here's my functions and backing headers that I use:
(db_coll is an abstract I use to pre-fill db attributes before I
create/open)
/* util_db.h: */
#ifndef __UTIL_DB_H
#define __UTIL_DB_H
#include <db.h>
#include <stdio.h>
#define db_perror(x,y,z) { fprintf(stderr, "%s: %s\n", y,
db_strerror(x)); return z; }
enum db_type {
DB_PRIMARY = 0x1 << 0,
DB_SECONDARY = 0x1 << 1,
DB_PRIMARY_DUP = 0x1 << 2
};
struct db_coll {
char dbe_n[256]; /* environment directory */
char db_n[256]; /* db name */
DB_ENV *dbe;
DB *db;
uint32_t flags; /* db_type flags */
struct db_coll *db_c_p; /* parent db_coll */
int32_t (*cb_dup_compare)(DB *, const DBT *, const DBT *);
int32_t (*cb_assoc)(DB *, const DBT *, const DBT *, DBT *);
};
int32_t db_open(struct db_coll *);
int32_t db_close(DB *);
#endif /* __UTIL_DB_H */
/* util_db.c: */
#include <stdlib.h>
#include <unistd.h>
#include <db.h>
#include "util_db.h"
static const size_t cache_max = 990 * 1024 * 1024;
static const size_t pagesize = 4 * 1024;
static const uint32_t fl_db = DB_CREATE;
static const uint32_t fl_dbe = DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL;
int32_t db_open(struct db_coll *db_c)
{
uint32_t fl_db;
uint32_t fl_dbe;
int32_t ret;
if ((db_c->flags & DB_PRIMARY) && db_c->dbe == NULL) {
if ((ret = db_env_create(&db_c->dbe, 0)) != 0)
db_perror(ret, "db_env_create", -1);
db_c->dbe->set_cachesize(db_c->dbe, 0, cache_max, 1);
if ((ret = db_c->dbe->open(db_c->dbe,
db_c->dbe_n,
fl_dbe, 0)) != 0)
{
db_perror(ret, "db_env_open", -1);
}
}
if (db_c->db == NULL) {
if ((ret = db_create(&db_c->db,
db_c->flags & DB_PRIMARY
? db_c->dbe
: db_c->db_c_p->dbe,
0)) != 0)
{
db_perror(ret, "db_create", -1);
}
db_c->db->set_pagesize(db_c->db, pagesize);
if (db_c->flags & (DB_SECONDARY | DB_PRIMARY_DUP)) {
db_c->db->set_flags(db_c->db, DB_DUP |
DB_DUPSORT);
if (db_c->cb_dup_compare != NULL)
db_c->db->set_dup_compare(db_c->db,
db_c->cb_dup_compare);
}
if ((ret = db_c->db->open(db_c->db,
NULL,
db_c->db_n,
NULL,
DB_BTREE,
fl_db,
0)) != 0)
{
db_perror(ret, "db_open", -1);
}
if (db_c->flags & DB_SECONDARY)
if ((ret =
db_c->db->associate(db_c->db_c_p->db,
NULL,
db_c->db,
db_c->cb_assoc,
DB_IMMUTABLE_KEY)) != 0)
{
db_perror(ret, "db_associate", -1);
}
}
return 0;
}
int32_t db_close(DB *db)
{
if (db == NULL) return -1;
db->close(db, 0);
return 0;
}
p.s. google group's formatting logic sucks! |