static void sparkey_randomaccess(int n, int lookups) { sparkey_hashreader *myreader; sparkey_logiter *myiter; sparkey_assert(sparkey_hash_open(&myreader, "test.spi", "test.spl")); sparkey_logreader *logreader = sparkey_hash_getreader(myreader); sparkey_assert(sparkey_logiter_create(&myiter, logreader)); uint8_t *valuebuf = malloc(sparkey_logreader_maxvaluelen(logreader)); for (int i = 0; i < lookups; i++) { char mykey[100]; char myvalue[100]; int r = rand() % n; sprintf(mykey, "key_%d", r); sprintf(myvalue, "value_%d", r); sparkey_assert(sparkey_hash_get(myreader, (uint8_t*)mykey, strlen(mykey), myiter)); if (sparkey_logiter_state(myiter) != SPARKEY_ITER_ACTIVE) { printf("Failed to lookup key: %s\n", mykey); exit(1); } uint64_t wanted_valuelen = sparkey_logiter_valuelen(myiter); uint64_t actual_valuelen; sparkey_assert(sparkey_logiter_fill_value(myiter, logreader, wanted_valuelen, valuebuf, &actual_valuelen)); if (actual_valuelen != strlen(myvalue) || memcmp(myvalue, valuebuf, actual_valuelen)) { printf("Did not get the expected value for key: %s\n", mykey); exit(1); } } sparkey_logiter_close(&myiter); sparkey_hash_close(&myreader); }
geodb_t *geodb_init(char *dir) { if (dir == NULL) return NULL; geodb_t *gdb = malloc(sizeof(geodb_t)); if (gdb == NULL) return NULL; char_array *path = char_array_new_size(strlen(dir)); char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_NAMES_TRIE_FILENAME); char *names_path = char_array_get_string(path); gdb->names = trie_load(names_path); if (gdb->names == NULL) { goto exit_geodb_created; } char_array_clear(path); char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_FEATURES_TRIE_FILENAME); char *features_path = char_array_get_string(path); gdb->features = trie_load(features_path); if(gdb->features == NULL) { goto exit_geodb_created; } char_array_clear(path); char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_POSTAL_CODES_FILENAME); char *postal_codes_path = char_array_get_string(path); FILE *f = fopen(postal_codes_path, "rb"); uint64_t num_postal_strings = 0; if (!file_read_uint64(f, (uint64_t *)&num_postal_strings)) { goto exit_geodb_created; } size_t postal_codes_str_len; if (!file_read_uint64(f, (uint64_t *)&postal_codes_str_len)) { goto exit_geodb_created; } char_array *array = char_array_new_size(postal_codes_str_len); if (!file_read_chars(f, array->a, postal_codes_str_len)) { goto exit_geodb_created; } array->n = postal_codes_str_len; gdb->postal_codes = cstring_array_from_char_array(array); if (cstring_array_num_strings(gdb->postal_codes) != num_postal_strings) { goto exit_geodb_created; } fclose(f); char_array_clear(path); char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_HASH_FILENAME); char *hash_file_path = strdup(char_array_get_string(path)); char_array_clear(path); char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_LOG_FILENAME); char *log_path = char_array_get_string(path); gdb->hash_reader = NULL; if ((sparkey_hash_open(&gdb->hash_reader, hash_file_path, log_path)) != SPARKEY_SUCCESS) { free(hash_file_path); char_array_destroy(path); goto exit_geodb_created; } free(hash_file_path); char_array_destroy(path); gdb->log_iter = NULL; if ((sparkey_logiter_create(&gdb->log_iter, sparkey_hash_getreader(gdb->hash_reader))) != SPARKEY_SUCCESS) { goto exit_geodb_created; } gdb->value_buf = char_array_new_size(sparkey_logreader_maxvaluelen(sparkey_hash_getreader(gdb->hash_reader))); if (gdb->value_buf == NULL) { goto exit_geodb_created; } gdb->geoname = geoname_new(); if (gdb->geoname == NULL) { goto exit_geodb_created; } gdb->postal_code = gn_postal_code_new(); if (gdb->postal_code == NULL) { goto exit_geodb_created; } return gdb; exit_geodb_created: geodb_destroy(gdb); return NULL; }