MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / hashTypeSet

Function hashTypeSet

src/t_hash.cpp:208–278  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

206#define HASH_SET_TAKE_VALUE (1<<1)
207#define HASH_SET_COPY 0
208int hashTypeSet(robj *o, sds field, sds value, int flags) {
209 int update = 0;
210
211 if (o->encoding == OBJ_ENCODING_ZIPLIST) {
212 unsigned char *zl, *fptr, *vptr;
213
214 zl = (unsigned char*)ptrFromObj(o);
215 fptr = ziplistIndex(zl, ZIPLIST_HEAD);
216 if (fptr != NULL) {
217 fptr = ziplistFind(zl, fptr, (unsigned char*)field, sdslen(field), 1);
218 if (fptr != NULL) {
219 /* Grab pointer to the value (fptr points to the field) */
220 vptr = ziplistNext(zl, fptr);
221 serverAssert(vptr != NULL);
222 update = 1;
223
224 /* Replace value */
225 zl = ziplistReplace(zl, vptr, (unsigned char*)value,
226 sdslen(value));
227 }
228 }
229
230 if (!update) {
231 /* Push new field/value pair onto the tail of the ziplist */
232 zl = ziplistPush(zl, (unsigned char*)field, sdslen(field),
233 ZIPLIST_TAIL);
234 zl = ziplistPush(zl, (unsigned char*)value, sdslen(value),
235 ZIPLIST_TAIL);
236 }
237 o->m_ptr = zl;
238
239 /* Check if the ziplist needs to be converted to a hash table */
240 if (hashTypeLength(o) > g_pserver->hash_max_ziplist_entries)
241 hashTypeConvert(o, OBJ_ENCODING_HT);
242 } else if (o->encoding == OBJ_ENCODING_HT) {
243 dictEntry *de = dictFind((dict*)ptrFromObj(o),field);
244 if (de) {
245 sdsfree((sds)dictGetVal(de));
246 if (flags & HASH_SET_TAKE_VALUE) {
247 dictGetVal(de) = value;
248 value = NULL;
249 } else {
250 dictGetVal(de) = sdsdup(value);
251 }
252 update = 1;
253 } else {
254 sds f,v;
255 if (flags & HASH_SET_TAKE_FIELD) {
256 f = field;
257 field = NULL;
258 } else {
259 f = sdsdup(field);
260 }
261 if (flags & HASH_SET_TAKE_VALUE) {
262 v = value;
263 value = NULL;
264 } else {
265 v = sdsdup(value);

Callers 6

hsetnxCommandFunction · 0.85
hsetCommandFunction · 0.85
hincrbyCommandFunction · 0.85
hincrbyfloatCommandFunction · 0.85
hrenameCommandFunction · 0.85
RM_HashSetFunction · 0.85

Calls 13

ptrFromObjFunction · 0.85
ziplistIndexFunction · 0.85
ziplistFindFunction · 0.85
sdslenFunction · 0.85
ziplistNextFunction · 0.85
ziplistReplaceFunction · 0.85
ziplistPushFunction · 0.85
hashTypeLengthFunction · 0.85
hashTypeConvertFunction · 0.85
sdsfreeFunction · 0.85
sdsdupFunction · 0.85
dictFindFunction · 0.70

Tested by

no test coverage detected