/**
 * 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;
	}
}
Exemple #2
0
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;
	}
}