chiark / gitweb /
writeable, update entrypoints impl'd
authorian <ian>
Sun, 5 Feb 2006 00:56:59 +0000 (00:56 +0000)
committerian <ian>
Sun, 5 Feb 2006 00:56:59 +0000 (00:56 +0000)
cdb/writeable.c

index 3c718d83fd813f63e26645839cfba0f02e0bf1a5..3205ff0612f870e120ea819fa3d4b48c29d7ea63 100644 (file)
@@ -68,11 +68,6 @@ static HashValue *htv_prep(int len) {
 static Byte *htv_fillptr(HashValue *hd) {
   return hd->data;
 }
-#if 0
-static void htv_fill(HashValue *hd, const Byte *data) {
-  memcpy(hd->data, data, hd->len);
-}
-#endif
 
 static void ht_setup(HashTable *ht) {
   Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS);
@@ -212,7 +207,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;
@@ -289,6 +284,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,
@@ -524,17 +531,8 @@ 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 ----------*/
@@ -732,7 +730,7 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
 
 /*---------- Other compaction-related entrypoints ----------*/
 
-static int compact_keeopen(Tcl_Interp *ip, Rw *rw, int force) {
+static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
   off_t logsz;
   long reccount;
   int rc, r;
@@ -764,16 +762,16 @@ static int compact_keeopen(Tcl_Interp *ip, Rw *rw, int force) {
 
 x_rc:
   /* doom! all updates fail after this (because rw->logfile is 0), and
-   * we may be usin a lot more RAM than would be ideal.  Program will
+   * 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_keeopen(ip, rw_v, 1);
+  return compact_keepopen(ip, rw_v, 1);
 }
 int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) {
-  return compact_keeopen(ip, rw_v, 0);
+  return compact_keepopen(ip, rw_v, 0);
 }
 
 int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) {
@@ -786,3 +784,42 @@ int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *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));
+}