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

Function rewriteSortedSetObject

src/aof.cpp:1193–1269  ·  view source on GitHub ↗

Emit the commands needed to rebuild a sorted set object. * The function returns 0 on error, 1 on success. */

Source from the content-addressed store, hash-verified

1191/* Emit the commands needed to rebuild a sorted set object.
1192 * The function returns 0 on error, 1 on success. */
1193int rewriteSortedSetObject(rio *r, robj *key, robj *o) {
1194 long long count = 0, items = zsetLength(o);
1195
1196 if (o->encoding == OBJ_ENCODING_ZIPLIST) {
1197 unsigned char *zl = (unsigned char*)ptrFromObj(o);
1198 unsigned char *eptr, *sptr;
1199 unsigned char *vstr;
1200 unsigned int vlen;
1201 long long vll;
1202 double score;
1203
1204 eptr = ziplistIndex(zl,0);
1205 serverAssert(eptr != NULL);
1206 sptr = ziplistNext(zl,eptr);
1207 serverAssert(sptr != NULL);
1208
1209 while (eptr != NULL) {
1210 serverAssert(ziplistGet(eptr,&vstr,&vlen,&vll));
1211 score = zzlGetScore(sptr);
1212
1213 if (count == 0) {
1214 int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?
1215 AOF_REWRITE_ITEMS_PER_CMD : items;
1216
1217 if (!rioWriteBulkCount(r,'*',2+cmd_items*2) ||
1218 !rioWriteBulkString(r,"ZADD",4) ||
1219 !rioWriteBulkObject(r,key))
1220 {
1221 return 0;
1222 }
1223 }
1224 if (!rioWriteBulkDouble(r,score)) return 0;
1225 if (vstr != NULL) {
1226 if (!rioWriteBulkString(r,(char*)vstr,vlen)) return 0;
1227 } else {
1228 if (!rioWriteBulkLongLong(r,vll)) return 0;
1229 }
1230 zzlNext(zl,&eptr,&sptr);
1231 if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;
1232 items--;
1233 }
1234 } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
1235 zset *zs = (zset*)ptrFromObj(o);
1236 dictIterator *di = dictGetIterator(zs->dict);
1237 dictEntry *de;
1238
1239 while((de = dictNext(di)) != NULL) {
1240 sds ele = (sds)dictGetKey(de);
1241 double *score = (double*)dictGetVal(de);
1242
1243 if (count == 0) {
1244 int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?
1245 AOF_REWRITE_ITEMS_PER_CMD : items;
1246
1247 if (!rioWriteBulkCount(r,'*',2+cmd_items*2) ||
1248 !rioWriteBulkString(r,"ZADD",4) ||
1249 !rioWriteBulkObject(r,key))
1250 {

Callers 1

rewriteAppendOnlyFileRioFunction · 0.85

Calls 15

zsetLengthFunction · 0.85
ptrFromObjFunction · 0.85
ziplistIndexFunction · 0.85
ziplistNextFunction · 0.85
ziplistGetFunction · 0.85
zzlGetScoreFunction · 0.85
rioWriteBulkCountFunction · 0.85
rioWriteBulkStringFunction · 0.85
rioWriteBulkObjectFunction · 0.85
rioWriteBulkDoubleFunction · 0.85
rioWriteBulkLongLongFunction · 0.85
zzlNextFunction · 0.85

Tested by

no test coverage detected