X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=cdb%2Fwriteable.c;h=9cdd0095bd615fd1a1359e2ee145a81e12487513;hp=2b7adcc486fa1ada9e41336e2d4aa9cc12c1cd0b;hb=5dca360f2da544c9b6baf9d18ad8cd0b0e073e60;hpb=0ec7eaa2194834986c6b46068b9fc459ddf54f87 diff --git a/cdb/writeable.c b/cdb/writeable.c index 2b7adcc..9cdd009 100644 --- a/cdb/writeable.c +++ b/cdb/writeable.c @@ -1,4 +1,22 @@ -/**/ +/* + * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension + * Copyright 2006 Ian Jackson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ #include "chiark_tcl_cdb.h" @@ -23,14 +41,13 @@ typedef struct Pathbuf { char *buf, *sfx; } Pathbuf; -#define MAX_SUFFIX 4 +#define MAX_SUFFIX 5 static void pathbuf_init(Pathbuf *pb, const char *pathb) { int l= strlen(pathb); - pb->buf= TALLOC(l + 4); + pb->buf= TALLOC(l + MAX_SUFFIX + 1); memcpy(pb->buf, pathb, l); pb->sfx= pb->buf + l; - *pb->sfx++= '.'; } static const char *pathbuf_sfx(Pathbuf *pb, const char *suffix) { assert(strlen(suffix) <= MAX_SUFFIX); @@ -166,11 +183,13 @@ static int rw_close(Tcl_Interp *ip, Rw *rw) { } pathbuf_free(&rw->pbsome); pathbuf_free(&rw->pbother); - TFREE(rw); return rc; } -static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw) { rw_close(0,rw); } +static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw_v) { + rw_close(0,rw_v); + TFREE(rw_v); +} const IdDataSpec cdbtcl_rwdatabases= { "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb }; @@ -191,7 +210,7 @@ static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) { /* Remove r where umask would remove w; * eg umask intending 0664 here gives 0660 */ - *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDONLY|O_CREAT, lockmode); + *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDWR|O_CREAT, lockmode); if (*lockfd_r < 0) return cht_posixerr(ip, errno, "could not open/create lockfile"); @@ -255,7 +274,7 @@ static int readstorelogrecord(FILE *f, HashTable *ht, int c, rc, r; c= getc(f); - if (c==EOF) { if (feof(f)) return -1; return -2; } + if (c==EOF) { return feof(f) ? -1 : -2; } if (c=='\n') return -3; if (c!='+') return -2; @@ -276,6 +295,8 @@ static int readstorelogrecord(FILE *f, HashTable *ht, r= fread(htv_fillptr(val), 1,vallen, f); if (r!=vallen) goto x2_free_keyval; + c= getc(f); if (c!='\n') goto x2_free_keyval; + rc= omitfn ? omitfn(val, ctx) : TCL_OK; if (rc) { assert(rc>0); TFREE(val); } else updatefn(ht, key, val); @@ -298,6 +319,9 @@ static int writerecord(FILE *f, const char *key, const HashValue *val) { r= fwrite(val->data, 1, val->len, f); if (r != val->len) return -1; + r= putc('\n', f); + if (r==EOF) return -1; + return 0; } @@ -305,32 +329,45 @@ static int writerecord(FILE *f, const char *key, const HashValue *val) { int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip, const char *pathb) { - static const char *const toremoves[]= { - ".main", ".cdb", ".log", ".tmp", 0 - }; + static const char *const toremoves[]= { ".cdb", ".jrn", ".tmp", 0 }; - Pathbuf pb; - int lock_fd=-1, fd=-1, rc, r; + Pathbuf pb, pbmain; + int lock_fd=-1, rc, r; + FILE *f= 0; const char *const *toremove; + struct stat stab; pathbuf_init(&pb, pathb); + pathbuf_init(&pbmain, pathb); + rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc; - - fd= open(pathbuf_sfx(&pb, ".main"), O_RDWR|O_CREAT|O_EXCL, 0666); - if (fd <= 0) PE("create new database file"); + + r= lstat(pathbuf_sfx(&pbmain, ".main"), &stab); + if (!r) { rc= cht_staticerr(ip, "database already exists during creation", + "CDB ALREADY-EXISTS"); goto x_rc; } + if (errno != ENOENT) PE("check for existing database .main during creation"); for (toremove=toremoves; *toremove; toremove++) { - r= remove(*toremove); + r= remove(pathbuf_sfx(&pb, *toremove)); if (r && errno != ENOENT) PE("delete possible spurious file during creation"); } + f= fopen(pathbuf_sfx(&pb, ".tmp"), "w"); + if (!f) PE("create new database .tmp"); + r= putc('\n', f); if (r==EOF) PE("write sentinel to new database .tmp"); + r= fclose(f); f=0; if (r) PE("close new database .tmp during creation"); + + r= rename(pb.buf, pbmain.buf); + if (r) PE("install new database .tmp as .main (finalising creation)"); + rc= TCL_OK; x_rc: - maybe_close(fd); + if (f) fclose(f); maybe_close(lock_fd); pathbuf_free(&pb); + pathbuf_free(&pbmain); return rc; } @@ -396,6 +433,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, off_t logrecstart, logjunkpos; rw= TALLOC(sizeof(*rw)); + rw->ix= -1; ht_setup(&rw->logincore); cht_scriptinv_init(&rw->on_info); cht_scriptinv_init(&rw->on_lexminval); @@ -404,12 +442,11 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, pathbuf_init(&rw->pbother, pathb); rw->autocompact= 1; - if (on_lexminval) { - rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0); - if (rc) goto x_rc; - } else { - rw->on_lexminval.llength= 0; - } + rc= cht_scriptinv_set(&rw->on_info, ip, on_info, 0); + if (rc) goto x_rc; + + rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0); + if (rc) goto x_rc; mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY); if (mainfd<0) PE("open existing database file .main"); @@ -422,8 +459,8 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, if (rw->cdb_fd >=0) { rc= cdbinit(ip, rw); if (rc) goto x_rc; } else if (errno == ENOENT) { - if (rw->mainsz) { - rc= cht_staticerr(ip, ".cdb does not exist but .main is nonempty -" + if (rw->mainsz > 1) { + rc= cht_staticerr(ip, ".cdb does not exist but .main is >1byte -" " .cdb must have been accidentally deleted!", "CDB CDBMISSING"); goto x_rc; @@ -433,24 +470,24 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, PE("open .cdb"); } - rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "r+"); + rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".jrn"), "r+"); if (!rw->logfile) { - if (errno != ENOENT) PE("failed to open .log during open"); + if (errno != ENOENT) PE("failed to open .jrn during open"); rw->logfile= fopen(rw->pbsome.buf, "w"); - if (!rw->logfile) PE("create .log during (clean) open"); + if (!rw->logfile) PE("create .jrn during (clean) open"); } else { /* rw->logfile */ r= fstat(fileno(rw->logfile), &stab); - if (r==-1) PE("fstat .log during open"); + if (r==-1) PE("fstat .jrn during open"); rc= infocb(ip, rw, "open-dirty-start", "log=%luby", (unsigned long)stab.st_size); if (rc) goto x_rc; for (;;) { logrecstart= ftello(rw->logfile); - if (logrecstart < 0) PE("ftello .log during (dirty) open"); + if (logrecstart < 0) PE("ftello .jrn during (dirty) open"); r= readstorelogrecord(rw->logfile, &rw->logincore, 0,0, ht_update); if (ferror(rw->logfile)) { - rc= cht_posixerr(ip, errno, "error reading .log during (dirty) open"); + rc= cht_posixerr(ip, errno, "error reading .jrn during (dirty) open"); goto x_rc; } if (r==-1) { @@ -458,7 +495,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, } else if (r==-2 || r==-3) { char buf[100]; logjunkpos= ftello(rw->logfile); - if(logjunkpos<0) PE("ftello .log during report of junk in dirty open"); + if(logjunkpos<0) PE("ftello .jrn during report of junk in dirty open"); snprintf(buf,sizeof(buf), "CDB SYNTAX LOG %lu %lu", (unsigned long)logjunkpos, (unsigned long)logrecstart); @@ -467,7 +504,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1)); snprintf(buf,sizeof(buf),"%lu",(unsigned long)logjunkpos); Tcl_ResetResult(ip); - Tcl_AppendResult(ip, "syntax error (junk) in .log during" + Tcl_AppendResult(ip, "syntax error (junk) in .jrn during" " (dirty) open, at file position ", buf, (char*)0); rc= TCL_ERROR; goto x_rc; @@ -477,10 +514,10 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, if (rc) goto x_rc; r= fseeko(rw->logfile, logrecstart, SEEK_SET); - if (r) PE("failed to fseeko .log before junk during dirty open"); + if (r) PE("failed to fseeko .jrn before junk during dirty open"); r= ftruncate(fileno(rw->logfile), logrecstart); - if (r) PE("ftruncate .log to chop junk during dirty open"); + if (r) PE("ftruncate .jrn to chop junk during dirty open"); } else { assert(!r); } @@ -494,6 +531,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, x_rc: rw_close(0,rw); + TFREE(rw); maybe_close(mainfd); return rc; } @@ -509,16 +547,16 @@ int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb, struct ht_forall_ctx { struct cdb_make cdbm; FILE *mainfile; - int lexminvall; long *reccount; - const char *lexminval; + int lexminvall; + const char *lexminval; /* may be invalid if lexminvall <= 0 */ }; /*---------- helper functions ----------*/ static int expiredp(const HashValue *val, struct ht_forall_ctx *a) { int r, l; - if (!val->len) return 0; + if (!val->len || a->lexminvall<=0) return 0; l= val->len < a->lexminvall ? val->len : a->lexminvall; r= memcmp(val->data, a->lexminval, l); if (r>0) return 0; @@ -552,7 +590,7 @@ 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 .jrn with old data * leaves cdb fd open onto old db * leaves logincore full of crap */ @@ -578,7 +616,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, logsz, (unsigned long)rw->mainsz); if (rc) goto x_rc; - if (rw->on_lexminval.llength) { + if (cht_scriptinv_interp(&rw->on_lexminval)) { rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0); if (rc) goto x_rc; @@ -592,7 +630,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, rc= ht_forall(&rw->logincore, delete_ifexpired, &a); } else { - a.lexminval= ""; + a.lexminvall= 0; } /* merge unsuperseded records from main into hash table */ @@ -605,19 +643,23 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, expiredp, &a, ht_maybeupdate); if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading" - " .main during compact"); goto x_rc; - } + " .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); + snprintf(buf,sizeof(buf), "CDB %s MAIN %lu", + r==-1 ? "TRUNCATED" : "SYNTAX", (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); + Tcl_AppendResult(ip, + r==-1 ? "unexpected eof (truncated file)" + " in .main during compact, at file position " + : "syntax error" + " in .main during compact, at file position ", + buf, (char*)0); rc= TCL_ERROR; goto x_rc; } else { @@ -658,6 +700,9 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, 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= putc('\n', a.mainfile); + if (r==EOF) PE("write trailing \n to main during compact"); r= fflush(a.mainfile); if (r) PE("fflush new main during compact"); r= fdatasync(fileno(a.mainfile)); @@ -676,7 +721,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, /* done! */ - rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%l", + rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%ld", (unsigned long)rw->mainsz, *a.reccount); if (rc) goto x_rc; @@ -701,8 +746,8 @@ static int compact_forclose(Tcl_Interp *ip, Rw *rw, long *reccount_r) { 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)"); + r= remove(pathbuf_sfx(&rw->pbsome,".jrn")); + if (r) PE("remove .jrn (during tidy close)"); return TCL_OK; @@ -719,7 +764,7 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) { else rc= TCL_OK; if (!rc) { - if (!rw->logfile) { + if (rw->logfile) { logsz= ftello(rw->logfile); if (logsz < 0) rc= cht_posixerr(ip, errno, "ftell logfile during close info"); @@ -727,7 +772,8 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) { 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); + rc= infocb(ip, rw, "close", "main=%luby nrecs=%ld", + rw->mainsz, reccount); } else { rc= infocb(ip, rw, "close", "main=%luby", rw->mainsz); } @@ -736,6 +782,7 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) { if (rc_close) rc= rc_close; cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases); + TFREE(rw); return rc; } @@ -747,10 +794,19 @@ static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) { int rc, r; logsz= ftello(rw->logfile); - if (logsz < 0) return cht_posixerr(ip, errno, "ftell .log" + if (logsz < 0) return cht_posixerr(ip, errno, "ftell .jrn" " during compact check or force"); - if (!force && logsz < rw->mainsz / 10 + 1000) return TCL_OK; + if (!force && logsz < rw->mainsz / 3 + 1000) return TCL_OK; + /* Test case: ^^^ testing best value for this + * main=9690434by nrecs=122803 read all in one go + * no autocompact, : 6.96user 0.68system 0:08.93elapsed + * auto, mulitplier 2: 7.10user 0.79system 0:09.54elapsed + * auto, unity: 7.80user 0.98system 0:11.84elapsed + * auto, divisor 2: 8.23user 1.05system 0:13.30elapsed + * auto, divisor 3: 8.55user 1.12system 0:12.88elapsed + * auto, divisor 5: 9.95user 1.43system 0:15.72elapsed + */ rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc; @@ -764,10 +820,10 @@ static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) { 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"); + rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".jrn"), "w"); + if (!rw->logfile) PE("reopen .jrn after compact"); - r= fsync(fileno(rw->logfile)); if (r) PE("fsync .log after compact reopen"); + r= fsync(fileno(rw->logfile)); if (r) PE("fsync .jrn after compact reopen"); return TCL_OK; @@ -815,6 +871,8 @@ static int update(Tcl_Interp *ip, Rw *rw, const char *key, if (r) PE("write update to logfile"); ht_update(&rw->logincore, key, val); + + if (!rw->autocompact) return TCL_OK; return compact_keepopen(ip, rw, 0); x_rc: