Low level add or find: * 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 they wish. * * This function is also directly exposed to the user API to be called * mainly in order to store non-pointers inside the hash value, example: * * entry = dictAddRaw(dict,mykey,NULL); * if (entry !=
| 317 | * If key was added, the hash entry is returned to be manipulated by the caller. |
| 318 | */ |
| 319 | dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing) |
| 320 | { |
| 321 | long index; |
| 322 | dictEntry *entry; |
| 323 | dictht *ht; |
| 324 | |
| 325 | if (dictIsRehashing(d)) _dictRehashStep(d); |
| 326 | |
| 327 | /* Get the index of the new element, or -1 if |
| 328 | * the element already exists. */ |
| 329 | if ((index = _dictKeyIndex(d, key, dictHashKey(d,key), existing)) == -1) |
| 330 | return NULL; |
| 331 | |
| 332 | /* Allocate the memory and store the new entry. |
| 333 | * Insert the element in top, with the assumption that in a database |
| 334 | * system it is more likely that recently added entries are accessed |
| 335 | * more frequently. */ |
| 336 | ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0]; |
| 337 | entry = zmalloc(sizeof(*entry)); |
| 338 | entry->next = ht->table[index]; |
| 339 | ht->table[index] = entry; |
| 340 | ht->used++; |
| 341 | |
| 342 | /* Set the hash entry fields. */ |
| 343 | dictSetKey(d, entry, key); |
| 344 | return entry; |
| 345 | } |
| 346 | |
| 347 | /* Add or Overwrite: |
| 348 | * Add an element, discarding the old value if the key already exists. |
no test coverage detected