int mboxkey_merge(const char *tmpfile, const char *tgtfile) { int r = 0; struct db *tmp = NULL, *tgt = NULL; struct mboxkey_merge_rock rock; /* xxx does this need to be CYRUSDB_CREATE? */ r = cyrusdb_open(DB, tmpfile, CYRUSDB_CREATE, &tmp); if(r) goto done; r = cyrusdb_open(DB, tgtfile, CYRUSDB_CREATE, &tgt); if(r) goto done; rock.db = tgt; rock.tid = NULL; r = cyrusdb_foreach(tmp, "", 0, NULL, mboxkey_merge_cb, &rock, &rock.tid); if(r) cyrusdb_abort(rock.db, rock.tid); else cyrusdb_commit(rock.db, rock.tid); done: if(tgt) cyrusdb_close(tgt); if(tmp) cyrusdb_close(tmp); return r; }
EXPORTED void denydb_close(void) { int r; if (denydb) { r = cyrusdb_close(denydb); if (r) { syslog(LOG_ERR, "DENYDB_ERROR: error closing: %s", cyrusdb_strerror(r)); } denydb = NULL; } }
void statuscache_close(void) { int r; if (statuscache_dbopen) { r = cyrusdb_close(statuscachedb); if (r) { syslog(LOG_ERR, "DBERROR: error closing statuscache: %s", cyrusdb_strerror(r)); } statuscache_dbopen = 0; } }
EXPORTED void quotadb_close(void) { int r; if (quota_dbopen) { r = cyrusdb_close(qdb); if (r) { syslog(LOG_ERR, "DBERROR: error closing quotas: %s", cyrusdb_strerror(r)); } quota_dbopen = 0; } }
static int newsrc_done(void) { int r = 0; if (newsrc_dbopen) { r = cyrusdb_close(newsrc_db); if (r) { syslog(LOG_ERR, "DBERROR: error closing fetchnews.db: %s", cyrusdb_strerror(r)); } newsrc_dbopen = 0; } return r; }
/* we want to merge records from "newfile" into * the already existing "currentfile", but only * if the record in newfile is actually newer * (or doesn't exist in currentfile yet) */ int seen_merge(struct seen *seendb, const char *newfile) { int r = 0; struct db *newdb = NULL; r = cyrusdb_open(DB, newfile, 0, &newdb); /* if it doesn't exist, there's nothing * to do, so abort without an error */ if (r == CYRUSDB_NOTFOUND) return 0; if (!r) r = cyrusdb_foreach(newdb, "", 0, NULL, seen_merge_cb, seendb, NULL); if (newdb) cyrusdb_close(newdb); return r; }
int mboxkey_close(struct mboxkey *mboxkeydb) { int r; if (MBOXKEY_DEBUG) { syslog(LOG_DEBUG, "mboxkey_db: mboxkey_close(%s)", mboxkeydb->user); } if (mboxkeydb->tid) { r = cyrusdb_commit(mboxkeydb->db, mboxkeydb->tid); if (r != CYRUSDB_OK) { syslog(LOG_ERR, "DBERROR: error committing mboxkey txn; " "mboxkey state lost: %s", cyrusdb_strerror(r)); } mboxkeydb->tid = NULL; } if (lastmboxkey) { int r; /* free the old database hanging around */ abortcurrent(lastmboxkey); r = cyrusdb_close(lastmboxkey->db); if (r != CYRUSDB_OK) { syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s", cyrusdb_strerror(r)); r = IMAP_IOERROR; } if(!r) lastmboxkey->db = NULL; free(lastmboxkey->user); free(lastmboxkey->fname); free(lastmboxkey); lastmboxkey = NULL; } /* this database can now be reused */ lastmboxkey = mboxkeydb; return 0; }
int mboxkey_done(void) { int r = 0; if (MBOXKEY_DEBUG) { syslog(LOG_DEBUG, "mboxkey_db: mboxkey_done()"); } if (lastmboxkey) { abortcurrent(lastmboxkey); r = cyrusdb_close(lastmboxkey->db); if (r) { syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s", cyrusdb_strerror(r)); r = IMAP_IOERROR; } free(lastmboxkey->user); free(lastmboxkey->fname); free(lastmboxkey); } return r; }
int seen_close(struct seen **seendbptr) { struct seen *seendb = *seendbptr; int r; if (!seendb) return 0; if (SEEN_DEBUG) { syslog(LOG_DEBUG, "seen_db: seen_close(%s)", seendb->user); } if (seendb->tid) { if (SEEN_DEBUG) { syslog(LOG_DEBUG, "seen_db: committing changes for %s", seendb->user); } r = cyrusdb_commit(seendb->db, seendb->tid); if (r != CYRUSDB_OK) { syslog(LOG_ERR, "DBERROR: error committing seen txn; " "seen state lost: %s", cyrusdb_strerror(r)); } seendb->tid = NULL; } r = cyrusdb_close(seendb->db); if (r) { syslog(LOG_ERR, "DBERROR: error closing: %s", cyrusdb_strerror(r)); r = IMAP_IOERROR; } free(seendb->user); free(seendb); *seendbptr = NULL; return 0; }
int main(int argc, char *argv[]) { int iter; int seed; int i; char *key; char *val; struct db *db; int r; struct txn *txn; const char *data; int datalen; struct timeval t1, t2; int initsize; if (argc > 1) { iter = atoi(argv[1]); } else { printf("%s [iterations] [rndseed] [initsize]\n", argv[0]); printf("if iterations is negative, run forever and report every -iter\n"); exit(1); } TRY(DB->init(".", 0)); if (argc > 2) { srand(atoi(argv[2])); } TRY(cyrusdb_open(DB, "scratch", &db)); if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } if (argc > 3) { initsize = atoi(argv[3]); txn = NULL; for (i = 0; i < initsize; i++) { /* generate a random key */ key = genrand(10 + (rand() % 10)); /* generate a random value */ val = genrand(10 + (rand() % 100)); TRY(cyrusdb_store(db, key, strlen(key), val, strlen(val), &txn)); } TRY(cyrusdb_commit(db, txn)); if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } } printf("starting...\n"); /* repeat for ever if iter < 0 */ for (i = 0; iter > 0 ? (i < iter) : 1; i++) { int oper = rand() % 10; if (i > 0 && iter < 0 && ((i % -iter) == 0)) { do_report(); } switch (oper) { case 0: /* do an ADD */ if (verbose) printf("A"); /* insert it */ gettimeofday(&t1, NULL); /* generate a random key */ key = genrand(10 + (rand() % 10)); /* generate a random value */ val = genrand(10 + (rand() % 100)); txn = NULL; TRY(cyrusdb_store(db, key, strlen(key), val, strlen(val), &txn)); TRY(cyrusdb_commit(db, txn)); gettimeofday(&t2, NULL); ADDDIFF(t_add, t1, t2); c_add++; free(key); free(val); break; case 1: /* do a modify */ if (verbose) printf("M"); gettimeofday(&t1, NULL); /* pick a random victim */ count = 0; victim = NULL; txn = NULL; TRY(cyrusdb_foreach(db, NULL, 0, &countem, NULL, NULL, &txn)); if (count == 0) continue; TRY(cyrusdb_foreach(db, NULL, 0, &findvictim, NULL, NULL, &txn)); assert(victim != NULL); /* generate a random value */ val = genrand(10 + (rand() % 100)); /* do an add */ TRY(cyrusdb_store(db, victim, strlen(victim), val, strlen(val), &txn)); free(val); TRY(cyrusdb_commit(db, txn)); free(victim); victim = NULL; gettimeofday(&t2, NULL); ADDDIFF(t_mod, t1, t2); c_mod++; break; case 2: /* do a delete */ if (verbose) printf("D"); gettimeofday(&t1, NULL); /* pick a random victim */ count = 0; victim = NULL; txn = NULL; TRY(cyrusdb_foreach(db, NULL, 0, &countem, NULL, NULL, &txn)); if (count == 0) continue; TRY(cyrusdb_foreach(db, NULL, 0, &findvictim, NULL, NULL, &txn)); assert(victim != NULL); /* delete it */ TRY(cyrusdb_delete(db, victim, strlen(victim), &txn, 0)); TRY(cyrusdb_commit(db, txn)); free(victim); victim = NULL; gettimeofday(&t2, NULL); ADDDIFF(t_del, t1, t2); c_del++; break; default: /* do a "read" */ if (verbose) printf("R"); gettimeofday(&t1, NULL); /* generate a random key */ key = genrand(10 + (rand() % 10)); txn = NULL; TRY(cyrusdb_fetch(db, key, strlen(key), &data, &datalen, &txn)); TRY(cyrusdb_commit(db, txn)); gettimeofday(&t2, NULL); ADDDIFF(t_find, t1, t2); c_find++; free(key); } fflush(stdout); #if 0 /* run the consistency function, if any */ if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } #endif } TRY(cyrusdb_close(db)); TRY(DB->done()); do_report(); return 0; }
int mboxkey_open(const char *user, int flags, struct mboxkey **mboxkeydbptr) { struct mboxkey *mboxkeydb; struct stat sbuf; char *fname = NULL; int r; /* try to reuse the last db handle */ mboxkeydb = lastmboxkey; lastmboxkey = NULL; if (MBOXKEY_DEBUG) { syslog(LOG_DEBUG, "mboxkey_db: mboxkey_open(%s)", user); } /* if this is the db we've already opened, return it */ if (mboxkeydb && !strcmp(mboxkeydb->user, user) && !stat(mboxkeydb->fname, &sbuf)) { abortcurrent(mboxkeydb); *mboxkeydbptr = mboxkeydb; return 0; } *mboxkeydbptr = NULL; /* otherwise, close the existing database */ if (mboxkeydb) { abortcurrent(mboxkeydb); r = cyrusdb_close(mboxkeydb->db); if (r) { syslog(LOG_ERR, "DBERROR: error closing mboxkeydb: %s", cyrusdb_strerror(r)); } free(mboxkeydb->user); free(mboxkeydb->fname); } else { /* create mboxkeydb */ mboxkeydb = (struct mboxkey *) xmalloc(sizeof(struct mboxkey)); } /* open the mboxkeydb corresponding to user */ fname = mboxkey_getpath(user); r = cyrusdb_open(DB, fname, (flags & MBOXKEY_CREATE) ? CYRUSDB_CREATE : 0, &mboxkeydb->db); if (r != 0) { int level = (flags & MBOXKEY_CREATE) ? LOG_ERR : LOG_DEBUG; syslog(level, "DBERROR: opening %s: %s", fname, cyrusdb_strerror(r)); r = IMAP_IOERROR; free(mboxkeydb); free(fname); return r; } syslog(LOG_DEBUG, "mboxkey_db: user %s opened %s", user, fname); mboxkeydb->tid = NULL; mboxkeydb->user = xstrdup(user); mboxkeydb->fname = fname; *mboxkeydbptr = mboxkeydb; return r; }