commit 486b76fc261b3456edaac3b9a3eb9bb3a88760d5
parent a81b3524bf756fdd5d00976abaa45c13363e7df8
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Mon, 26 Aug 2024 21:58:59 +0000
src/wdb: complete switch to fltp format
Diffstat:
6 files changed, 198 insertions(+), 321 deletions(-)
diff --git a/src/wdb/genindex b/src/wdb/genindex
@@ -1,9 +1,10 @@
#!/bin/rc
rfork e
+wdbdir=$home/lib/wdb
+indexdir=$wdbdir/index
-cd $home/lib/wdb/data
-
-echo generating monodb...;
-wdb/print > $home/lib/wdb/index/all
+echo generating monodb
+echo -n > $indexdir/all
+ls $wdbdir/data|xargs cat >> $indexdir/all
echo Done.
diff --git a/src/wdb/mkfile b/src/wdb/mkfile
@@ -9,13 +9,9 @@ RC=\
ui.inc\
new\
ui
-TARG=search update search-fltp
+TARG=search
-
-LIB=\
- ../util/libutil.a$O\
- ../tablist/libtablist.a$O\
- ../fltp/libfltp.a$O
+LIB=../fltp/libfltp.a$O
</sys/src/cmd/mkmany
diff --git a/src/wdb/new b/src/wdb/new
@@ -6,11 +6,15 @@ id=`{uuid}
file=$home/lib/wdb/data/$id
cat > $file <<EOF
-name
- \unnamed
-tags \untagged
-type \untyped
-status \none
+{ $uuid
+{ name
+.n unnamed
+}
+{ tags untagged }
+{ type untyped }
+{ status .n none
+}
+}
EOF
echo $id
diff --git a/src/wdb/search-fltp.c b/src/wdb/search-fltp.c
@@ -1,214 +0,0 @@
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include <regexp.h>
-
-Biobuf *bout;
-
-#include "../fltp/fltp.h"
-
-#define AFTER(X) ( (char *)X + sizeof(*X) )
-
-enum{
- MaxTags = 256,
- BufSize = 4096,
- ListSize = 256,
-};
-
-int tagsqn, typeqn;
-char *buf, *tagsquery[MaxTags + 1], *typequery[MaxTags + 1], *tagslist[MaxTags];
-FLTNode *uuid, *name, *tags, *type;
-Reprog *re;
-
-FLTP *wdb;
-
-void
-node2buf(FLTNode *p)
-{
- snprint(buf, BufSize, "%.*s", (int)p->nbytes, AFTER(p));
-}
-
-int
-_testtag(char **tags, int n, char *test){
- char **p;
- for(p = tags; p < tags + n; p++){
- if (strcmp(test, *p) == 0) return 1;
- }
- return 0;
-}
-
-int
-_testtags(FLTNode *p){
- int n, i, t;
- node2buf(p);
- t = tokenize(buf, tagslist, MaxTags);
- n = 0;
- for(i = 0; i < t; i++){
- if (_testtag(tagsquery, tagsqn, tagslist[i]) != 0) n++;
- }
- return n;
-}
-
-int
-_testtype(FLTNode *p){
- int n, i, t;
- node2buf(p);
- t = tokenize(buf, tagslist, MaxTags);
- n = 0;
- for(i = 0; i < t; i++){
- if (_testtag(typequery, typeqn, tagslist[i]) != 0) n++;
- }
- return n;
-}
-
-int
-_iselem(FLTNode *p){
- fprint(2, "elem %.*s\n", (int)p->nbytes, AFTER(p));
- return 0;
-}
-
-int
-_islist(FLTNode *p){
- fprint(2, "list %.*s\n", (int)p->nbytes, AFTER(p));
- return 0;
-}
-
-int
-_testnameelem(FLTNode *p)
-{
- node2buf(p);
- return regexec(re, buf, nil, 0);
-}
-
-int
-_testnamelist(FLTNode *)
-{
- node2buf(uuid);
- fprint(2, "sublist in name field: %s\n", buf);
- return 0;
-}
-
-int
-testlist(FLTNode *lp, int (*elemfp)(FLTNode *), int (*listfp)(FLTNode *)){
- FLTNode *p, *root;
- int r;
- r = 0;
- root = lp->parent;
- for(p = lp->prev; p->prev != root; p = p->prev){
- if(p->parent != root) continue;
- if(p->next->parent == p) r += (listfp == nil) ? 0 : listfp(p);
- else r += (elemfp == nil) ? 0 : elemfp(p);
- }
- return r;
-}
-
-int
-testname(FLTP *){
- if (re == nil) return 1;
- if(name == nil) sysfatal("name == nil");
- return testlist(name, _testnameelem, _testnamelist);
-}
-
-int
-testtags(FLTP *){
- int n;
- if (tagsquery[0] == nil) return 1;
- if(tags == nil) sysfatal("tags == nil");
- n = testlist(tags, _testtags, nil);
- return n >= tagsqn;
-}
-
-int
-testtype(FLTP *){
- if (typequery[0] == nil) return 1;
- if(type == nil) sysfatal("type == nil");
- return testlist(type, _testtype, nil);
-}
-
-void
-runtests(FLTP *fltp){
- if (testname(fltp) == 0) return;
- if (testtags(fltp) == 0) return;
- if (testtype(fltp) == 0) return;
- print("%.*s\n", (int)(uuid->parent->next->nbytes), AFTER(uuid->parent->next));
-}
-
-void
-fltpnewendlist(FLTP *fltp){
- char *s;
- FLTNode *ls;
- fltpendlist(fltp);
- ls = fltp->np->parent->next;
- s = (char *)(ls) + sizeof(FLTNode);
- snprint(buf, BufSize, "%.*s", (int)ls->nbytes, s);
- if(ls->prev->parent == nil){
- uuid = fltp->np;
- runtests(fltp);
- name = nil;
- tags = nil;
- type = nil;
- return;
- } else if(strcmp(buf, "name") == 0) name = fltp->np;
- else if(strcmp(buf, "tags") == 0) tags = fltp->np;
- else if(strcmp(buf, "type") == 0)type = fltp->np;
-}
-
-void
-fltpnewgetline(FLTP *fltp){
- FLTNode *old, *new;
- old = fltp->np;
- fltpgetline(fltp);
- new = fltp->np;
-
- new->prev = old->prev;
- new->parent = old->parent;
- if(old->prev != nil) old->prev->next = new;
-
- free(old);
-}
-
-void
-loadindex(void){
- Biobuf *bfd;
- char *bp, *path = "/usr/rpa/lib/wdb/index/fltp";
- wdb = initfltp();
- FLTWord nw[] = {
- { &nw[1], "}", fltpnewendlist },
- { wdb->wp, ".n", fltpnewgetline },
- };
- wdb->wp = nw;
- bfd = Bopen(path, OREAD);
- while((bp = Brdline(bfd, '\n')) != nil){
- evalfltp(wdb, bp, Blinelen(bfd));
- }
- Bterm(bfd);
-}
-
-void
-usage(void){
- fprint(2, "usage: %s [-t tags] [-T type] [query]\n", argv0);
- exits("usage");
-}
-
-void
-main(int argc, char *argv[]){
- char *t;
- ARGBEGIN{
- case 't':
- t = EARGF(usage());
- tagsqn = tokenize(t, tagsquery, MaxTags);
- break;
- case 'T':
- t = EARGF(usage());
- typeqn = tokenize(t, typequery, MaxTags);
- break;
- default:
- usage();
- } ARGEND
- if(argc > 1) usage();
- if(argc == 1) re = regcomp(argv[0]);
- else re = nil;
- buf = malloc(BufSize);
- bout = Bfdopen(1, OWRITE);
- loadindex();
-}
diff --git a/src/wdb/search.c b/src/wdb/search.c
@@ -5,123 +5,210 @@
Biobuf *bout;
-#include "../util/util.h"
-#include "../tablist/tablist.h"
+#include "../fltp/fltp.h"
-enum { MaxTags = 256, Growth = 16, BSize = 8192, };
+#define AFTER(X) ( (char *)X + sizeof(*X) )
-char *tags[MaxTags], *types[MaxTags];
+enum{
+ MaxTags = 256,
+ BufSize = 4096,
+ ListSize = 256,
+};
-Slice *wdb;
+int tagsqn, typeqn;
+char *buf, *tagsquery[MaxTags + 1], *typequery[MaxTags + 1], *tagslist[MaxTags];
+FLTNode *uuid, *name, *tags, *type;
+Reprog *re;
-int
-_testtag(char **tags, char *test)
+FLTP *wdb;
+
+void
+node2buf(FLTNode *p)
{
- for (; *tags != nil; tags++) {
- if (strcmp(test, *tags) == 0) return 1;
+ snprint(buf, BufSize, "%.*s", (int)p->nbytes, AFTER(p));
+}
+
+int
+_testtag(char **tags, int n, char *test){
+ char **p;
+ for(p = tags; p < tags + n; p++){
+ if (strcmp(test, *p) == 0) return 1;
}
return 0;
}
int
-_testtags(char **tags, char **test)
-{
- for (; *test != nil; test++) {
- if (_testtag(tags, *test) == 0) return 0;
+_testtags(FLTNode *p){
+ int n, i, t;
+ node2buf(p);
+ t = tokenize(buf, tagslist, MaxTags);
+ n = 0;
+ for(i = 0; i < t; i++){
+ if (_testtag(tagsquery, tagsqn, tagslist[i]) != 0) n++;
}
- return 1;
+ return n;
}
-void
-loadindex(void)
-{
- int n, fd;
- char buf[BSize], *path = "/usr/rpa/lib/wdb/index/all";
- fd = open(path, OREAD);
- TLdecoder *tld = initTLdecoder(path);
- while ((n = read(fd, buf, BSize)) > 0) {
- TLdecode(tld, buf, n);
+int
+_testtype(FLTNode *p){
+ int n, i, t;
+ node2buf(p);
+ t = tokenize(buf, tagslist, MaxTags);
+ n = 0;
+ for(i = 0; i < t; i++){
+ if (_testtag(typequery, typeqn, tagslist[i]) != 0) n++;
}
- wdb = tld->nodes;
- close(fd);
+ return n;
+}
+
+int
+_iselem(FLTNode *p){
+ fprint(2, "elem %.*s\n", (int)p->nbytes, AFTER(p));
+ return 0;
}
int
-testname(TLnode *n, Reprog *re)
+_islist(FLTNode *p){
+ fprint(2, "list %.*s\n", (int)p->nbytes, AFTER(p));
+ return 0;
+}
+
+int
+_testnameelem(FLTNode *p)
{
- if (re == nil) return 1;
- TLnode *name = TLgetnode(n->children, "name", nil);
- char *str = TLval2str(name);
- if (str == nil) sysfatal("testname: can't get name");
- int r = regexec(re, str, nil, 0);
- free(str);
- return r;
+ node2buf(p);
+ return regexec(re, buf, nil, 0);
}
int
-testtags(TLnode *n, char *field, char **tags)
+_testnamelist(FLTNode *)
{
- int i;
- char uuid[64], *str;
- if (tags[0] == nil) return 1;
- i = snprint(uuid, 64, "%.*s\n", n->name->len, n->name->p);
- uuid[i-1] = 0;
- TLnode *nfield = TLgetnode(n->children, field, nil);
- if (nfield == nil) {
- fprint(2, "can't get node %sā%s\n", uuid, field);
- return 0;
- }
- str = TLval2str(nfield);
- if (str == nil) {
- fprint(2, "can't get value %sā%s\n", uuid, field);
- return 0;
+ node2buf(uuid);
+ fprint(2, "sublist in name field: %s\n", buf);
+ return 0;
+}
+
+int
+testlist(FLTNode *lp, int (*elemfp)(FLTNode *), int (*listfp)(FLTNode *)){
+ FLTNode *p, *root;
+ int r;
+ r = 0;
+ root = lp->parent;
+ for(p = lp->prev; p->prev != root; p = p->prev){
+ if(p->parent != root) continue;
+ if(p->next->parent == p) r += (listfp == nil) ? 0 : listfp(p);
+ else r += (elemfp == nil) ? 0 : elemfp(p);
}
- char *test[MaxTags];
- int N = tokenize(str, test, MaxTags);
- test[N] = nil;
- int r = _testtags(test, tags);
- free(str);
return r;
}
+int
+testname(FLTP *){
+ if (re == nil) return 1;
+ if(name == nil) sysfatal("name == nil");
+ return testlist(name, _testnameelem, _testnamelist);
+}
+
+int
+testtags(FLTP *){
+ int n;
+ if (tagsquery[0] == nil) return 1;
+ if(tags == nil) sysfatal("tags == nil");
+ n = testlist(tags, _testtags, nil);
+ return n >= tagsqn;
+}
+
+int
+testtype(FLTP *){
+ if (typequery[0] == nil) return 1;
+ if(type == nil) sysfatal("type == nil");
+ return testlist(type, _testtype, nil);
+}
+
void
-usage(void)
-{
- fprint(2, "usage: %s [-t tags] [-T type] [query]\n", argv0);
+runtests(FLTP *fltp){
+ if (testname(fltp) == 0) return;
+ if (testtags(fltp) == 0) return;
+ if (testtype(fltp) == 0) return;
+ print("%.*s\n", (int)(uuid->parent->next->nbytes), AFTER(uuid->parent->next));
+}
+
+void
+fltpnewendlist(FLTP *fltp){
+ char *s;
+ FLTNode *ls;
+ fltpendlist(fltp);
+ ls = fltp->np->parent->next;
+ s = (char *)(ls) + sizeof(FLTNode);
+ snprint(buf, BufSize, "%.*s", (int)ls->nbytes, s);
+ if(ls->prev->parent == nil){
+ uuid = fltp->np;
+ runtests(fltp);
+ name = nil;
+ tags = nil;
+ type = nil;
+ return;
+ } else if(strcmp(buf, "name") == 0) name = fltp->np;
+ else if(strcmp(buf, "tags") == 0) tags = fltp->np;
+ else if(strcmp(buf, "type") == 0)type = fltp->np;
+}
+
+void
+fltpnewgetline(FLTP *fltp){
+ FLTNode *old, *new;
+ old = fltp->np;
+ fltpgetline(fltp);
+ new = fltp->np;
+
+ new->prev = old->prev;
+ new->parent = old->parent;
+ if(old->prev != nil) old->prev->next = new;
+
+ free(old);
+}
+
+void
+loadindex(void){
+ Biobuf *bfd;
+ char *bp;
+ wdb = initfltp();
+ FLTWord nw[] = {
+ { &nw[1], "}", fltpnewendlist },
+ { wdb->wp, ".n", fltpnewgetline },
+ };
+ wdb->wp = nw;
+ bfd = Bfdopen(0, OREAD);
+ while((bp = Brdline(bfd, '\n')) != nil){
+ evalfltp(wdb, bp, Blinelen(bfd));
+ }
+ Bterm(bfd);
+}
+
+void
+usage(void){
+ fprint(2, "usage: %s [-t tags] [-T type] [name query]\n", argv0);
exits("usage");
}
void
-main(int argc, char *argv[])
-{
- long i;
- Reprog *re;
+main(int argc, char *argv[]){
char *t;
- ARGBEGIN {
+ ARGBEGIN{
case 't':
t = EARGF(usage());
- tokenize(t, tags, MaxTags);
+ tagsqn = tokenize(t, tagsquery, MaxTags);
break;
case 'T':
t = EARGF(usage());
- tokenize(t, types, MaxTags);
+ typeqn = tokenize(t, typequery, MaxTags);
break;
default:
usage();
} ARGEND
- if (argc > 1) usage();
- if (argc == 1) re = regcomp(argv[0]);
+ if(argc > 1) usage();
+ if(argc == 1) re = regcomp(argv[0]);
else re = nil;
-
+ buf = malloc(BufSize);
bout = Bfdopen(1, OWRITE);
loadindex();
-
- for (i = 0; i < wdb->len; i++) {
- TLnode **p = slicegetp(wdb, i);
- TLnode *n = *p;
- if (testname(n, re) == 0) continue;
- if (testtags(n, "tags", tags) == 0) continue;
- if (testtags(n, "type", types) == 0) continue;
- Bprint(bout, "%.*s\n", n->name->len, n->name->p);
- }
}
diff --git a/src/wdb/ui.inc b/src/wdb/ui.inc
@@ -2,61 +2,64 @@ wdb=$home/lib/wdb
data=$wdb/data
index=$wdb/index
-fn main_help {
- echo \
-'h - help
+fn main_help{
+ cat<EOF
+h - help
i - regenerate index
s [query] - search
t [query] - search by tag
T [query] - search by type
n - new entry
-q - quit'
+q - quit
+EOF
}
-fn printguid {
+fn printguid{
id=$1
echo $data/$"id
+ grep -n $"id $index/all /dev/null
cat $data/$"id
echo '---'
}
-fn printguids {
- test -z $"guids || for (i in `{seq $#guids}) {
+fn printguids{
+ test -z $"guids||
+ for(i in `{seq $#guids}){
echo -n $i^': '
printguid $guids($i)
}
}
-fn entry {
+fn entry{
id=$1
echo $data/$id
cat $data/$id
}
-fn enumer {
- echo Found $#1 entries.
+fn enumer{
+ echo Found $#* entries.
}
-fn main {
+fn main{
echo -n 'ā '
input=`{read}
cmd=$input(1)
arg=$input(2-)
- switch ($"cmd) {
+ switch($"cmd){
case h
main_help
case i
wdb/genindex
case s
- guids=`{wdb/search $"arg}
+ guids=`{wdb/search $"arg < $index/all}
enumer $guids
printguids
case t
- guids=`{wdb/search -t $"arg}
+ guids=`{wdb/search -t $"arg < $index/all}
enumer $guids
printguids
case T
- guids=`{wdb/search -T $"arg}
+ guids=`{wdb/search -T $"arg < $index/all}
enumer $guids
printguids
case q
@@ -66,13 +69,13 @@ fn main {
printguids
case n
guids=`{wdb/new $"arg}
- if (test -z $status) {
+ if(test -z $status){
echo New entry: $data/$"guids
plumb -s 'wdb' $data/$"guids
}
case *
id=$guids($"cmd)
- if ( test -n $"id -a -f $data/^$"id ) {
+ if(test -n $"id -a -f $data/^$"id){
entry $"id
}
if not echo '?'