squat-test.c revision 97e62b2b36dda0acb3215667042f5c80cdee8155
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* Copyright (c) 2006-2011 Dovecot authors, see the included COPYING file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void result_print(ARRAY_TYPE(seq_range) *result)
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek unsigned int i, count;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; i < count; i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherint main(int argc ATTR_UNUSED, char *argv[])
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *trie_path = "/tmp/squat-test-index.search";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *uidlist_path = "/tmp/squat-test-index.search.uids";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct squat_trie_build_context *build_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ARRAY_TYPE(seq_range) definite_uids, maybe_uids;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsigned int len, last = 0, seq = 1, node_count, uidlist_count;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bool data_header = TRUE, first = TRUE, skip_body = FALSE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher trie = squat_trie_init(trie_path, time(NULL),
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher FILE_LOCK_METHOD_FCNTL, FALSE, 0600, (gid_t)-1);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (squat_trie_build_init(trie, &build_ctx) < 0)
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek valid = buffer_create_dynamic(default_pool, 4096);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek input = i_stream_create_fd(fd, (size_t)-1, FALSE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (ret == 0 && (line = i_stream_read_next_line(input)) != NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, "\r%ukB", (unsigned)(input->v_offset/1024));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strncasecmp(line, "Content-Type:", 13) == 0 &&
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher strncasecmp(line, "Content-Type: text/", 19) != 0 &&
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher strncasecmp(line, "Content-Type: message/", 22) != 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (strncasecmp(line, "Content-Transfer-Encoding: base64", 33) == 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* we're actually indexing here headers as bodies and bodies
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher as headers. it doesn't really matter in this test, and
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fixing it would require storing headers temporarily
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher elsewhere and index them only after the body */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher index_type = !data_header ? SQUAT_INDEX_TYPE_HEADER :
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (uni_utf8_get_valid_data((const unsigned char *)line,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = squat_trie_build_more(build_ctx, seq, index_type,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = squat_trie_build_more(build_ctx, seq, index_type,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (squat_trie_build_deinit(&build_ctx, NULL) < 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cputime = (double)(clock_end - clock_start) / CLOCKS_PER_SEC;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, "\n - Index time: %.2f CPU seconds, "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "%.2f real seconds (%.02fMB/CPUs)\n", cputime,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher timeval_diff_msecs(&tv_end, &tv_start)/1000.0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher i_error("stat(%s) failed: %m", trie_path);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher i_error("stat(%s) failed: %m", uidlist_path);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher trie_mem = squat_trie_mem_used(trie, &node_count);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek uidlist_mem = squat_uidlist_mem_used(squat_trie_get_uidlist(trie),
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, " - memory: %uk for trie, %uk for uidlist\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (unsigned)(trie_mem/1024), (unsigned)(uidlist_mem/1024));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, " - %"PRIuUOFF_T" bytes in %u nodes (%.02f%%)\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher trie_st.st_size / (float)input->v_offset * 100.0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, " - %"PRIuUOFF_T" bytes in %u UID lists (%.02f%%)\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher uidlist_st.st_size / (float)input->v_offset * 100.0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, " - %"PRIuUOFF_T" bytes total of %"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (trie_st.st_size + uidlist_st.st_size), input->v_offset,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while ((str = fgets(buf, sizeof(buf), stdin)) != NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = squat_trie_lookup(trie, str, SQUAT_INDEX_TYPE_HEADER |