summaryrefslogtreecommitdiffstats
path: root/dhash
diff options
context:
space:
mode:
authorSumit Bose <sbose@redhat.com>2010-09-29 12:24:19 +0200
committerStephen Gallagher <sgallagh@redhat.com>2010-10-12 14:26:07 -0400
commit8a5a0c340cb259103abc77d37960710177a74f8c (patch)
treef4a8f0d7c093763b284bcda578e221001827a86c /dhash
parent841a1ab44968f13f0f7b6a2ace4e38d418142cbd (diff)
downloadding-libs-8a5a0c340cb259103abc77d37960710177a74f8c.tar.gz
ding-libs-8a5a0c340cb259103abc77d37960710177a74f8c.tar.xz
ding-libs-8a5a0c340cb259103abc77d37960710177a74f8c.zip
dhash: Allow hash_enter() to update entries
Diffstat (limited to 'dhash')
-rw-r--r--dhash/dhash.c61
-rw-r--r--dhash/examples/dhash_test.c65
2 files changed, 99 insertions, 27 deletions
diff --git a/dhash/dhash.c b/dhash/dhash.c
index cb292b7..f92e463 100644
--- a/dhash/dhash.c
+++ b/dhash/dhash.c
@@ -898,34 +898,10 @@ int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value)
memcpy((void *)element->entry.key.str, key->str, len);
break;
}
- switch(element->entry.value.type = value->type) {
- case HASH_VALUE_UNDEF:
- element->entry.value.ul = 0;
- break;
- case HASH_VALUE_PTR:
- element->entry.value.ptr = value->ptr;
- break;
- case HASH_VALUE_INT:
- element->entry.value.i = value->i;
- break;
- case HASH_VALUE_UINT:
- element->entry.value.ui = value->ui;
- break;
- case HASH_VALUE_LONG:
- element->entry.value.l = value->l;
- break;
- case HASH_VALUE_ULONG:
- element->entry.value.ul = value->ul;
- break;
- case HASH_VALUE_FLOAT:
- element->entry.value.f = value->f;
- break;
- case HASH_VALUE_DOUBLE:
- element->entry.value.d = value->d;
- break;
- }
+
*chain = element; /* link into chain */
element->next = NULL;
+
/*
* Table over-full?
*/
@@ -934,7 +910,38 @@ int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value)
return error;
}
}
- } /* end not found */
+
+ } else {
+ hdelete_callback(table, HASH_ENTRY_DESTROY, &element->entry);
+ }
+
+ switch(element->entry.value.type = value->type) {
+ case HASH_VALUE_UNDEF:
+ element->entry.value.ul = 0;
+ break;
+ case HASH_VALUE_PTR:
+ element->entry.value.ptr = value->ptr;
+ break;
+ case HASH_VALUE_INT:
+ element->entry.value.i = value->i;
+ break;
+ case HASH_VALUE_UINT:
+ element->entry.value.ui = value->ui;
+ break;
+ case HASH_VALUE_LONG:
+ element->entry.value.l = value->l;
+ break;
+ case HASH_VALUE_ULONG:
+ element->entry.value.ul = value->ul;
+ break;
+ case HASH_VALUE_FLOAT:
+ element->entry.value.f = value->f;
+ break;
+ case HASH_VALUE_DOUBLE:
+ element->entry.value.d = value->d;
+ break;
+ }
+
return HASH_SUCCESS;
}
diff --git a/dhash/examples/dhash_test.c b/dhash/examples/dhash_test.c
index e5b922d..6c02de1 100644
--- a/dhash/examples/dhash_test.c
+++ b/dhash/examples/dhash_test.c
@@ -135,6 +135,8 @@ int main(int argc, char **argv)
long i, k;
int status;
hash_value_t value;
+ hash_value_t old_value;
+ hash_value_t new_value;
hash_key_t key;
char buf[1024];
hash_table_t *table = NULL;
@@ -404,6 +406,69 @@ int main(int argc, char **argv)
}
}
+ /* Update an entry */
+ if (test[0].val & 1) {
+ key.type = HASH_KEY_STRING;
+ key.str = test[0].str;
+ } else {
+ key.type = HASH_KEY_ULONG;
+ key.ul = test[0].val;
+ }
+
+ if ((status = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
+ fprintf(stderr, "Error: failed lookup for value %ld, at line %d (%s)\n",
+ test[0].val, __LINE__, error_string(status));
+ exit(1);
+ }
+
+ old_value.type = value.type;
+ switch (value.type) {
+ case HASH_VALUE_LONG:
+ old_value.ul = value.ul;
+ break;
+ case HASH_VALUE_PTR:
+ old_value.ptr = strdup(value.ptr);
+ break;
+ default:
+ fprintf(stderr, "Error: unsupported value type for update.\n");
+ exit(1);
+ }
+
+ value.type = HASH_VALUE_PTR;
+ value.ptr = (void *) strdup("Updated");
+
+ if ((status = hash_enter(table, &key, &value)) != HASH_SUCCESS) {
+ fprintf(stderr, "Error: %ld failed insertion at line %d (%s) \n",
+ test[0].val, __LINE__, error_string(status));
+ exit(1);
+ }
+
+ if ((status = hash_lookup(table, &key, &new_value)) != HASH_SUCCESS) {
+ fprintf(stderr, "Error: failed lookup for value %ld, at line %d (%s)\n",
+ test[0].val, __LINE__, error_string(status));
+ exit(1);
+ }
+
+ if (value.type == new_value.type) {
+ if (strcmp(value.ptr, new_value.ptr) != 0) {
+ fprintf(stderr, "Error: Updated value is not correct, "
+ "expected (%s) got (%s), at line %d\n",
+ (char *) value.ptr, (char *) new_value.ptr, __LINE__);
+ exit(1);
+ }
+ } else {
+ fprintf(stderr, "Error: Updated value is not correct, "
+ "expected type (%d) got (%d), at line %d\n",
+ value.type, new_value.type, __LINE__);
+ exit(1);
+ }
+
+ if ((status = hash_enter(table, &key, &old_value)) != HASH_SUCCESS) {
+ fprintf(stderr, "Error: %ld failed insertion at line %d (%s) \n",
+ test[0].val, __LINE__, error_string(status));
+ exit(1);
+ }
+
/*
* Delete a key, make sure we can't find it, assure we can find all other