]> git.saurik.com Git - redis.git/commitdiff
dict.c: added two lower level methods for directly manipulating hash entries. This...
authorantirez <antirez@gmail.com>
Tue, 8 Nov 2011 15:57:20 +0000 (16:57 +0100)
committerantirez <antirez@gmail.com>
Tue, 8 Nov 2011 15:57:20 +0000 (16:57 +0100)
src/dict.c
src/dict.h

index f98fdd0ee78c818b315158cabf7ae4ea0b9ecc04..9d3368412d0443a8c22db585101bd95290915625 100644 (file)
@@ -258,6 +258,30 @@ static void _dictRehashStep(dict *d) {
 
 /* Add an element to the target hash table */
 int dictAdd(dict *d, void *key, void *val)
 
 /* Add an element to the target hash table */
 int dictAdd(dict *d, void *key, void *val)
+{
+    dictEntry *entry = dictAddRaw(d,key);
+
+    if (!entry) return DICT_ERR;
+    dictSetHashVal(d, entry, val);
+    return DICT_OK;
+}
+
+/* Low level add. This function adds the entry but instead of setting
+ * a value returns the dictEntry structure to the user, that will make
+ * sure to fill the value field as he wishes.
+ *
+ * This function is also directly expoed to user API to be called
+ * mainly in order to store non-pointers inside the hash value, example:
+ *
+ * entry = dictAddRaw(dict,mykey);
+ * if (entry != NULL) dictSetValSignedInteger(entry,1000);
+ *
+ * Return values:
+ *
+ * If key already exists NULL is returned.
+ * If key was added, the hash entry is returned to be manipulated by the caller.
+ */
+dictEntry *dictAddRaw(dict *d, void *key)
 {
     int index;
     dictEntry *entry;
 {
     int index;
     dictEntry *entry;
@@ -268,7 +292,7 @@ int dictAdd(dict *d, void *key, void *val)
     /* Get the index of the new element, or -1 if
      * the element already exists. */
     if ((index = _dictKeyIndex(d, key)) == -1)
     /* Get the index of the new element, or -1 if
      * the element already exists. */
     if ((index = _dictKeyIndex(d, key)) == -1)
-        return DICT_ERR;
+        return NULL;
 
     /* Allocate the memory and store the new entry */
     ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
 
     /* Allocate the memory and store the new entry */
     ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
@@ -279,8 +303,7 @@ int dictAdd(dict *d, void *key, void *val)
 
     /* Set the hash entry fields. */
     dictSetHashKey(d, entry, key);
 
     /* Set the hash entry fields. */
     dictSetHashKey(d, entry, key);
-    dictSetHashVal(d, entry, val);
-    return DICT_OK;
+    return entry;
 }
 
 /* Add an element, discarding the old if the key already exists.
 }
 
 /* Add an element, discarding the old if the key already exists.
@@ -308,6 +331,18 @@ int dictReplace(dict *d, void *key, void *val)
     return 0;
 }
 
     return 0;
 }
 
+/* dictReplaceRaw() is simply a version of dictAddRaw() that always
+ * returns the hash entry of the specified key, even if the key already
+ * exists and can't be added (in that case the entry of the already
+ * existing key is returned.)
+ *
+ * See dictAddRaw() for more information. */
+dictEntry *dictReplaceRaw(dict *d, void *key) {
+    dictEntry *entry = dictFind(d,key);
+
+    return entry ? entry : dictAddRaw(d,key);
+}
+
 /* Search and remove an element */
 static int dictGenericDelete(dict *d, const void *key, int nofree)
 {
 /* Search and remove an element */
 static int dictGenericDelete(dict *d, const void *key, int nofree)
 {
index 31cd65646c12f72658231328f745d9495754decc..23b1fc52eeb21c79dc013630d26d48eb814e2567 100644 (file)
@@ -133,7 +133,9 @@ typedef struct dictIterator {
 dict *dictCreate(dictType *type, void *privDataPtr);
 int dictExpand(dict *d, unsigned long size);
 int dictAdd(dict *d, void *key, void *val);
 dict *dictCreate(dictType *type, void *privDataPtr);
 int dictExpand(dict *d, unsigned long size);
 int dictAdd(dict *d, void *key, void *val);
+dictEntry *dictAddRaw(dict *d, void *key);
 int dictReplace(dict *d, void *key, void *val);
 int dictReplace(dict *d, void *key, void *val);
+dictEntry *dictReplaceRaw(dict *d, void *key);
 int dictDelete(dict *d, const void *key);
 int dictDeleteNoFree(dict *d, const void *key);
 void dictRelease(dict *d);
 int dictDelete(dict *d, const void *key);
 int dictDeleteNoFree(dict *d, const void *key);
 void dictRelease(dict *d);