commit e8fcfc35bdb4da086200c9c08bf77adac4198a65
parent e2762aed174dde8f4022a6f6b756c9f037cf31d0
Author: rpa <rpa@grass>
Date: Sun, 10 Sep 2023 00:04:10 +0000
src/wdb/update: a primitive db update tool
Diffstat:
2 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/src/wdb/mkfile b/src/wdb/mkfile
@@ -1,5 +1,9 @@
</$objtype/mkfile
-TARG=search
+TARG=search update
HFILES=wdb.h util.h
OFILES=util.$O
</sys/src/cmd/mkmany
+
+# LIB=../util/libutil.a$O ../tablist/libtablist.a$O
+
+# $O.update: update.$O $LIB
diff --git a/src/wdb/update.c b/src/wdb/update.c
@@ -0,0 +1,99 @@
+#include <u.h>
+#include <libc.h>
+#include <../util/util.h>
+#include <../tablist/tablist.h>
+enum {
+ BSize = 1024 * 8,
+ HSize = BSize / 2,
+};
+
+int db, bn;
+char *path, buf[BSize];
+TLdecoder *tldec;
+TLencoder *tlenc;
+Slice *query, *nodes;
+
+TLnode **
+find(TLnode *n)
+{
+ TLnode **v, *m;
+ int i;
+ if (n->name == nil) return nil;
+ for (i = 0; i < nodes->len; i++) {
+ v = slicegetp(nodes, i);
+ m = *v;
+ if (m->name == nil) continue;
+ if (memcmp(m->name->p, n->name->p, m->name->len) == 0)
+ return v;
+ }
+ return nil;
+}
+
+void
+update(TLnode *n)
+{
+ TLnode **v;
+ v = find(n);
+ if (v != nil) {
+ *v = n;
+ } else {
+ nodes = sliceappendp(nodes, 1, &n);
+ }
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage %s db_file < node\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+
+ fprint(2, "hello\n");
+
+ int i;
+
+ ARGBEGIN {
+ default:
+ usage();
+ } ARGEND
+
+ if (argc != 1) usage();
+ path = argv[0];
+ db = open(path, OREAD);
+ if (db < 0) sysfatal("can't open db: %r");
+
+ fprint(2, "reading query\n");
+ tldec = initTLdecoder(nil);
+ while((bn = read(0, buf, BSize)) > 0) {
+ TLdecode(tldec, buf, bn);
+ }
+ if (bn < 0) sysfatal("%r");
+ query = tldec->nodes;
+ fprint(2, "qlen %d\n", query->len);
+
+ tldec = initTLdecoder(path);
+ while((bn = read(db, buf, BSize)) > 0) {
+ TLdecode(tldec, buf, bn);
+ }
+ if (bn < 0) sysfatal("%r");
+ nodes = tldec->nodes;
+ close(db);
+
+ for (i = 0; i < query->len; i++) {
+ TLnode **v;
+ v = slicegetp(query, i);
+ update(*v);
+ }
+
+ db = open(path, OWRITE | OTRUNC);
+ if (db < 0) sysfatal("%r");
+ tlenc = initTLencoder(nodes);
+ while((bn = TLencode(tlenc, buf, BSize)) > 0) {
+ bn = write(db, buf, bn);
+ if (bn < 0) sysfatal("%r");
+ }
+}