X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=blobdiff_plain;f=common%2Ftst-trie.c;h=739bd415e7a1e1575d1777e25be89c606648bbc0;hp=cc8c8f35aa8db2af4562bb71849a2cf5172f36b7;hb=ab82546953101224d09dcf2e44ff170e454e7282;hpb=4694ac125f0dd752126de3f3f4e81b406cdd98e6 diff --git a/common/tst-trie.c b/common/tst-trie.c index cc8c8f3..739bd41 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -33,51 +33,28 @@ * Copyright © 2008 Florent Bruneau */ -#include -#include -#include -#include -#include - #include "common.h" #include "str.h" #include "trie.h" +#include "file.h" static trie_t *create_trie_from_file(const char *file) { trie_t *db; - const char *map, *p, *end; - struct stat st; - int fd; + file_map_t map; + const char *p, *end; char line[BUFSIZ]; - fd = open(file, O_RDONLY, 0000); - if (fd < 0) { - UNIXERR("open"); + if (!file_map_open(&map, file, false)) { return NULL; } - - if (fstat(fd, &st) < 0) { - UNIXERR("fstat"); - close(fd); - return NULL; - } - - p = map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (map == MAP_FAILED) { - UNIXERR("mmap"); - close(fd); - return NULL; - } - close(fd); - - end = map + st.st_size; - while (end > map && end[-1] != '\n') { + p = map.map; + end = map.end; + while (end > p && end[-1] != '\n') { --end; } - if (end != map + st.st_size) { - syslog(LOG_WARNING, "file %s miss a final \\n, ignoring last line", - file); + if (end != map.end) { + warn("file %s miss a final \\n, ignoring last line", file); } db = trie_new(); @@ -102,7 +79,7 @@ static trie_t *create_trie_from_file(const char *file) trie_insert(db, line); p = eol + 1; } - munmap((void*)map, st.st_size); + file_map_close(&map); trie_compile(db, false); return db; } @@ -151,8 +128,14 @@ int main(int argc, char *argv[]) */ if (argc > 1) { trie = create_trie_from_file(argv[1]); - trie_lock(trie); trie_inspect(trie, false); + if (argc > 2) { + time_t now = time(NULL); + for (uint32_t i = 0 ; i < 1000000000 ; ++i) { + trie_lookup(trie, argv[2]); + } + printf("%lu lookups per second\n", 1000000000 / (time(NULL) - now)); + } trie_delete(&trie); } return 0;