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

Function RM_HashSet

src/module.cpp:3207–3280  ·  view source on GitHub ↗

Set the field of the specified hash field to the specified value. * If the key is an empty key open for writing, it is created with an empty * hash value, in order to set the specified field. * * The function is variadic and the user must specify pairs of field * names and values, both as RedisModuleString pointers (unless the * CFIELD option is set, see later). At the end of the field/value

Source from the content-addressed store, hash-verified

3205 * the Redis version and handle it accordingly.
3206 */
3207int RM_HashSet(RedisModuleKey *key, int flags, ...) {
3208 va_list ap;
3209 if (!key || (flags & ~(REDISMODULE_HASH_NX |
3210 REDISMODULE_HASH_XX |
3211 REDISMODULE_HASH_CFIELDS |
3212 REDISMODULE_HASH_COUNT_ALL))) {
3213 errno = EINVAL;
3214 return 0;
3215 } else if (key->value && key->value->type != OBJ_HASH) {
3216 errno = ENOTSUP;
3217 return 0;
3218 } else if (!(key->mode & REDISMODULE_WRITE)) {
3219 errno = EBADF;
3220 return 0;
3221 }
3222 if (key->value == NULL) moduleCreateEmptyKey(key,REDISMODULE_KEYTYPE_HASH);
3223
3224 int count = 0;
3225 va_start(ap, flags);
3226 while(1) {
3227 RedisModuleString *field, *value;
3228 /* Get the field and value objects. */
3229 if (flags & REDISMODULE_HASH_CFIELDS) {
3230 char *cfield = va_arg(ap,char*);
3231 if (cfield == NULL) break;
3232 field = createRawStringObject(cfield,strlen(cfield));
3233 } else {
3234 field = va_arg(ap,RedisModuleString*);
3235 if (field == NULL) break;
3236 }
3237 value = va_arg(ap,RedisModuleString*);
3238
3239 /* Handle XX and NX */
3240 if (flags & (REDISMODULE_HASH_XX|REDISMODULE_HASH_NX)) {
3241 int exists = hashTypeExists(key->value, szFromObj(field));
3242 if (((flags & REDISMODULE_HASH_XX) && !exists) ||
3243 ((flags & REDISMODULE_HASH_NX) && exists))
3244 {
3245 if (flags & REDISMODULE_HASH_CFIELDS) decrRefCount(field);
3246 continue;
3247 }
3248 }
3249
3250 /* Handle deletion if value is REDISMODULE_HASH_DELETE. */
3251 if (value == REDISMODULE_HASH_DELETE) {
3252 count += hashTypeDelete(key->value, szFromObj(field));
3253 if (flags & REDISMODULE_HASH_CFIELDS) decrRefCount(field);
3254 continue;
3255 }
3256
3257 int low_flags = HASH_SET_COPY;
3258 /* If CFIELDS is active, we can pass the ownership of the
3259 * SDS object to the low level function that sets the field
3260 * to avoid a useless copy. */
3261 if (flags & REDISMODULE_HASH_CFIELDS)
3262 low_flags |= HASH_SET_TAKE_FIELD;
3263
3264 robj *argv[2] = {field,value};

Callers

nothing calls this directly

Calls 9

moduleCreateEmptyKeyFunction · 0.85
createRawStringObjectFunction · 0.85
hashTypeExistsFunction · 0.85
szFromObjFunction · 0.85
decrRefCountFunction · 0.85
hashTypeDeleteFunction · 0.85
hashTypeTryConversionFunction · 0.85
hashTypeSetFunction · 0.85
moduleDelKeyIfEmptyFunction · 0.85

Tested by

no test coverage detected