/* Add the specified value into a set. * * If the value was already member of the set, nothing is done and 0 is * returned, otherwise the new element is added and 1 is returned. */ int setTypeAdd(robj *subject, sds value) { long long llval; if (subject->encoding == OBJ_ENCODING_HT) { dict *ht = subject->ptr; dictEntry *de = dictAddRaw(ht,value); if (de) { dictSetKey(ht,de,sdsdup(value)); dictSetVal(ht,de,NULL); return 1; } } else if (subject->encoding == OBJ_ENCODING_INTSET) { if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) { uint8_t success = 0; subject->ptr = intsetAdd(subject->ptr,llval,&success); if (success) { /* Convert to regular set when the intset contains * too many entries. */ if (intsetLen(subject->ptr) > server.set_max_intset_entries) setTypeConvert(subject,OBJ_ENCODING_HT); return 1; } } else { /* Failed to get integer from object, convert to regular set. */ setTypeConvert(subject,OBJ_ENCODING_HT); /* The set *was* an intset and this value is not integer * encodable, so dictAdd should always work. */ serverAssert(dictAdd(subject->ptr,sdsdup(value),NULL) == DICT_OK); return 1; } } else { serverPanic("Unknown set encoding"); } return 0; }
/* 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; dictSetVal(d, entry, val); return DICT_OK; }
//添加一个元素到字典中 //注意,当前的字典只允许调用该API添加一个值为void*类型的key-value对 int dictAdd(dict *d, void *key, void *val) { //创建该key值对应的entry,然后设置其对应的值 dictEntry *entry = dictAddRaw(d,key); if (!entry) return DICT_ERR; //这里设置这个entry的value值 dictSetVal(d, entry, val); return DICT_OK; }
void lrw_dict_put(LRWDict *d, const Term *key, const void *value) { dictEntry *entry = dictFind(d->dict, key); // pstring *t = term_to_s(key); // printf("adding %.*s to size %d\n", t->len, t->val,dictSize(d->dict)); if(entry == NULL) { if(dictSize(d->dict) >= d->capacity) { _truncate(d); } entry = dictAddRaw(d->dict, (void *) key); if(!entry && dictIsRehashing(d->dict)) { entry = dictFind(d->dict, key); } assert(entry); } dictSetVal(d->dict, entry, (void *) value); _bump(d, entry->key); }
/* 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); }