unsigned long datastore_delete(datastore_t *ds, const char *key) { pthread_mutex_lock(&ds->mutex); datastore_entry_t *entry = dictionary_tfind(ds, key); if (entry == NULL) { pthread_mutex_unlock(&ds->mutex); return 0; } else { /* if (entry->rev != known_revision) { pthread_mutex_unlock(&ds->mutex); return 0; } */ dictionary_tdelete(ds, key); free((void *)entry->key); free((void *)entry->value); free(entry); pthread_mutex_unlock(&ds->mutex); return 1; } }
unsigned long datastore_update(datastore_t *ds, const char *key, const char *value, unsigned long known_revision) { pthread_mutex_lock(&ds->mutex); datastore_entry_t *entry = dictionary_tfind(ds, key); if (entry == NULL) { // key does not exist pthread_mutex_unlock(&ds->mutex); return 0; } else { // ensure revisions match if (entry->rev != known_revision) { pthread_mutex_unlock(&ds->mutex); return -2; } free((void *)entry->value); entry->value = strdup(value); unsigned long new_revision = ++(entry->rev); //printf("updata: %s %s %d\n", entry->value, entry->key, entry->rev); pthread_mutex_unlock(&ds->mutex); return new_revision; } }
/** * Retrieves the value from the dictionary for a given key. * * @return The stored value associated with the key * if the key exists in the dictionary. If the key does not exist, * this function will return NULL. */ const char *dictionary_get(dictionary_t *d, const char *key) { pthread_mutex_lock(&d->mutex); dictionary_entry_t *entry = dictionary_tfind(d, key); if (entry == NULL) { pthread_mutex_unlock(&d->mutex); return NULL; } else { pthread_mutex_unlock(&d->mutex); return entry->value; } }
/** * Adds a (key, value) pair to the dictionary. * @return 0 on success or KEY_EXISTS if * the key already exists in the dictionary. */ int dictionary_add(dictionary_t *d, const char *key, const char *value) { pthread_mutex_lock(&d->mutex); if (dictionary_tfind(d, key) == NULL) { tsearch((void *)malloc_entry_t(key, value), &d->root, compare); pthread_mutex_unlock(&d->mutex); return 0; } else { pthread_mutex_unlock(&d->mutex); return KEY_EXISTS; } }
const char *datastore_get(datastore_t *ds, const char *key, unsigned long *revision) { pthread_mutex_lock(&ds->mutex); datastore_entry_t *entry = dictionary_tfind(ds, key); if (entry == NULL) { pthread_mutex_unlock(&ds->mutex); return NULL; } else { if (revision != NULL) *revision = entry->rev; const char *value = strdup(entry->value); //const char * value = entry->value; pthread_mutex_unlock(&ds->mutex); return value; } }
unsigned long datastore_put(datastore_t *ds, const char *key, const char *value, const long rev) { pthread_mutex_lock(&ds->mutex); if (dictionary_tfind(ds, key) == NULL) { datastore_entry_t *entry = malloc_entry_t(strdup(key), strdup(value), rev); tsearch((void *)entry, &ds->root, compare); pthread_mutex_unlock(&ds->mutex); //datastore_entry_t *entry = dictionary_tfind(ds, key); //printf("add: %s %s %d\n", entry->value, entry->key, entry->rev); return entry->rev; } else { pthread_mutex_unlock(&ds->mutex); return 0; } }
/** Internal use only. */ static int dictionary_remove_options(dictionary_t *d, const char *key, int free_memory) { dictionary_entry_t *entry = dictionary_tfind(d, key); if (entry == NULL) return NO_KEY_EXISTS; else { dictionary_tdelete(d, key); if (free_memory) { free((void *)entry->key); free((void *)entry->value); } free(entry); return 0; } }