From ecf9e2e104a854a4fff6b502e2902de91355536a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 18 Aug 2006 18:04:26 +0000 Subject: author db --- math/metadb.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 4 deletions(-) (limited to 'math/metadb.c') diff --git a/math/metadb.c b/math/metadb.c index 7e50959..ce910e3 100644 --- a/math/metadb.c +++ b/math/metadb.c @@ -36,9 +36,11 @@ #define P(f1,f2) printf( #f1 " = '%s'\t" #f2 " = '%s'\n", (f1), (f2)) -DB *idx, *meta, *srch; +DB *idx, *meta, *srch, *auth; + +static char *encode_meta(struct meta *, size_t *); +static struct meta *decode_meta(struct meta *, char *); -struct meta *decode_meta(struct meta *, char *); int cmptitle_i(const void *, const void *); int cmptitle_d(const void *, const void *); int cmpdate_i(const void *, const void *); @@ -46,13 +48,17 @@ int cmpdate_d(const void *, const void *); int cmpauthor_i(const void *, const void *); int cmpauthor_d(const void *, const void *); +static char *encode_author(struct author *, size_t *); +static struct author *decode_author(struct author *, char *); + int db_open(int flags) { idx = dbopen(INDEXDB, flags, DB_MODE, DB_HASH, NULL); meta = dbopen(METADB, flags, DB_MODE, DB_HASH, NULL); srch = dbopen(SEARCHDB, flags, DB_MODE, DB_HASH, NULL); - if (idx == NULL || meta == NULL || srch == NULL) { + auth = dbopen(AUTHORDB, flags, DB_MODE, DB_HASH, NULL); + if (idx == NULL || meta == NULL || srch == NULL || auth == NULL) { db_close(); return -1; } @@ -68,6 +74,8 @@ db_close(void) (meta->close)(meta); if (srch != NULL) (srch->close)(srch); + if (auth != NULL) + (auth->close)(auth); } static int @@ -201,7 +209,7 @@ encode_meta(struct meta *mp, size_t *siz) (l) += strlen(d) + 1; \ } while (0) -struct meta * +static struct meta * decode_meta(struct meta *mp, char *buf) { size_t blen; @@ -578,4 +586,85 @@ db_wipe(void) while ((srch->seq)(srch, &k, &d, 0) != 0) (srch->del)(srch, &k, 0); + + while ((auth->seq)(auth, &k, &d, 0) != 0) + (auth->del)(auth, &k, 0); +} + +static char * +encode_author(struct author *ap, size_t *siz) +{ + char *buf; + size_t len, blen; + + *siz = LENGTH(ap->name); + *siz += LENGTH(ap->mail); + + buf = malloc(*siz); + assert(buf); + + blen = 0; + PUTSTR(buf, ap->name, blen, len); + PUTSTR(buf, ap->mail, blen, len); + + return buf; +} + +static struct author * +decode_author(struct author *ap, char *buf) +{ + size_t blen; + + blen = 0; + GETSTR(ap->name, buf, blen); + GETSTR(ap->mail, buf, blen); + + return ap; +} + +int +db_putauthor(struct author *ap, char *id) +{ + DBT k, d; + char *buf; + size_t siz; + int ret; + + memset(&k, 0, sizeof(DBT)); + memset(&d, 0, sizeof(DBT)); + + buf = encode_author(ap, &siz); + + k.data = id; + k.size = strlen(id); + d.data = buf; + d.size = siz; + + ret = (auth->put)(auth, &k, &d, 0); + free(buf); + + return ret; +} + +struct author * +db_getauthor(struct author *ap, char *id) +{ + DBT k, d; + + memset(&k, 0, sizeof(DBT)); + memset(&d, 0, sizeof(DBT)); + + k.data = id; + k.size = strlen(id); + + if ((auth->get)(auth, &k, &d, 0) == 0) { + if (ap == NULL) { + ap = malloc(sizeof(struct author)); + assert(ap); + } + + return decode_author(ap, d.data); + } + + return NULL; } -- cgit v1.2.3