From 87ed7d9a0f01fb49017476153b83792606432e35 Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 16 Sep 2015 22:11:10 -0400 Subject: [PATCH] [geodb] Adding trie search methods for finding geodb phrases --- src/geodb.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- src/geodb.h | 11 +++++++---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/geodb.c b/src/geodb.c index 66263c4f..32a522c5 100644 --- a/src/geodb.c +++ b/src/geodb.c @@ -61,9 +61,7 @@ geodb_t *geodb_init(char *dir) { char_array_clear(path); - char_array_cat(path, dir); - char_array_cat(path, PATH_SEPARATOR); - char_array_cat(path, GEODB_BLOOM_FILTER_FILENAME); + char_array_cat_joined(path, PATH_SEPARATOR, PATH_SEPARATOR_LEN, true, 2, dir, GEODB_BLOOM_FILTER_FILENAME); char *bloom_path = char_array_get_string(path); @@ -74,17 +72,13 @@ geodb_t *geodb_init(char *dir) { char_array_clear(path); - char_array_cat(path, dir); - char_array_cat(path, PATH_SEPARATOR); - char_array_cat(path, GEODB_HASH_FILENAME); + char_array_cat_joined(path, PATH_SEPARATOR, PATH_SEPARATOR_LEN, true, 2, dir, GEODB_HASH_FILENAME); char *hash_file_path = strdup(char_array_get_string(path)); char_array_clear(path); - char_array_cat(path, dir); - char_array_cat(path, PATH_SEPARATOR); - char_array_cat(path, GEODB_LOG_FILENAME); + char_array_cat_joined(path, PATH_SEPARATOR, PATH_SEPARATOR_LEN, true, 2, dir, GEODB_LOG_FILENAME); char *log_path = char_array_get_string(path); @@ -133,6 +127,41 @@ bool geodb_load(char *dir) { } +bool search_geodb_with_phrases(char *str, phrase_array **phrases) { + if (str == NULL) return false; + + return trie_search_with_phrases(address_dict->trie, str, phrases); +} + +phrase_array *search_geodb(char *str) { + phrase_array *phrases = NULL; + + if (!search_geodb_with_phrases(str, &phrases)) { + return NULL; + } + + return phrases; +} + + +bool search_geodb_tokens_with_phrases(char *str, token_array *tokens, phrase_array **phrases) { + if (str == NULL) return false; + + return trie_search_tokens_with_phrases(address_dict->trie, str, tokens, phrases); +} + + +phrase_array *search_geodb_tokens(char *str, token_array *tokens) { + phrase_array *phrases = NULL; + + if (!search_address_dictionaries_tokens_with_phrases(str, tokens, &phrases)) { + return NULL; + } + + return phrases; +} + + geonames_generic_t *geodb_get_len(char *key, size_t len) { if (db == NULL || db->hash_reader == NULL || db->log_iter == NULL) return NULL; sparkey_returncode ret = sparkey_hash_get(db->hash_reader, (uint8_t *)key, len, db->log_iter); @@ -162,6 +191,8 @@ inline geonames_generic_t *geodb_get(char *key) { return geodb_get_len(key, strlen(key)); } + + bool geodb_module_setup(char *dir) { if (db == NULL) { return geodb_load(dir == NULL ? LIBPOSTAL_GEODB_DIR : dir); diff --git a/src/geodb.h b/src/geodb.h index f12984af..c3727961 100644 --- a/src/geodb.h +++ b/src/geodb.h @@ -1,8 +1,6 @@ #ifndef GEONAMES_DICTIONARY_H #define GEONAMES_DICTIONARY_H - - #include #include #include @@ -59,9 +57,14 @@ void geodb_module_teardown(void); void geodb_destroy(geodb_t *self); + +// Trie search +bool search_geodb_with_phrases(char *str, phrase_array **phrases); +phrase_array *search_geodb(char *str); +bool search_geodb_tokens_with_phrases(char *str, token_array *tokens, phrase_array **phrases); +phrase_array *search_geodb_tokens(char *str, token_array *tokens); + geonames_generic_t *geodb_get_len(char *key, size_t len); geonames_generic_t *geodb_get(char *key); - - #endif \ No newline at end of file