From e12b27acf72ee7e40a6a39ffc7c109914c584cd9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 13:16:38 +0100 Subject: [PATCH] Persistence code for encoded sorted sets --- src/rdb.c | 46 +++++++++++++++++++++++++++++++--------------- src/redis.h | 2 ++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index c9fb3e83..62762bee 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -302,24 +302,33 @@ int rdbSaveObject(FILE *fp, robj *o) { redisPanic("Unknown set encoding"); } } else if (o->type == REDIS_ZSET) { - /* Save a set value */ - zset *zs = o->ptr; - dictIterator *di = dictGetIterator(zs->dict); - dictEntry *de; - - if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1; - nwritten += n; - - while((de = dictNext(di)) != NULL) { - robj *eleobj = dictGetEntryKey(de); - double *score = dictGetEntryVal(de); + /* Save a sorted set value */ + if (o->encoding == REDIS_ENCODING_ZIPLIST) { + size_t l = ziplistBlobLen((unsigned char*)o->ptr); - if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + if ((n = rdbSaveRawString(fp,o->ptr,l)) == -1) return -1; nwritten += n; - if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1; + } else if (o->encoding == REDIS_ENCODING_RAW) { + zset *zs = o->ptr; + dictIterator *di = dictGetIterator(zs->dict); + dictEntry *de; + + if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1; nwritten += n; + + while((de = dictNext(di)) != NULL) { + robj *eleobj = dictGetEntryKey(de); + double *score = dictGetEntryVal(de); + + if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + nwritten += n; + if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1; + nwritten += n; + } + dictReleaseIterator(di); + } else { + redisPanic("Unknown sorted set enoding"); } - dictReleaseIterator(di); } else if (o->type == REDIS_HASH) { /* Save a hash value */ if (o->encoding == REDIS_ENCODING_ZIPMAP) { @@ -386,6 +395,8 @@ int rdbSaveKeyValuePair(FILE *fp, robj *key, robj *val, vtype = REDIS_LIST_ZIPLIST; else if (vtype == REDIS_SET && val->encoding == REDIS_ENCODING_INTSET) vtype = REDIS_SET_INTSET; + else if (vtype == REDIS_ZSET && val->encoding == REDIS_ENCODING_ZIPLIST) + vtype = REDIS_ZSET_ZIPLIST; /* Save type, key, value */ if (rdbSaveType(fp,vtype) == -1) return -1; if (rdbSaveStringObject(fp,key) == -1) return -1; @@ -811,7 +822,8 @@ robj *rdbLoadObject(int type, FILE *fp) { } } else if (type == REDIS_HASH_ZIPMAP || type == REDIS_LIST_ZIPLIST || - type == REDIS_SET_INTSET) + type == REDIS_SET_INTSET || + type == REDIS_ZSET_ZIPLIST) { robj *aux = rdbLoadStringObject(fp); @@ -846,6 +858,10 @@ robj *rdbLoadObject(int type, FILE *fp) { if (intsetLen(o->ptr) > server.set_max_intset_entries) setTypeConvert(o,REDIS_ENCODING_HT); break; + case REDIS_ZSET_ZIPLIST: + o->type = REDIS_ZSET; + o->encoding = REDIS_ENCODING_ZIPLIST; + break; default: redisPanic("Unknown enoding"); break; diff --git a/src/redis.h b/src/redis.h index 78294d0d..9924cee7 100644 --- a/src/redis.h +++ b/src/redis.h @@ -70,10 +70,12 @@ #define REDIS_ZSET 3 #define REDIS_HASH 4 #define REDIS_VMPOINTER 8 + /* Object types only used for persistence in .rdb files */ #define REDIS_HASH_ZIPMAP 9 #define REDIS_LIST_ZIPLIST 10 #define REDIS_SET_INTSET 11 +#define REDIS_ZSET_ZIPLIST 12 /* Objects encoding. Some kind of objects like Strings and Hashes can be * internally represented in multiple ways. The 'encoding' field of the object -- 2.47.2