summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmar Tumballi <amarts@redhat.com>2012-09-06 00:44:04 +0530
committerAnand Avati <avati@redhat.com>2012-10-02 20:07:16 -0700
commitec79583cfd2ae10998dedfeb0c2c2a83a81cdec3 (patch)
tree55703f648d71cba9317395e54a77c8904ce99160
parent3372f198a8272b3467944c759be9975ee8f0aa02 (diff)
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 <amarts@redhat.com> BUG: 850917 Reviewed-on: http://review.gluster.org/3844 Tested-by: Gluster Build System <jenkins@build.gluster.com> Reviewed-by: Anand Avati <avati@redhat.com>
-rw-r--r--libglusterfs/src/dict.c52
-rw-r--r--libglusterfs/src/dict.h4
2 files changed, 42 insertions, 14 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index 1420246e8af..e9b8da1c4e9 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 30246cdb493..dbf98046f4f 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);