Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stdio.h>
18 #include <stdlib.h>
20 #include "db.h"
21 #include "fts.h"
22 #include "tokenize.h"
24 struct doclist {
25 uint32_t *ids;
26 size_t len;
27 };
29 int
30 fts(struct db *db, const char *query, db_hit_cb cb, void *data)
31 {
32 struct doclist *xs = NULL;
33 size_t i, len;
34 char **toks, **t;
35 int ret = 0;
37 if ((toks = tokenize(query)) == NULL)
38 return -1;
40 len = 0;
41 for (t = toks; *t != NULL; ++t)
42 len++;
44 if (len == 0)
45 goto done;
47 if ((xs = calloc(len, sizeof(*xs))) == NULL) {
48 freetoks(toks);
49 return -1;
50 }
52 for (i = 0; i < len; ++i) {
53 xs[i].ids = db_word_docs(db, toks[i], &xs[i].len);
54 if (xs[i].ids == NULL || xs[i].len == 0)
55 goto done;
56 }
58 for (;;) {
59 struct db_entry e;
60 uint32_t mdoc;
62 mdoc = xs[0].ids[0];
63 for (i = 1; i < len; ++i) {
64 if (xs[i].ids[0] > mdoc)
65 goto next;
66 while (xs[i].ids[0] < mdoc) {
67 if (--xs[i].len == 0)
68 goto done;
69 xs[i].ids++;
70 }
72 if (xs[i].ids[0] != mdoc)
73 goto next;
74 }
76 if (db_doc_by_id(db, mdoc, &e) == -1) {
77 ret = -1;
78 goto done;
79 }
81 if (cb(db, &e, data) == -1) {
82 ret = -1;
83 goto done;
84 }
86 next:
87 if (--xs[0].len == 0)
88 goto done;
89 xs[0].ids++;
90 }
92 done:
93 free(xs);
94 freetoks(toks);
96 return ret;
97 }