2 * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension
3 * Copyright 2006 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include "chiark_tcl_cdb.h"
23 #define KEYLEN_MAX (INT_MAX/2)
28 /*---------- Forward declarations ----------*/
32 /*---------- Useful routines ----------*/
34 static void maybe_close(int fd) {
38 /*==================== Subsystems and subtypes ====================*/
40 /*---------- Pathbuf ----------*/
42 typedef struct Pathbuf {
48 static void pathbuf_init(Pathbuf *pb, const char *pathb) {
49 size_t l= strlen(pathb);
51 pb->buf= TALLOC(l + MAX_SUFFIX + 1);
52 memcpy(pb->buf, pathb, l);
55 static const char *pathbuf_sfx(Pathbuf *pb, const char *suffix) {
56 assert(strlen(suffix) <= MAX_SUFFIX);
57 strcpy(pb->sfx, suffix);
60 static void pathbuf_free(Pathbuf *pb) {
65 /*---------- Our hash table ----------*/
67 typedef struct HashTable {
69 Byte padding[128]; /* allow for expansion by Tcl, urgh */
73 typedef struct HashValue {
78 static HashValue *htv_prep(int len) {
80 hd= TALLOC((hd->data - (Byte*)hd) + len);
84 static Byte *htv_fillptr(HashValue *hd) {
88 static void ht_setup(HashTable *ht) {
89 Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS);
91 static void ht_update(HashTable *ht, const char *key, HashValue *val_eat) {
95 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
96 if (!new) TFREE(Tcl_GetHashValue(he));
97 Tcl_SetHashValue(he, val_eat);
98 /* eats the value since the data structure owns the memory */
100 static void ht_maybeupdate(HashTable *ht, const char *key,
101 HashValue *val_eat) {
102 /* like ht_update except does not overwrite existing values */
106 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
107 if (!new) { TFREE(val_eat); return; }
108 Tcl_SetHashValue(he, val_eat);
111 static const HashValue *ht_lookup(HashTable *ht, const char *key) {
114 he= Tcl_FindHashEntry(&ht->t, key);
117 return Tcl_GetHashValue(he);
120 static int ht_forall(HashTable *ht,
121 int (*fn)(const char *key, HashValue *val,
122 struct ht_forall_ctx *ctx),
123 struct ht_forall_ctx *ctx) {
124 /* Returns first positive value returned by any call to fn, or 0. */
131 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
133 he= Tcl_NextHashEntry(&sp)) {
134 val= Tcl_GetHashValue(he);
135 if (!val->len) continue;
137 key= Tcl_GetHashKey(&ht->t, he);
139 r= fn(key, val, ctx);
145 static void ht_destroy(HashTable *ht) {
149 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
151 he= Tcl_NextHashEntry(&sp)) {
152 /* ht_forall skips empty (deleted) entries so is no good for this */
153 TFREE(Tcl_GetHashValue(he));
155 Tcl_DeleteHashTable(&ht->t);
158 /*==================== Existential ====================*/
160 /*---------- Rw data structure ----------*/
165 struct cdb cdb; /* valid iff cdb_fd >= 0 */
168 Pathbuf pbsome, pbother;
170 ScriptToInvoke on_info, on_lexminval;
173 static int rw_close(Tcl_Interp *ip, Rw *rw) {
177 ht_destroy(&rw->logincore);
178 if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
179 maybe_close(rw->cdb_fd);
180 maybe_close(rw->lock_fd);
183 r= fclose(rw->logfile);
184 if (r && ip) { rc= cht_posixerr(ip, errno, "probable data loss! failed to"
185 " fclose logfile during untidy close"); }
188 pathbuf_free(&rw->pbsome); pathbuf_free(&rw->pbother);
192 static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw_v) {
196 const IdDataSpec cdbtcl_rwdatabases= {
197 "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb
200 /*---------- File handling ----------*/
202 static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) {
203 /* *lockfd_r must be -1 on entry. If may be set to >=0 even
204 * on error, and must be closed by the caller. */
209 um= umask(~(mode_t)0);
212 lockmode= 0666 & ~((um & 0444)>>1);
213 /* Remove r where umask would remove w;
214 * eg umask intending 0664 here gives 0660 */
216 *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDWR|O_CREAT, lockmode);
218 return cht_posixerr(ip, errno, "could not open/create lockfile");
221 fl.l_whence= SEEK_SET;
226 r= fcntl(*lockfd_r, F_SETLK, &fl);
228 if (errno == EACCES || errno == EAGAIN)
229 return cht_staticerr(ip, "lock held by another process", "CDB LOCKED");
230 else return cht_posixerr(ip, errno, "unexpected error from fcntl while"
237 /*---------- Log reading and writing ----------*/
239 static int readlognum(FILE *f, int delim, int *num_r) {
241 char numbuf[20], *p, *ep;
246 c= getc(f); if (c==EOF) return -2;
247 if (c == delim) break;
248 if (!isdigit((unsigned char)c)) return -2;
250 if (p == numbuf+sizeof(numbuf)) return -2;
252 if (p == numbuf) return -2;
255 errno=0; ul= strtoul(numbuf, &ep, 10);
256 if (*ep || errno || ul >= KEYLEN_MAX) return -2;
261 static int readstorelogrecord(FILE *f, HashTable *ht,
262 int (*omitfn)(const HashValue*,
263 struct ht_forall_ctx *ctx),
264 struct ht_forall_ctx *ctx,
265 void (*updatefn)(HashTable*, const char*,
270 * -2 corrupt or error
271 * -3 got newline indicating end
272 * >0 value from omitfn
280 if (c==EOF) { return feof(f) ? -1 : -2; }
281 if (c=='\n') return -3;
282 if (c!='+') return -2;
284 rc= readlognum(f, ',', &keylen); if (rc) return rc;
285 rc= readlognum(f, ':', &vallen); if (rc) return rc;
287 key= TALLOC(keylen+1);
288 val= htv_prep(vallen);
290 r= fread(key, 1,keylen, f);
291 if (r!=keylen) goto x2_free_keyval;
292 if (memchr(key,0,keylen)) goto x2_free_keyval;
295 c= getc(f); if (c!='-') goto x2_free_keyval;
296 c= getc(f); if (c!='>') goto x2_free_keyval;
298 r= fread(htv_fillptr(val), 1,vallen, f);
299 if (r!=vallen) goto x2_free_keyval;
301 c= getc(f); if (c!='\n') goto x2_free_keyval;
303 rc= omitfn ? omitfn(val, ctx) : TCL_OK;
304 if (rc) { assert(rc>0); TFREE(val); }
305 else updatefn(ht, key, val);
316 static int writerecord(FILE *f, const char *key, const HashValue *val) {
319 r= fprintf(f, "+%d,%d:%s->", (int)strlen(key), val->len, key);
322 r= fwrite(val->data, 1, val->len, f);
323 if (r != val->len) return -1;
326 if (r==EOF) return -1;
331 /*---------- Creating ----------*/
333 int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip,
335 static const char *const toremoves[]= { ".cdb", ".jrn", ".tmp", 0 };
338 int lock_fd=-1, rc, r;
340 const char *const *toremove;
343 pathbuf_init(&pb, pathb);
344 pathbuf_init(&pbmain, pathb);
346 rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc;
348 r= lstat(pathbuf_sfx(&pbmain, ".main"), &stab);
349 if (!r) { rc= cht_staticerr(ip, "database already exists during creation",
350 "CDB ALREADY-EXISTS"); goto x_rc; }
351 if (errno != ENOENT) PE("check for existing database .main during creation");
353 for (toremove=toremoves; *toremove; toremove++) {
354 r= remove(pathbuf_sfx(&pb, *toremove));
355 if (r && errno != ENOENT)
356 PE("delete possible spurious file during creation");
359 f= fopen(pathbuf_sfx(&pb, ".tmp"), "w");
360 if (!f) PE("create new database .tmp");
361 r= putc('\n', f); if (r==EOF) PE("write sentinel to new database .tmp");
362 r= fclose(f); f=0; if (r) PE("close new database .tmp during creation");
364 r= rename(pb.buf, pbmain.buf);
365 if (r) PE("install new database .tmp as .main (finalising creation)");
371 maybe_close(lock_fd);
373 pathbuf_free(&pbmain);
377 /*---------- Info callbacks ----------*/
379 static int infocbv3(Tcl_Interp *ip, Rw *rw, const char *arg1,
380 const char *arg2fmt, const char *arg3, va_list al) {
384 vsnprintf(buf, sizeof(buf), arg2fmt, al);
387 aa[na++]= cht_ret_string(ip, arg1);
388 aa[na++]= cht_ret_string(ip, buf);
389 if (arg3) aa[na++]= cht_ret_string(ip, arg3);
391 return cht_scriptinv_invoke_fg(&rw->on_info, na, aa);
394 static int infocb3(Tcl_Interp *ip, Rw *rw, const char *arg1,
395 const char *arg2fmt, const char *arg3, ...) {
399 rc= infocbv3(ip,rw,arg1,arg2fmt,arg3,al);
404 static int infocb(Tcl_Interp *ip, Rw *rw, const char *arg1,
405 const char *arg2fmt, ...) {
408 va_start(al, arg2fmt);
409 rc= infocbv3(ip,rw,arg1,arg2fmt,0,al);
414 /*---------- Opening ----------*/
416 static int cdbinit(Tcl_Interp *ip, Rw *rw) {
417 /* On entry, cdb_fd >=0 but cdb is _undefined_/
418 * On exit, either cdb_fd<0 or cdb is initialised */
421 r= cdb_init(&rw->cdb, rw->cdb_fd);
423 rc= cht_posixerr(ip, errno, "failed to initialise cdb reader");
424 close(rw->cdb_fd); rw->cdb_fd= -1; return rc;
429 int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
430 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
432 const Cdbwr_SubCommand *subcmd= cd;
433 int r, rc, mainfd=-1;
436 off_t logrecstart, logjunkpos;
438 rw= TALLOC(sizeof(*rw));
440 ht_setup(&rw->logincore);
441 cht_scriptinv_init(&rw->on_info);
442 cht_scriptinv_init(&rw->on_lexminval);
443 rw->cdb_fd= rw->lock_fd= -1; rw->logfile= 0;
444 pathbuf_init(&rw->pbsome, pathb);
445 pathbuf_init(&rw->pbother, pathb);
448 rc= cht_scriptinv_set(&rw->on_info, ip, on_info, 0);
451 rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0);
454 mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY);
455 if (mainfd<0) PE("open existing database file .main");
456 rc= acquire_lock(ip, &rw->pbsome, &rw->lock_fd); if (rc) goto x_rc;
458 r= fstat(mainfd, &stab); if (r) PE("fstat .main");
459 rw->mainsz= stab.st_size;
461 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
462 if (rw->cdb_fd >=0) {
463 rc= cdbinit(ip, rw); if (rc) goto x_rc;
464 } else if (errno == ENOENT) {
465 if (rw->mainsz > 1) {
466 rc= cht_staticerr(ip, ".cdb does not exist but .main is >1byte -"
467 " .cdb must have been accidentally deleted!",
476 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".jrn"), "r+");
478 if (errno != ENOENT) PE("failed to open .jrn during open");
479 rw->logfile= fopen(rw->pbsome.buf, "w");
480 if (!rw->logfile) PE("create .jrn during (clean) open");
481 } else { /* rw->logfile */
482 r= fstat(fileno(rw->logfile), &stab);
483 if (r==-1) PE("fstat .jrn during open");
484 rc= infocb(ip, rw, "open-dirty-start", "log=%luby",
485 (unsigned long)stab.st_size);
489 logrecstart= ftello(rw->logfile);
490 if (logrecstart < 0) PE("ftello .jrn during (dirty) open");
491 r= readstorelogrecord(rw->logfile, &rw->logincore, 0,0, ht_update);
492 if (ferror(rw->logfile)) {
493 rc= cht_posixerr(ip, errno, "error reading .jrn during (dirty) open");
498 } else if (r==-2 || r==-3) {
500 logjunkpos= ftello(rw->logfile);
501 if(logjunkpos<0) PE("ftello .jrn during report of junk in dirty open");
503 snprintf(buf,sizeof(buf), "CDB SYNTAX LOG %lu %lu",
504 (unsigned long)logjunkpos, (unsigned long)logrecstart);
506 if (!(subcmd->flags & RWSCF_OKJUNK)) {
507 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
508 snprintf(buf,sizeof(buf),"%lu",(unsigned long)logjunkpos);
510 Tcl_AppendResult(ip, "syntax error (junk) in .jrn during"
511 " (dirty) open, at file position ", buf, (char*)0);
515 rc= infocb3(ip, rw, "open-dirty-junk", "errorfpos=%luby", buf,
516 (unsigned long)logjunkpos);
519 r= fseeko(rw->logfile, logrecstart, SEEK_SET);
520 if (r) PE("failed to fseeko .jrn before junk during dirty open");
522 r= ftruncate(fileno(rw->logfile), logrecstart);
523 if (r) PE("ftruncate .jrn to chop junk during dirty open");
529 /* now log is positioned for appending and everything is read */
542 int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb,
543 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
545 return cht_do_cdbwr_open(cd,ip,pathb,on_info,on_lexminval,result);
548 /*==================== COMPACTION ====================*/
550 struct ht_forall_ctx {
551 struct cdb_make cdbm;
555 const char *lexminval; /* may be invalid if lexminvall <= 0 */
558 /*---------- helper functions ----------*/
560 static int expiredp(const HashValue *val, struct ht_forall_ctx *a) {
562 if (!val->len || a->lexminvall<=0) return 0;
563 l= val->len < a->lexminvall ? val->len : a->lexminvall;
564 r= memcmp(val->data, a->lexminval, l);
567 return val->len < a->lexminvall;
570 static int delete_ifexpired(const char *key, HashValue *val,
571 struct ht_forall_ctx *a) {
572 if (!expiredp(val, a)) return 0;
574 /* we don't actually need to realloc it to free the memory because
575 * this will shortly all be deleted as part of the compaction */
579 static int addto_cdb(const char *key, HashValue *val,
580 struct ht_forall_ctx *a) {
581 return cdb_make_add(&a->cdbm, key, strlen(key), val->data, val->len);
584 static int addto_main(const char *key, HashValue *val,
585 struct ht_forall_ctx *a) {
587 return writerecord(a->mainfile, key, val);
590 /*---------- compact main entrypoint ----------*/
592 static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
594 /* creates new .cdb and .main
596 * leaves .jrn with old data
597 * leaves cdb fd open onto old db
598 * leaves logincore full of crap
601 int cdbfd, cdbmaking;
602 off_t errpos, newmainsz;
605 struct ht_forall_ctx a;
611 a.reccount= reccount_r;
613 r= fclose(rw->logfile);
614 if (r) { rc= cht_posixerr(ip, errno, "probable data loss! failed to fclose"
615 " logfile during compact"); goto x_rc; }
618 rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
619 logsz, (unsigned long)rw->mainsz);
622 if (cht_scriptinv_interp(&rw->on_lexminval)) {
623 rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0);
626 res= Tcl_GetObjResult(ip); assert(res);
627 a.lexminval= Tcl_GetStringFromObj(res, &a.lexminvall);
630 /* we rely not calling Tcl_Eval during the actual compaction;
631 * if we did Tcl_Eval then the interp result would be trashed.
633 rc= ht_forall(&rw->logincore, delete_ifexpired, &a);
639 /* merge unsuperseded records from main into hash table */
641 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".main"), "r");
642 if (!a.mainfile) PE("failed to open .main for reading during compact");
645 r= readstorelogrecord(a.mainfile, &rw->logincore,
648 if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading"
649 " .main during compact"); goto x_rc; }
652 } else if (r==-1 || r==-2) {
653 errpos= ftello(a.mainfile);
654 if (errpos<0) PE("ftello .main during report of syntax error");
655 snprintf(buf,sizeof(buf), "CDB %s MAIN %lu",
656 r==-1 ? "TRUNCATED" : "SYNTAX", (unsigned long)errpos);
657 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
658 snprintf(buf,sizeof(buf), "%lu", (unsigned long)errpos);
661 r==-1 ? "unexpected eof (truncated file)"
662 " in .main during compact, at file position "
664 " in .main during compact, at file position ",
677 cdbfd= open(pathbuf_sfx(&rw->pbsome,".tmp"), O_WRONLY|O_CREAT|O_TRUNC, 0666);
678 if (cdbfd<0) PE("create .tmp for new cdb during compact");
680 r= cdb_make_start(&a.cdbm, cdbfd);
681 if (r) PE("cdb_make_start during compact");
684 r= ht_forall(&rw->logincore, addto_cdb, &a);
685 if (r) PE("cdb_make_add during compact");
687 r= cdb_make_finish(&a.cdbm);
688 if(r) PE("cdb_make_finish during compact");
691 r= fdatasync(cdbfd); if (r) PE("fdatasync new cdb during compact");
692 r= close(cdbfd); if (r) PE("close new cdb during compact");
695 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".cdb"));
696 if (r) PE("install new .cdb during compact");
698 /* create new main */
700 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".tmp"), "w");
701 if (!a.mainfile) PE("create .tmp for new main during compact");
703 r= ht_forall(&rw->logincore, addto_main, &a);
704 if (r) { rc= cht_posixerr(ip, errno, "error writing to new .main"
705 " during compact"); goto x_rc; }
707 r= putc('\n', a.mainfile);
708 if (r==EOF) PE("write trailing \n to main during compact");
710 r= fflush(a.mainfile); if (r) PE("fflush new main during compact");
711 r= fdatasync(fileno(a.mainfile));
712 if (r) PE("fdatasync new main during compact");
714 newmainsz= ftello(a.mainfile);
715 if (newmainsz<0) PE("ftello new main during compact");
717 r= fclose(a.mainfile); if (r) PE("fclose new main during compact");
720 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".main"));
721 if (r) PE("install new .main during compact");
723 rw->mainsz= newmainsz;
727 rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%ld",
728 (unsigned long)rw->mainsz, *a.reccount);
734 if (a.mainfile) fclose(a.mainfile);
735 if (cdbmaking) cdb_make_finish(&a.cdbm);
737 remove(pathbuf_sfx(&rw->pbsome,".tmp")); /* for tidyness */
741 /*---------- Closing ----------*/
743 static int compact_forclose(Tcl_Interp *ip, Rw *rw, long *reccount_r) {
747 logsz= ftello(rw->logfile);
748 if (logsz < 0) PE("ftello logfile (during tidy close)");
750 rc= compact_core(ip, rw, logsz, reccount_r); if (rc) goto x_rc;
752 r= remove(pathbuf_sfx(&rw->pbsome,".jrn"));
753 if (r) PE("remove .jrn (during tidy close)");
760 int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
766 if (rw->autocompact) rc= compact_forclose(ip, rw, &reccount);
771 logsz= ftello(rw->logfile);
773 rc= cht_posixerr(ip, errno, "ftell logfile during close info");
775 rc= infocb(ip, rw, "close", "main=%luby log=%luby",
777 } else if (reccount>=0) {
778 rc= infocb(ip, rw, "close", "main=%luby nrecs=%ld",
779 rw->mainsz, reccount);
781 rc= infocb(ip, rw, "close", "main=%luby", rw->mainsz);
784 rc_close= rw_close(ip,rw);
785 if (rc_close) rc= rc_close;
787 cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases);
792 /*---------- Other compaction-related entrypoints ----------*/
794 static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
799 logsz= ftello(rw->logfile);
800 if (logsz < 0) return cht_posixerr(ip, errno, "ftell .jrn"
801 " during compact check or force");
803 if (!force && logsz < rw->mainsz / 3 + 1000) return TCL_OK;
804 /* Test case: ^^^ testing best value for this
805 * main=9690434by nrecs=122803 read all in one go
806 * no autocompact, : 6.96user 0.68system 0:08.93elapsed
807 * auto, mulitplier 2: 7.10user 0.79system 0:09.54elapsed
808 * auto, unity: 7.80user 0.98system 0:11.84elapsed
809 * auto, divisor 2: 8.23user 1.05system 0:13.30elapsed
810 * auto, divisor 3: 8.55user 1.12system 0:12.88elapsed
811 * auto, divisor 5: 9.95user 1.43system 0:15.72elapsed
814 rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc;
816 maybe_close(rw->cdb_fd);
818 ht_destroy(&rw->logincore);
819 ht_setup(&rw->logincore);
821 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
822 if (rw->cdb_fd < 0) PE("reopen .cdb after compact");
824 rc= cdbinit(ip, rw); if (rc) goto x_rc;
826 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".jrn"), "w");
827 if (!rw->logfile) PE("reopen .jrn after compact");
829 r= fsync(fileno(rw->logfile)); if (r) PE("fsync .jrn after compact reopen");
834 /* doom! all updates fail after this (because rw->logfile is 0), and
835 * we may be using a lot more RAM than would be ideal. Program will
836 * have to reopen if it really wants sanity. */
840 int cht_do_cdbwr_compact_force(ClientData cd, Tcl_Interp *ip, void *rw_v) {
841 return compact_keepopen(ip, rw_v, 1);
843 int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) {
844 return compact_keepopen(ip, rw_v, 0);
847 int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) {
852 int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) {
858 /*---------- Updateing ----------*/
860 static int update(Tcl_Interp *ip, Rw *rw, const char *key,
861 const Byte *data, int dlen) {
865 if (strlen(key) >= KEYLEN_MAX)
866 return cht_staticerr(ip, "key too long", "CDB KEYOVERFLOW");
868 if (!rw->logfile) return cht_staticerr
869 (ip, "previous compact failed; cdbwr must be closed and reopened "
870 "before any further updates", "CDB BROKEN");
872 val= htv_prep(dlen); assert(val);
873 memcpy(htv_fillptr(val), data, dlen);
875 r= writerecord(rw->logfile, key, val);
876 if (!r) r= fflush(rw->logfile);
877 if (r) PE("write update to logfile");
879 ht_update(&rw->logincore, key, val);
881 if (!rw->autocompact) return TCL_OK;
882 return compact_keepopen(ip, rw, 0);
889 int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip,
890 void *rw_v, const char *key, Tcl_Obj *value) {
893 data= Tcl_GetStringFromObj(value, &dlen); assert(data);
894 return update(ip, rw_v, key, data, dlen);
897 int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip,
898 void *rw_v, const char *key, HBytes_Value value) {
899 return update(ip, rw_v, key, cht_hb_data(&value), cht_hb_len(&value));
902 int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
904 return update(ip, rw_v, key, 0, 0);
907 /*---------- Lookups ----------*/
909 static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key,
910 const Byte **data_r, int *len_r /* -1 => notfound */) {
912 const HashValue *val;
914 val= ht_lookup(&rw->logincore, key);
916 if (val->len) { *data_r= val->data; *len_r= val->len; return TCL_OK; }
917 else { *data_r= 0; *len_r= -1; return TCL_OK; }
920 return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
923 int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
924 const char *key, Tcl_Obj *def,
929 r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
930 return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
931 cht_cdb_storeanswer_string);
934 int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
935 const char *key, Tcl_Obj *def,
940 r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
941 return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
942 cht_cdb_storeanswer_hb);