+static int expiredp(const HashValue *val, struct ht_forall_ctx *a) {
+ int r, l;
+ if (!val->len) return 0;
+ l= val->len < a->lexminvall ? val->len : a->lexminvall;
+ r= memcmp(val->data, a->lexminval, l);
+ if (r>0) return 0;
+ if (r<0) return 1;
+ return val->len < a->lexminvall;
+}
+
+static int delete_ifexpired(const char *key, HashValue *val,
+ struct ht_forall_ctx *a) {
+ if (!expiredp(val, a)) return 0;
+ val->len= 0;
+ /* we don't actually need to realloc it to free the memory because
+ * this will shortly all be deleted as part of the compaction */
+ return 0;
+}
+
+static int addto_cdb(const char *key, HashValue *val,
+ struct ht_forall_ctx *a) {
+ return cdb_make_add(&a->cdbm, key, strlen(key), val->data, val->len);
+}
+
+static int addto_main(const char *key, HashValue *val,
+ struct ht_forall_ctx *a) {
+ (*a->reccount)++;
+ return writerecord(a->mainfile, key, val);
+}
+
+/*---------- compact main entrypoint ----------*/
+
+static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
+ long *reccount_r) {
+ /* creates new .cdb and .main
+ * closes logfile
+ * leaves .log with old data
+ * leaves cdb fd open onto old db
+ * leaves logincore full of crap
+ */
+ int r, rc;
+ int cdbfd, cdbmaking;
+ off_t errpos, newmainsz;
+ char buf[100];
+ Tcl_Obj *res;
+ struct ht_forall_ctx a;
+
+ a.mainfile= 0;
+ cdbfd= -1;
+ cdbmaking= 0;
+ *reccount_r= 0;
+ a.reccount= reccount_r;
+
+ r= fclose(rw->logfile);
+ if (r) { rc= cht_posixerr(ip, errno, "probable data loss! failed to fclose"
+ " logfile during compact"); goto x_rc; }
+ rw->logfile= 0;
+
+ rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
+ logsz, (unsigned long)rw->mainsz);
+ if (rc) goto x_rc;
+
+ if (rw->on_lexminval.llength) {
+ rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0);
+ if (rc) goto x_rc;
+
+ res= Tcl_GetObjResult(ip); assert(res);
+ a.lexminval= Tcl_GetStringFromObj(res, &a.lexminvall);
+ assert(a.lexminval);
+
+ /* we rely not calling Tcl_Eval during the actual compaction;
+ * if we did Tcl_Eval then the interp result would be trashed.
+ */
+ rc= ht_forall(&rw->logincore, delete_ifexpired, &a);
+
+ } else {
+ a.lexminval= "";
+ }
+
+ /* merge unsuperseded records from main into hash table */
+
+ a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".main"), "r");
+ if (!a.mainfile) PE("failed to open .main for reading during compact");
+
+ for (;;) {
+ r= readstorelogrecord(a.mainfile, &rw->logincore,
+ expiredp, &a,
+ ht_maybeupdate);
+ if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading"
+ " .main during compact"); goto x_rc;
+ }
+ if (r==-3) {
+ break;
+ } else if (r==-1 || r==-2) {
+ errpos= ftello(a.mainfile);
+ if (errpos<0) PE("ftello .main during report of syntax error");
+ snprintf(buf,sizeof(buf), "CDB SYNTAX MAIN %lu", (unsigned long)errpos);
+ Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
+ snprintf(buf,sizeof(buf), "%lu", (unsigned long)errpos);
+ Tcl_ResetResult(ip);
+ Tcl_AppendResult(ip, "syntax error in .main during"
+ " compact, at file position ", buf, (char*)0);
+ rc= TCL_ERROR;
+ goto x_rc;
+ } else {
+ assert(!rc);
+ }
+ }
+ fclose(a.mainfile);
+ a.mainfile= 0;
+
+ /* create new cdb */
+
+ cdbfd= open(pathbuf_sfx(&rw->pbsome,".tmp"), O_WRONLY|O_CREAT|O_TRUNC, 0666);
+ if (cdbfd<0) PE("create .tmp for new cdb during compact");
+
+ r= cdb_make_start(&a.cdbm, cdbfd);
+ if (r) PE("cdb_make_start during compact");
+ cdbmaking= 1;
+
+ r= ht_forall(&rw->logincore, addto_cdb, &a);
+ if (r) PE("cdb_make_add during compact");
+
+ r= cdb_make_finish(&a.cdbm);
+ if(r) PE("cdb_make_finish during compact");
+ cdbmaking= 0;
+
+ r= fdatasync(cdbfd); if (r) PE("fdatasync new cdb during compact");
+ r= close(cdbfd); if (r) PE("close new cdb during compact");
+ cdbfd= -1;
+
+ r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".cdb"));
+ if (r) PE("install new .cdb during compact");
+
+ /* create new main */
+
+ a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".tmp"), "w");
+ if (!a.mainfile) PE("create .tmp for new main during compact");
+
+ r= ht_forall(&rw->logincore, addto_main, &a);
+ if (r) { rc= cht_posixerr(ip, r, "error writing to new .main"
+ " during compact"); goto x_rc; }
+
+ r= fflush(a.mainfile); if (r) PE("fflush new main during compact");
+ r= fdatasync(fileno(a.mainfile));
+ if (r) PE("fdatasync new main during compact");
+
+ newmainsz= ftello(a.mainfile);
+ if (newmainsz<0) PE("ftello new main during compact");
+
+ r= fclose(a.mainfile); if (r) PE("fclose new main during compact");
+ a.mainfile= 0;
+
+ r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".main"));
+ if (r) PE("install new .main during compact");
+
+ rw->mainsz= newmainsz;
+
+ /* done! */
+
+ rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%l",
+ (unsigned long)rw->mainsz, *a.reccount);
+ if (rc) goto x_rc;
+
+ return rc;
+
+x_rc:
+ if (a.mainfile) fclose(a.mainfile);
+ if (cdbmaking) cdb_make_finish(&a.cdbm);
+ maybe_close(cdbfd);
+ remove(pathbuf_sfx(&rw->pbsome,".tmp")); /* for tidyness */
+ return rc;
+}
+
+/*---------- Closing ----------*/
+
+static int compact_forclose(Tcl_Interp *ip, Rw *rw, long *reccount_r) {
+ off_t logsz;
+ int r, rc;
+
+ logsz= ftello(rw->logfile);
+ if (logsz < 0) PE("ftello logfile (during tidy close)");
+
+ rc= compact_core(ip, rw, logsz, reccount_r); if (rc) goto x_rc;
+
+ r= remove(pathbuf_sfx(&rw->pbsome,".log"));
+ if (r) PE("remove .log (during tidy close)");
+
+ return TCL_OK;
+
+x_rc: return rc;
+}
+
+int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
+ Rw *rw= rw_v;
+ int rc, rc_close;
+ long reccount= -1;
+ off_t logsz;
+
+ if (rw->autocompact) rc= compact_forclose(ip, rw, &reccount);
+ else rc= TCL_OK;
+
+ if (!rc) {
+ if (!rw->logfile) {
+ logsz= ftello(rw->logfile);
+ if (logsz < 0)
+ rc= cht_posixerr(ip, errno, "ftell logfile during close info");
+ else
+ rc= infocb(ip, rw, "close", "main=%luby log=%luby",
+ rw->mainsz, logsz);
+ } else if (reccount>=0) {
+ rc= infocb(ip, rw, "close", "main=%luby nrecs=%l", rw->mainsz, reccount);
+ } else {
+ rc= infocb(ip, rw, "close", "main=%luby", rw->mainsz);
+ }
+ }
+ rc_close= rw_close(ip,rw);
+ if (rc_close) rc= rc_close;
+
+ cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases);
+ return rc;
+}
+
+/*---------- Other compaction-related entrypoints ----------*/
+
+static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
+ off_t logsz;
+ long reccount;
+ int rc, r;
+
+ logsz= ftello(rw->logfile);
+ if (logsz < 0) return cht_posixerr(ip, errno, "ftell .log"
+ " during compact check or force");
+
+ if (!force && logsz < rw->mainsz / 10 + 1000) return TCL_OK;
+
+ rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc;
+
+ maybe_close(rw->cdb_fd);
+ rw->cdb_fd= -1;
+ ht_destroy(&rw->logincore);
+ ht_setup(&rw->logincore);
+
+ rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
+ if (rw->cdb_fd < 0) PE("reopen .cdb after compact");
+
+ rc= cdbinit(ip, rw); if (rc) goto x_rc;
+
+ rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "w");
+ if (!rw->logfile) PE("reopen .log after compact");
+
+ r= fsync(fileno(rw->logfile)); if (r) PE("fsync .log after compact reopen");
+
+ return TCL_OK;
+
+x_rc:
+ /* doom! all updates fail after this (because rw->logfile is 0), and
+ * we may be using a lot more RAM than would be ideal. Program will
+ * have to reopen if it really wants sanity. */
+ return rc;
+}
+
+int cht_do_cdbwr_compact_force(ClientData cd, Tcl_Interp *ip, void *rw_v) {
+ return compact_keepopen(ip, rw_v, 1);
+}
+int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) {
+ return compact_keepopen(ip, rw_v, 0);
+}
+
+int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) {
+ Rw *rw= rw_v;
+ rw->autocompact= 0;
+ return TCL_OK;
+}
+int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) {
+ Rw *rw= rw_v;
+ rw->autocompact= 1;
+ return TCL_OK;
+}
+
+/*---------- Updateing ----------*/
+
+static int update(Tcl_Interp *ip, Rw *rw, const char *key,
+ const Byte *data, int dlen) {
+ HashValue *val;
+ int rc, r;
+
+ if (!rw->logfile) return cht_staticerr
+ (ip, "previous compact failed; cdbwr must be closed and reopened "
+ "before any further updates", "CDB BROKEN");
+
+ val= htv_prep(dlen); assert(val);
+ memcpy(htv_fillptr(val), data, dlen);
+
+ r= writerecord(rw->logfile, key, val);
+ if (!r) r= fflush(rw->logfile);
+ if (r) PE("write update to logfile");
+
+ ht_update(&rw->logincore, key, val);
+ return compact_keepopen(ip, rw, 0);
+
+ x_rc:
+ TFREE(val);
+ return rc;
+}
+
+int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip,
+ void *rw_v, const char *key, Tcl_Obj *value) {
+ int dlen;
+ const char *data;
+ data= Tcl_GetStringFromObj(value, &dlen); assert(data);
+ return update(ip, rw_v, key, data, dlen);
+}
+
+int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip,
+ void *rw_v, const char *key, HBytes_Value value) {
+ return update(ip, rw_v, key, cht_hb_data(&value), cht_hb_len(&value));
+}
+
+int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
+ const char *key) {
+ return update(ip, rw_v, key, 0, 0);
+}
+
+/*---------- Lookups ----------*/
+
+static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key,
+ const Byte **data_r, int *len_r /* -1 => notfound */) {
+ Rw *rw= rw_v;
+ const HashValue *val;
+
+ val= ht_lookup(&rw->logincore, key);
+ if (val) {
+ if (val->len) { *data_r= val->data; *len_r= val->len; return TCL_OK; }
+ else { *data_r= 0; *len_r= -1; return TCL_OK; }
+ }
+
+ return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
+}
+
+int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
+ const char *key, Tcl_Obj *def,
+ Tcl_Obj **result) {
+ const Byte *data;
+ int dlen, r;
+
+ r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
+ return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
+ cht_cdb_storeanswer_string);
+}
+
+int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
+ const char *key, Tcl_Obj *def,
+ Tcl_Obj **result) {
+ const Byte *data;
+ int dlen, r;
+
+ r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
+ return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
+ cht_cdb_storeanswer_hb);
+}
+
+int cht_do_cdbtoplevel_cdb_wr(ClientData cd, Tcl_Interp *ip,
+ const Cdbwr_SubCommand* subcmd,
+ int objc, Tcl_Obj *const *objv) {
+ return subcmd->func((void*)subcmd,ip,objc,objv);
+}