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

Function zsetConvert

src/t_zset.cpp:1173–1244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1171}
1172
1173void zsetConvert(robj *zobj, int encoding) {
1174 zset *zs;
1175 zskiplistNode *node, *next;
1176 sds ele;
1177 double score;
1178
1179 if (zobj->encoding == encoding) return;
1180 if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
1181 unsigned char *zl = (unsigned char*)zobj->m_ptr;
1182 unsigned char *eptr, *sptr;
1183 unsigned char *vstr;
1184 unsigned int vlen;
1185 long long vlong;
1186
1187 if (encoding != OBJ_ENCODING_SKIPLIST)
1188 serverPanic("Unknown target encoding");
1189
1190 zs = (zset*)zmalloc(sizeof(*zs), MALLOC_SHARED);
1191 zs->dict = dictCreate(&zsetDictType,NULL);
1192 zs->zsl = zslCreate();
1193
1194 if (ziplistLen(zl) > 0) {
1195 eptr = ziplistIndex(zl,0);
1196 serverAssertWithInfo(NULL,zobj,eptr != NULL);
1197 sptr = ziplistNext(zl,eptr);
1198 serverAssertWithInfo(NULL,zobj,sptr != NULL);
1199
1200 while (eptr != NULL) {
1201 score = zzlGetScore(sptr);
1202 serverAssertWithInfo(NULL,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
1203 if (vstr == NULL)
1204 ele = sdsfromlonglong(vlong);
1205 else
1206 ele = sdsnewlen((char*)vstr,vlen);
1207
1208 node = zslInsert(zs->zsl,score,ele);
1209 serverAssert(dictAdd(zs->dict,ele,&node->score) == DICT_OK);
1210 zzlNext(zl,&eptr,&sptr);
1211 }
1212 }
1213
1214 zfree(zobj->m_ptr);
1215 zobj->m_ptr = zs;
1216 zobj->encoding = OBJ_ENCODING_SKIPLIST;
1217 } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
1218 unsigned char *zl = ziplistNew();
1219
1220 if (encoding != OBJ_ENCODING_ZIPLIST)
1221 serverPanic("Unknown target encoding");
1222
1223 /* Approach similar to zslFree(), since we want to free the skiplist at
1224 * the same time as creating the ziplist. */
1225 zs = (zset*)zobj->m_ptr;
1226 dictRelease(zs->dict);
1227 node = zs->zsl->header->level(0)->forward;
1228 zfree(zs->zsl->header);
1229 zfree(zs->zsl);
1230

Callers 4

sortCommandFunction · 0.85
rdbLoadObjectFunction · 0.85
zsetAddFunction · 0.85

Calls 15

zmallocFunction · 0.85
zslCreateFunction · 0.85
ziplistLenFunction · 0.85
ziplistIndexFunction · 0.85
ziplistNextFunction · 0.85
zzlGetScoreFunction · 0.85
ziplistGetFunction · 0.85
sdsfromlonglongFunction · 0.85
sdsnewlenFunction · 0.85
zslInsertFunction · 0.85
zzlNextFunction · 0.85
zfreeFunction · 0.85

Tested by

no test coverage detected