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 !=
| 631 | * If key was added, the hash entry is returned to be manipulated by the caller. |
| 632 | */ |
| 633 | dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing) |
| 634 | { |
| 635 | long index; |
| 636 | dictEntry *entry; |
| 637 | dictht *ht; |
| 638 | |
| 639 | if (dictIsRehashing(d)) _dictRehashStep(d); |
| 640 | |
| 641 | /* Get the index of the new element, or -1 if |
| 642 | * the element already exists. */ |
| 643 | if ((index = _dictKeyIndex(d, key, dictHashKey(d,key), existing)) == -1) |
| 644 | return NULL; |
| 645 | |
| 646 | /* Allocate the memory and store the new entry. |
| 647 | * Insert the element in top, with the assumption that in a database |
| 648 | * system it is more likely that recently added entries are accessed |
| 649 | * more frequently. */ |
| 650 | ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0]; |
| 651 | entry = (dictEntry*)zmalloc(sizeof(*entry), MALLOC_SHARED); |
| 652 | entry->next = ht->table[index]; |
| 653 | ht->table[index] = entry; |
| 654 | ht->used++; |
| 655 | |
| 656 | /* Set the hash entry fields. */ |
| 657 | dictSetKey(d, entry, key); |
| 658 | return entry; |
| 659 | } |
| 660 | |
| 661 | /* Add or Overwrite: |
| 662 | * Add an element, discarding the old value if the key already exists. |
no test coverage detected