diff options
| author | Sumit Bose <sbose@redhat.com> | 2010-09-29 12:24:19 +0200 |
|---|---|---|
| committer | Stephen Gallagher <sgallagh@redhat.com> | 2010-10-12 14:26:07 -0400 |
| commit | 8a5a0c340cb259103abc77d37960710177a74f8c (patch) | |
| tree | f4a8f0d7c093763b284bcda578e221001827a86c /dhash/examples | |
| parent | 841a1ab44968f13f0f7b6a2ace4e38d418142cbd (diff) | |
| download | ding-libs2-8a5a0c340cb259103abc77d37960710177a74f8c.tar.gz ding-libs2-8a5a0c340cb259103abc77d37960710177a74f8c.tar.xz ding-libs2-8a5a0c340cb259103abc77d37960710177a74f8c.zip | |
dhash: Allow hash_enter() to update entries
Diffstat (limited to 'dhash/examples')
| -rw-r--r-- | dhash/examples/dhash_test.c | 65 |
1 files changed, 65 insertions, 0 deletions
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 |
