1 #ifndef GEONAMES_DICTIONARY_H 2 #define GEONAMES_DICTIONARY_H 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <stdint.h> 7 8 #include "libpostal_config.h" 9 #include "geonames.h" 10 #include "graph.h" 11 #include "sparkey/sparkey.h" 12 #include "sparkey/sparkey-internal.h" 13 #include "string_utils.h" 14 #include "trie.h" 15 #include "trie_search.h" 16 17 #define GEODB_NAMES_TRIE_FILENAME "geodb_names.trie" 18 #define GEODB_TRIE_FILENAME_LEN strlen(GEODB_NAMES_TRIE_FILENAME) 19 #define GEODB_FEATURES_TRIE_FILENAME "geodb_features.trie" 20 #define GEODB_FEATURES_TRIE_FILENAME_LEN strlen(GEODB_FEATURES_TRIE_FILENAME) 21 #define GEODB_FEATURE_GRAPH_FILENAME "geodb_feature_graph.dat" 22 #define GEODB_FEATURE_GRAPH_FILENAME_LEN strlen(GEODB_FEATURE_GRAPH_FILENAME) 23 #define GEODB_POSTAL_CODES_FILENAME "geodb_postal_codes.dat" 24 #define GEODB_POSTAL_CODES_FILENAME_LEN strlen(GEODB_POSTAL_CODES_FILENAME) 25 #define GEODB_HASH_FILENAME "geodb.spi" 26 #define GEODB_HASH_FILENAME_LEN strlen(GEODB_HASH_FILENAME) 27 #define GEODB_LOG_FILENAME "geodb.spl" 28 #define GEODB_LOG_FILENAME_LEN strlen(GEODB_LOG_FILENAME) 29 30 // Can manipulate the bit-packed values separately, or access the whole value 31 typedef union geodb_value { 32 uint32_t value; 33 struct { 34 uint32_t is_canonical:1; 35 uint32_t components:15; 36 uint32_t count:16; 37 }; 38 } geodb_value_t; 39 40 typedef struct geodb { 41 trie_t *names; 42 trie_t *features; 43 cstring_array *postal_codes; 44 graph_t *feature_graph; 45 sparkey_hashreader *hash_reader; 46 sparkey_logiter *log_iter; 47 char_array *value_buf; 48 geoname_t *geoname; 49 gn_postal_code_t *postal_code; 50 } geodb_t; 51 52 typedef struct gn_geocoding_result { 53 int start; 54 int end; 55 geonames_generic_t result; 56 } gn_geocoding_result_t; 57 58 geodb_t *get_geodb(void); 59 bool geodb_load(char *dir); 60 61 bool geodb_module_setup(char *dir); 62 void geodb_module_teardown(void); 63 64 void geodb_destroy(geodb_t *self); 65 66 67 // Trie search 68 bool search_geodb_with_phrases(char *str, phrase_array **phrases); 69 phrase_array *search_geodb(char *str); 70 bool search_geodb_tokens_with_phrases(char *str, token_array *tokens, phrase_array **phrases); 71 phrase_array *search_geodb_tokens(char *str, token_array *tokens); 72 73 geonames_generic_t *geodb_get_len(char *key, size_t len); 74 geonames_generic_t *geodb_get(char *key); 75 76 #endif