| 1171 | } |
| 1172 | |
| 1173 | void 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 = zobj->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 = zmalloc(sizeof(*zs)); |
| 1191 | zs->dict = dictCreate(&zsetDictType,NULL); |
| 1192 | zs->zsl = zslCreate(); |
| 1193 | |
| 1194 | eptr = ziplistIndex(zl,0); |
| 1195 | serverAssertWithInfo(NULL,zobj,eptr != NULL); |
| 1196 | sptr = ziplistNext(zl,eptr); |
| 1197 | serverAssertWithInfo(NULL,zobj,sptr != NULL); |
| 1198 | |
| 1199 | while (eptr != NULL) { |
| 1200 | score = zzlGetScore(sptr); |
| 1201 | serverAssertWithInfo(NULL,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong)); |
| 1202 | if (vstr == NULL) |
| 1203 | ele = sdsfromlonglong(vlong); |
| 1204 | else |
| 1205 | ele = sdsnewlen((char*)vstr,vlen); |
| 1206 | |
| 1207 | node = zslInsert(zs->zsl,score,ele); |
| 1208 | serverAssert(dictAdd(zs->dict,ele,&node->score) == DICT_OK); |
| 1209 | zzlNext(zl,&eptr,&sptr); |
| 1210 | } |
| 1211 | |
| 1212 | zfree(zobj->ptr); |
| 1213 | zobj->ptr = zs; |
| 1214 | zobj->encoding = OBJ_ENCODING_SKIPLIST; |
| 1215 | } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { |
| 1216 | unsigned char *zl = ziplistNew(); |
| 1217 | |
| 1218 | if (encoding != OBJ_ENCODING_ZIPLIST) |
| 1219 | serverPanic("Unknown target encoding"); |
| 1220 | |
| 1221 | /* Approach similar to zslFree(), since we want to free the skiplist at |
| 1222 | * the same time as creating the ziplist. */ |
| 1223 | zs = zobj->ptr; |
| 1224 | dictRelease(zs->dict); |
| 1225 | node = zs->zsl->header->level[0].forward; |
| 1226 | zfree(zs->zsl->header); |
| 1227 | zfree(zs->zsl); |
| 1228 | |
| 1229 | while (node) { |
| 1230 | zl = zzlInsertAt(zl,NULL,node->ele,node->score); |
no test coverage detected