commit dfd928c15a15bb7a655cbb23a3ce2bbaff7ac2a3
parent d3cb1b256b354de09b0d26a233a1574166de9c7e
Author: rpa <rpa@grass>
Date: Mon, 20 Nov 2023 23:09:54 +0000
src/wdb: search global index, also housekeeping
Diffstat:
4 files changed, 77 insertions(+), 67 deletions(-)
diff --git a/src/wdb/mkfile b/src/wdb/mkfile
@@ -1,9 +1,28 @@
</$objtype/mkfile
+HFILES=wdb.h
+OFILES=
+BIN=$home/bin/$cputype/wdb
+RC=\
+ genindex\
+ print\
+ tags\
+ typesearch\
+ ui.inc\
+ new\
+ tagsearch\
+ ui
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
+
+install:V: installdir installrc
+
+installdir:V:
+ mkdir -p $BIN
+
+installrc:V: installdir
+ cp $RC $BIN
diff --git a/src/wdb/print b/src/wdb/print
@@ -0,0 +1,10 @@
+#!/bin/rc
+
+rfork e
+
+lib=$home/lib/wdb/data
+
+for (f in $lib/*) {
+ echo `{basename $f}
+ awk '{printf("\t%s\n", $0);}' < $f
+}
diff --git a/src/wdb/search.c b/src/wdb/search.c
@@ -5,16 +5,19 @@
Biobuf *bout;
+#include "../util/util.h"
+#include "../tablist/tablist.h"
+
#include "wdb.h"
-#include "util.h"
-enum { MaxTags = 256, };
+enum { MaxTags = 256, Growth = 16, BSize = 8192, };
-WDB db;
char *tags[MaxTags];
+Slice *wdb;
+
int
-testtag(char **tags, char *test)
+_testtag(char **tags, char *test)
{
for (; *tags != nil; tags++) {
if (strcmp(test, *tags) == 0) return 1;
@@ -23,16 +26,16 @@ testtag(char **tags, char *test)
}
int
-testtags(char **tags, char **test)
+_testtags(char **tags, char **test)
{
for (; *test != nil; test++) {
- if (testtag(tags, *test) == 0) return 0;
+ if (_testtag(tags, *test) == 0) return 0;
}
return 1;
}
int
-testtagsstr(char **tags, char *str)
+_testtagsstr(char **tags, char *str)
{
char *test[MaxTags];
char *s;
@@ -42,70 +45,48 @@ testtagsstr(char **tags, char *str)
s = strdup(str);
n = tokenize(s, test, MaxTags);
test[n] = nil;
- r = testtags(test, tags);
+ r = _testtags(test, tags);
free(s);
return r;
}
-long
-wdbgrow(WDB *db)
+void
+loadindex(void)
{
- int i;
- long j;
- for (i = 0; i < WDBMax; i++) {
- db->pp[i] = realloc(db->pp[i], sizeof(char *) * (db->n + 8));
- for (j = db->n; j < db->n + 8; j++) db->pp[i][j] = nil;
+ 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);
}
- db->n += 8;
- return db->n - 8;
+ wdb = tld->nodes;
+ close(fd);
}
-long
-wdbnewentry(WDB *db)
+int
+testname(TLnode *n, Reprog *re)
{
- long i;
- for (i = 0; i < db->n; i++) if (db->pp[0][i] == 0) return i;
- return wdbgrow(db);
+ 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;
}
-void
-_loadindex(char *fname, int f)
+int
+testtags(TLnode *n)
{
- char *path = smprint("/usr/rpa/lib/wdb/index/%s", fname);
- Biobuf *b = Bopen(path, OREAD);
- free(path);
- if (b == nil) sysfatal("%r");
- char *s;
- while((s = Brdstr(b, '\n', 1)) != nil) {
- long n;
- char *fields[2];
- getfields(s, fields, 2, 1, "\t");
- char **e = strfind(db.pp[WDBId], db.n, fields[0]);
- if (e == nil) {
- n = wdbnewentry(&db);
- db.pp[WDBId][n] = strdup(fields[0]);
- } else {
- n = e - db.pp[WDBId];
- }
- if (db.pp[f][n] == nil) db.pp[f][n] = strdup(fields[1]);
- else {
- char *ss = smprint("%s\n%s", db.pp[f][n], fields[1]);
- free(db.pp[f][n]);
- db.pp[f][n] = ss;
- }
- free(s);
- }
- Bterm(b);
- Bflush(bout);
+ // TODO
+ return 1;
}
-void
-loadindex(void)
+int
+testtype(TLnode *n)
{
- _loadindex("name", WDBName);
- _loadindex("tags", WDBTags);
- _loadindex("type", WDBType);
-
+ // TODO
+ return 1;
}
void
@@ -135,13 +116,13 @@ main(int argc, char *argv[])
bout = Bfdopen(1, OWRITE);
loadindex();
long i;
- for (i = 0; i < db.n; i++) {
- char *name = db.pp[WDBName][i];
- if (name == nil) break;
- if (
- (regexec(re, name, nil, 0) != 0) &&
- (testtagsstr(tags, db.pp[WDBTags][i]) != 0)) {
- Bprint(bout, "%s\n", db.pp[WDBId][i]);
- }
+
+ for (i = 0; i < wdb->len; i++) {
+ TLnode **p = slicegetp(wdb, i);
+ TLnode *n = *p;
+ if (testname(n, re) == 0) continue;
+ if (testtags(n) == 0) continue;
+ if (testtype(n) == 0) continue;
+ Bprint(bout, "%.*s\n", n->name->len, n->name->p);
}
}
diff --git a/src/wdb/ui b/src/wdb/ui
@@ -11,6 +11,6 @@ cat <<EOF
EOF
while () {
- . $home/bin/rc/wdb/ui.inc
+ . wdb/ui.inc
main
}