summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2020-04-09 16:21:48 +0200
committerantirez <antirez@gmail.com>2020-04-09 16:21:48 +0200
commitd88f52ee7d97c37a1845cf38d259b7900a940c71 (patch)
tree61040f5cacfeb2dff6e63fcf8a6ddcef9e9d9096
parent399a6b2b471cac481b00673e317fd585bd13d41f (diff)
downloadredis-d88f52ee7d97c37a1845cf38d259b7900a940c71.tar.gz
RDB: refactor some RDB loading code into dbAddRDBLoad().
-rw-r--r--src/db.c18
-rw-r--r--src/rdb.c7
-rw-r--r--src/server.h1
3 files changed, 22 insertions, 4 deletions
diff --git a/src/db.c b/src/db.c
index d393a5fdd..59f0cc7a0 100644
--- a/src/db.c
+++ b/src/db.c
@@ -188,6 +188,24 @@ void dbAdd(redisDb *db, robj *key, robj *val) {
if (server.cluster_enabled) slotToKeyAdd(key->ptr);
}
+/* This is a special version of dbAdd() that is used only when loading
+ * keys from the RDB file: the key is passed as an SDS string that is
+ * retained by the function (and not freed by the caller).
+ *
+ * Moreover this function will not abort if the key is already busy, to
+ * give more control to the caller, nor will signal the key as ready
+ * since it is not useful in this context.
+ *
+ * The function returns 1 if the key was added to the database, taking
+ * ownership of the SDS string, otherwise 0 is returned, and is up to the
+ * caller to free the SDS string. */
+int dbAddRDBLoad(redisDb *db, sds key, robj *val) {
+ int retval = dictAdd(db->dict, key, val);
+ if (retval != DICT_OK) return 0;
+ if (server.cluster_enabled) slotToKeyAdd(key);
+ return 1;
+}
+
/* Overwrite an existing key with a new value. Incrementing the reference
* count of the new value is up to the caller.
* This function does not modify the expire time of the existing key.
diff --git a/src/rdb.c b/src/rdb.c
index 3f25535ab..143b6c325 100644
--- a/src/rdb.c
+++ b/src/rdb.c
@@ -2245,22 +2245,21 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
robj keyobj;
/* Add the new object in the hash table */
- int retval = dictAdd(db->dict, key, val);
- if (retval != DICT_OK) {
+ int added = dbAddRDBLoad(db,key,val);
+ if (!added) {
if (rdbflags & RDBFLAGS_ALLOW_DUP) {
/* This flag is useful for DEBUG RELOAD special modes.
* When it's set we allow new keys to replace the current
* keys with the same name. */
initStaticStringObject(keyobj,key);
dbSyncDelete(db,&keyobj);
- dictAdd(db->dict, key, val);
+ dbAddRDBLoad(db,key,val);
} else {
serverLog(LL_WARNING,
"RDB has duplicated key '%s' in DB %d",key,db->id);
serverPanic("Duplicated key found in RDB file");
}
}
- if (server.cluster_enabled) slotToKeyAdd(key);
/* Set the expire time if needed */
if (expiretime != -1) {
diff --git a/src/server.h b/src/server.h
index 9691381c3..c268f2bee 100644
--- a/src/server.h
+++ b/src/server.h
@@ -2069,6 +2069,7 @@ int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
#define LOOKUP_NONE 0
#define LOOKUP_NOTOUCH (1<<0)
void dbAdd(redisDb *db, robj *key, robj *val);
+int dbAddRDBLoad(redisDb *db, sds key, robj *val);
void dbOverwrite(redisDb *db, robj *key, robj *val);
void genericSetKey(redisDb *db, robj *key, robj *val, int keepttl, int signal);
void setKey(redisDb *db, robj *key, robj *val);