Mercurial > dovecot > core-2.2
changeset 18557:6a6ce51597f7
lib-fts: Delay stopwords filter full initialization until it's needed.
author | Teemu Huovila <teemu.huovila@dovecot.fi> |
---|---|
date | Sat, 09 May 2015 11:15:34 +0300 |
parents | a45a328c5019 |
children | 8d445959df03 |
files | src/lib-fts/fts-filter-stopwords.c src/lib-fts/test-fts-filter.c |
diffstat | 2 files changed, 24 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib-fts/fts-filter-stopwords.c Sat May 09 11:14:51 2015 +0300 +++ b/src/lib-fts/fts-filter-stopwords.c Sat May 09 11:15:34 2015 +0300 @@ -74,7 +74,8 @@ static void fts_filter_stopwords_destroy(struct fts_filter *filter) { struct fts_filter_stopwords *sp = (struct fts_filter_stopwords *)filter; - hash_table_destroy(&sp->stopwords); + if (hash_table_is_created(sp->stopwords)) + hash_table_destroy(&sp->stopwords); pool_unref(&sp->pool); return; } @@ -87,7 +88,6 @@ { struct fts_filter_stopwords *sp; pool_t pp; - int ret; const char *dir = NULL; unsigned int i; @@ -101,7 +101,6 @@ return -1; } } - pp = pool_alloconly_create(MEMPOOL_GROWING"fts_filter_stopwords", sizeof(struct fts_filter)); sp = p_new(pp, struct fts_filter_stopwords, 1); @@ -109,19 +108,24 @@ sp->pool = pp; sp->lang = p_malloc(sp->pool, sizeof(struct fts_language)); sp->lang->name = str_lcase(p_strdup(sp->pool, lang->name)); - hash_table_create(&sp->stopwords, sp->pool, 0, str_hash, strcmp); if (dir != NULL) sp->stopwords_dir = p_strdup(pp, dir); else sp->stopwords_dir = DATADIR"/stopwords"; *filter_r = &sp->filter; + return 0; +} + +static int +fts_filter_stopwords_create_stopwords(struct fts_filter_stopwords *sp) +{ + int ret; + + hash_table_create(&sp->stopwords, sp->pool, 0, str_hash, strcmp); ret = fts_filter_stopwords_read_list(sp); - if (ret < 0) { - *error_r = t_strdup_printf( - "Failed to read stopword list %s", sp->stopwords_dir); - fts_filter_stopwords_destroy(*filter_r); - *filter_r = NULL; - } + if (ret < 0) + sp->filter.error = t_strdup_printf("Failed to read stopword list %s", + sp->stopwords_dir); return ret; } @@ -132,6 +136,9 @@ struct fts_filter_stopwords *sp = (struct fts_filter_stopwords *) filter; + if (!hash_table_is_created(sp->stopwords)) + if (fts_filter_stopwords_create_stopwords(sp) < 0) + return -1; stopword = hash_table_lookup(sp->stopwords, *token); if (stopword != NULL) { *token = NULL;
--- a/src/lib-fts/test-fts-filter.c Sat May 09 11:14:51 2015 +0300 +++ b/src/lib-fts/test-fts-filter.c Sat May 09 11:15:34 2015 +0300 @@ -160,18 +160,20 @@ test_end(); } -static void test_fts_filter_stopwords_fail_create(void) +static void test_fts_filter_stopwords_fail_lazy_init(void) { const struct fts_filter *filter_class; const struct fts_language unknown = { .name = "bebobidoop" }; struct fts_filter *filter = NULL; - const char *error; + const char *error = NULL, *token = "foobar"; int ret; - test_begin("fts filter stopwords, fail create()"); + test_begin("fts filter stopwords, fail filter() (lazy init)"); filter_class = fts_filter_find(STOPWORDS_FILTER_NAME); ret = fts_filter_create(filter_class, NULL, &unknown, stopword_settings, &filter, &error); - test_assert(ret == -1 && filter == NULL && error != NULL); + test_assert(ret == 0 && filter != NULL && error == NULL); + ret = fts_filter_filter(filter, &token, &error); + test_assert(ret == -1 && error != NULL); test_end(); } @@ -545,7 +547,7 @@ test_fts_filter_stopwords_eng, test_fts_filter_stopwords_fin, test_fts_filter_stopwords_fra, - test_fts_filter_stopwords_fail_create, + test_fts_filter_stopwords_fail_lazy_init, #ifdef HAVE_FTS_STEMMER test_fts_filter_stemmer_snowball_stem_english, test_fts_filter_stemmer_snowball_stem_french,