int get(const char *hashfile, const char *logfile, const char *key) { sparkey_hashreader *reader; sparkey_logreader *logreader; sparkey_logiter *iter; assert(sparkey_hash_open(&reader, hashfile, logfile)); logreader = sparkey_hash_getreader(reader); assert(sparkey_logiter_create(&iter, logreader)); uint64_t keylen = strlen(key); assert(sparkey_hash_get(reader, (uint8_t*) key, keylen, iter)); int exitcode = 2; if (sparkey_logiter_state(iter) == SPARKEY_ITER_ACTIVE) { exitcode = 0; uint8_t * res; uint64_t len; do { assert(sparkey_logiter_valuechunk(iter, logreader, 1 << 31, &res, &len)); assert(write_full(STDOUT_FILENO, res, len)); } while (len > 0); } sparkey_logiter_close(&iter); sparkey_hash_close(&reader); return exitcode; }
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); }
void geodb_destroy(geodb_t *self) { if (self == NULL) return; if (self->names != NULL) { trie_destroy(self->names); } if (self->features != NULL) { trie_destroy(self->features); } if (self->postal_codes != NULL) { cstring_array_destroy(self->postal_codes); } if (self->hash_reader != NULL) { sparkey_hash_close(&self->hash_reader); } if (self->log_iter != NULL) { sparkey_logiter_close(&self->log_iter); } if (self->value_buf != NULL) { char_array_destroy(self->value_buf); } if (self->geoname != NULL) { geoname_destroy(self->geoname); } if (self->postal_code != NULL) { gn_postal_code_destroy(self->postal_code); } free(self); }