From ec79583cfd2ae10998dedfeb0c2c2a83a81cdec3 Mon Sep 17 00:00:00 2001 From: Amar Tumballi Date: Thu, 6 Sep 2012 00:44:04 +0530 Subject: dict: add new API 'dict_add()' * this new API is used only when we are sure that there is no replacing keys, ie. in dict_unserialize(). Change-Id: I383dffc65056ebdaf0ab19727f7dc14ec7017fc1 Signed-off-by: Amar Tumballi BUG: 850917 Reviewed-on: http://review.gluster.org/3844 Tested-by: Gluster Build System Reviewed-by: Anand Avati --- libglusterfs/src/dict.c | 52 ++++++++++++++++++++++++++++++++++++------------- libglusterfs/src/dict.h | 4 ++++ 2 files changed, 42 insertions(+), 14 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index 1420246e..e9b8da1c 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -232,9 +232,7 @@ dict_lookup (dict_t *this, char *key, data_t **data) } static int32_t -_dict_set (dict_t *this, - char *key, - data_t *value) +_dict_set (dict_t *this, char *key, data_t *value, gf_boolean_t replace) { int hashval; data_pair_t *pair; @@ -253,17 +251,22 @@ _dict_set (dict_t *this, tmp = SuperFastHash (key, strlen (key)); hashval = (tmp % this->hash_size); - pair = _dict_lookup (this, key); - if (pair) { - data_t *unref_data = pair->value; - pair->value = data_ref (value); - data_unref (unref_data); - if (key_free) - GF_FREE (key); - /* Indicates duplicate key */ - return 0; + /* Search for a existing key if 'replace' is asked for */ + if (replace) { + pair = _dict_lookup (this, key); + + if (pair) { + data_t *unref_data = pair->value; + pair->value = data_ref (value); + data_unref (unref_data); + if (key_free) + GF_FREE (key); + /* Indicates duplicate key */ + return 0; + } } + if (this->free_pair_in_use) { pair = mem_get0 (THIS->ctx->dict_pair_pool); if (!pair) { @@ -328,7 +331,28 @@ dict_set (dict_t *this, LOCK (&this->lock); - ret = _dict_set (this, key, value); + ret = _dict_set (this, key, value, 1); + + UNLOCK (&this->lock); + + return ret; +} + + +int32_t +dict_add (dict_t *this, char *key, data_t *value) +{ + int32_t ret; + + if (!this || !value) { + gf_log_callingfn ("dict", GF_LOG_WARNING, + "!this || !value for key=%s", key); + return -1; + } + + LOCK (&this->lock); + + ret = _dict_set (this, key, value, 0); UNLOCK (&this->lock); @@ -2511,7 +2535,7 @@ dict_unserialize (char *orig_buf, int32_t size, dict_t **fill) value->is_static = 0; buf += vallen; - dict_set (*fill, key, value); + dict_add (*fill, key, value); } ret = 0; diff --git a/libglusterfs/src/dict.h b/libglusterfs/src/dict.h index 30246cdb..dbf98046 100644 --- a/libglusterfs/src/dict.h +++ b/libglusterfs/src/dict.h @@ -99,7 +99,11 @@ struct _dict { int32_t is_data_equal (data_t *one, data_t *two); void data_destroy (data_t *data); +/* function to set a key/value pair (overwrite existing if matches the key */ int32_t dict_set (dict_t *this, char *key, data_t *value); +/* function to set a new key/value pair (without checking for duplicate) */ +int32_t dict_add (dict_t *this, char *key, data_t *value); + data_t *dict_get (dict_t *this, char *key); void dict_del (dict_t *this, char *key); int dict_reset (dict_t *dict); -- cgit