Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 #define DB_VERSION 0
18 #define DB_WORDLEN 32
20 struct db {
21 uint8_t *m;
22 off_t len;
23 uint32_t version;
24 uint32_t nwords;
26 uint8_t *idx_start;
27 uint8_t *idx_end;
28 uint8_t *list_start;
29 uint8_t *list_end;
30 uint8_t *docs_start;
31 uint8_t *docs_end;
32 };
34 struct db_stats {
35 size_t nwords;
36 size_t ndocs;
37 const char *longest_word;
38 const char *most_popular;
39 size_t most_popular_ndocs;
40 };
42 struct db_entry {
43 char *name;
44 char *descr;
45 };
47 typedef int (*db_hit_cb)(struct db *, struct db_entry *, void *);
49 struct dictionary;
51 int db_create(FILE *, struct dictionary *, struct db_entry *, size_t);
52 int db_open(struct db *, int);
53 uint32_t *db_word_docs(struct db *, const char *, size_t *);
54 int db_stats(struct db *, struct db_stats *);
55 int db_listall(struct db *, db_hit_cb, void *);
56 int db_doc_by_id(struct db *, int, struct db_entry *);
57 void db_close(struct db *);