Skip to content

Commit 2b59cfd

Browse files
committed
ZSET now saved on disk like any other type
1 parent a7866db commit 2b59cfd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

redis.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ static size_t stringObjectLen(robj *o);
379379
static void processInputBuffer(redisClient *c);
380380
static zskiplist *zslCreate(void);
381381
static void zslFree(zskiplist *zsl);
382+
static void zslInsert(zskiplist *zsl, double score, robj *obj);
382383

383384
static void authCommand(redisClient *c);
384385
static void pingCommand(redisClient *c);
@@ -2352,6 +2353,21 @@ static int rdbSave(char *filename) {
23522353
if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
23532354
}
23542355
dictReleaseIterator(di);
2356+
} else if (o->type == REDIS_ZSET) {
2357+
/* Save a set value */
2358+
zset *zs = o->ptr;
2359+
dictIterator *di = dictGetIterator(zs->dict);
2360+
dictEntry *de;
2361+
2362+
if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) goto werr;
2363+
while((de = dictNext(di)) != NULL) {
2364+
robj *eleobj = dictGetEntryKey(de);
2365+
double *score = dictGetEntryVal(de);
2366+
2367+
if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
2368+
if (rdbSaveDoubleValue(fp,*score) == -1) goto werr;
2369+
}
2370+
dictReleaseIterator(di);
23552371
} else {
23562372
assert(0 != 0);
23572373
}
@@ -2631,6 +2647,27 @@ static int rdbLoad(char *filename) {
26312647
dictAdd((dict*)o->ptr,ele,NULL);
26322648
}
26332649
}
2650+
} else if (type == REDIS_ZSET) {
2651+
/* Read list/set value */
2652+
uint32_t zsetlen;
2653+
zset *zs;
2654+
2655+
if ((zsetlen = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR)
2656+
goto eoferr;
2657+
o = createZsetObject();
2658+
zs = o->ptr;
2659+
/* Load every single element of the list/set */
2660+
while(zsetlen--) {
2661+
robj *ele;
2662+
double *score = zmalloc(sizeof(double));
2663+
2664+
if ((ele = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
2665+
tryObjectEncoding(ele);
2666+
if (rdbLoadDoubleValue(fp,score) == -1) goto eoferr;
2667+
dictAdd(zs->dict,ele,score);
2668+
zslInsert(zs->zsl,*score,ele);
2669+
incrRefCount(ele); /* added to skiplist */
2670+
}
26342671
} else {
26352672
assert(0 != 0);
26362673
}
@@ -5114,6 +5151,8 @@ static struct redisFunctionSym symsTable[] = {
51145151
{"zrangeCommand",(unsigned long)zrangeCommand},
51155152
{"zrevrangeCommand",(unsigned long)zrevrangeCommand},
51165153
{"zremCommand",(unsigned long)zremCommand},
5154+
{"rdbSaveDoubleValue",(unsigned long)rdbSaveDoubleValue},
5155+
{"rdbLoadDoubleValue",(unsigned long)rdbLoadDoubleValue},
51175156
{NULL,0}
51185157
};
51195158

0 commit comments

Comments
 (0)