chiark / gitweb /
update wip; see cvs diff from 1.12 before checkin
[chiark-tcl.git] / cdb / writeable.c
index 4230e62d403e1f788a640bfe8caacd2605535c84..3613a7b8084642d31c807fde215590f20caf00b1 100644 (file)
@@ -63,13 +63,11 @@ static HashValue *htv_prep(int len) {
   HashValue *hd;
   hd= TALLOC((hd->data - (Byte*)hd) + len);
   hd->len= len;
+  return hd;
 }  
 static Byte *htv_fillptr(HashValue *hd) {
   return hd->data;
 }
-static void htv_fill(HashValue *hd, const Byte *data) {
-  memcpy(hd->data, data, hd->len);
-}
 
 static void ht_setup(HashTable *ht) {
   Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS);
@@ -94,6 +92,17 @@ static void ht_maybeupdate(HashTable *ht, const char *key,
   Tcl_SetHashValue(he, val_eat);
 }
 
+static fixme ht_lookup(HashTable *ht, const char *key) {
+  Tcl_HashEntry *he;
+  const HashValue *htv;
+  
+  he= Tcl_FindHashEntry(ht, key);
+  if (!he) return notfound;
+  
+  htv= Tcl_GetHashValue(he);
+  
+}
+
 static int ht_forall(HashTable *ht,
                     int (*fn)(const char *key, HashValue *val,
                               struct ht_forall_ctx *ctx),
@@ -140,7 +149,6 @@ typedef struct Rw {
   int ix, autocompact;
   int cdb_fd, lock_fd;
   struct cdb cdb; /* valid iff cdb_fd >= 0 */
-  off_t cdb_bytes; /* valid iff cdb_fd >= 0 */
   FILE *logfile;
   HashTable logincore;
   Pathbuf pbsome, pbother;
@@ -153,6 +161,7 @@ static int rw_close(Tcl_Interp *ip, Rw *rw) {
 
   rc= TCL_OK;
   ht_destroy(&rw->logincore);
+  if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
   maybe_close(rw->cdb_fd);
   maybe_close(rw->lock_fd);
 
@@ -209,7 +218,7 @@ static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) {
   return TCL_OK;
 }
 
-/*---------- Log reading ----------*/
+/*---------- Log reading and writing ----------*/
 
 static int readlognum(FILE *f, int delim, int *num_r) {
   int c;
@@ -286,6 +295,18 @@ static int readstorelogrecord(FILE *f, HashTable *ht,
   return -2;
 }
 
+static int writerecord(FILE *f, const char *key, const HashValue *val) {
+  int r;
+
+  r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key);
+  if (r<0) return -1;
+  
+  r= fwrite(val->data, 1, val->len, f);
+  if (r != val->len) return -1;
+
+  return 0;
+}
+
 /*---------- Creating ----------*/
 
 int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip,
@@ -358,6 +379,19 @@ static int infocb(Tcl_Interp *ip, Rw *rw, const char *arg1,
   
 /*---------- Opening ----------*/
 
+static int cdbinit(Tcl_Interp *ip, Rw *rw) {
+  /* On entry, cdb_fd >=0 but cdb is _undefined_/
+   * On exit, either cdb_fd<0 or cdb is initialised */
+  int r, rc;
+  
+  r= cdb_init(&rw->cdb, rw->cdb_fd);
+  if (r) {
+    rc= cht_posixerr(ip, errno, "failed to initialise cdb reader");
+    close(rw->cdb_fd);  rw->cdb_fd= -1;  return rc;
+  }
+  return TCL_OK;
+}
+
 int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
                      Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
                      void **result) {
@@ -392,11 +426,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
 
   rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
   if (rw->cdb_fd >=0) {
-    r= cdb_init(&rw->cdb, rw->cdb_fd);
-    if (r) {
-      rc= cht_posixerr(ip, errno, "failed to initialise cdb reader");
-      close(rw->cdb_fd);  rw->cdb_fd= -1;  goto x_rc;
-    }
+    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 -"
@@ -474,7 +504,13 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
   return rc;
 }
 
-/*==================== COMPACTING ====================*/
+int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb,
+                     Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
+                     void **result) {
+  return cht_do_cdbwr_open(cd,ip,pathb,on_info,on_lexminval,result);
+}
+
+/*==================== COMPACTION ====================*/
 
 struct ht_forall_ctx {
   struct cdb_make cdbm;
@@ -512,22 +548,13 @@ static int addto_cdb(const char *key, HashValue *val,
 
 static int addto_main(const char *key, HashValue *val,
                      struct ht_forall_ctx *a) {
-  int r;
-
   (*a->reccount)++;
-  
-  r= fprintf(a->mainfile, "+%d,%d:%s->", strlen(key), val->len, key);
-  if (r<0) return -1;
-  
-  r= fwrite(val->data, 1, val->len, a->mainfile);
-  if (r != val->len) return -1;
-
-  return 0;
+  return writerecord(a->mainfile, key, val);
 }
 
 /*---------- compact main entrypoint ----------*/
 
-static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsize,
+static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
                        long *reccount_r) {
   /* creates new .cdb and .main
    * closes logfile
@@ -554,7 +581,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsize,
   rw->logfile= 0;
   
   rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
-            logsize, (unsigned long)rw->mainsz);
+            logsz, (unsigned long)rw->mainsz);
   if (rc) goto x_rc;
 
   if (rw->on_lexminval.llength) {
@@ -655,16 +682,18 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsize,
 
   /* done! */
   
-  rc= infocb(ip, rw, "compact-end", "log=%luby main=%luby nrecs=%l",
-            logsize, (unsigned long)rw->mainsz, *a.reccount);
+  rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%l",
+            (unsigned long)rw->mainsz, *a.reccount);
   if (rc) goto x_rc;
 
-  rc= TCL_OK;
+  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 ----------*/
@@ -716,4 +745,115 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
   return rc;
 }
 
-/*==================== Other entrypoints ====================*/
+/*---------- 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(Tcl_Interp *ip, Rw *rw, const char *key, ) {
+  ht_lookup(
+
+int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *db,
+                       const char *key, Tcl_Obj *def, Tcl_Obj **result) {
+  
+}
+
+int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *db, const char *key, HBytes_Value def, HBytes_Value *result);