This is a helper function for the COPY command. * Duplicate a sorted set object, with the guarantee that the returned object * has the same encoding as the original one. * * The resulting object always has refcount set to 1 */
| 1577 | * |
| 1578 | * The resulting object always has refcount set to 1 */ |
| 1579 | robj *zsetDup(robj *o) { |
| 1580 | robj *zobj; |
| 1581 | zset *zs; |
| 1582 | zset *new_zs; |
| 1583 | |
| 1584 | serverAssert(o->type == OBJ_ZSET); |
| 1585 | |
| 1586 | /* Create a new sorted set object that have the same encoding as the original object's encoding */ |
| 1587 | if (o->encoding == OBJ_ENCODING_ZIPLIST) { |
| 1588 | unsigned char *zl = (unsigned char*)ptrFromObj(o); |
| 1589 | size_t sz = ziplistBlobLen(zl); |
| 1590 | unsigned char *new_zl = (unsigned char*)zmalloc(sz); |
| 1591 | memcpy(new_zl, zl, sz); |
| 1592 | zobj = createObject(OBJ_ZSET, new_zl); |
| 1593 | zobj->encoding = OBJ_ENCODING_ZIPLIST; |
| 1594 | } else if (o->encoding == OBJ_ENCODING_SKIPLIST) { |
| 1595 | zobj = createZsetObject(); |
| 1596 | zs = (zset*)ptrFromObj(o); |
| 1597 | new_zs = (zset*)ptrFromObj(zobj); |
| 1598 | dictExpand(new_zs->dict,dictSize(zs->dict)); |
| 1599 | zskiplist *zsl = zs->zsl; |
| 1600 | zskiplistNode *ln; |
| 1601 | sds ele; |
| 1602 | long llen = zsetLength(o); |
| 1603 | |
| 1604 | /* We copy the skiplist elements from the greatest to the |
| 1605 | * smallest (that's trivial since the elements are already ordered in |
| 1606 | * the skiplist): this improves the load process, since the next loaded |
| 1607 | * element will always be the smaller, so adding to the skiplist |
| 1608 | * will always immediately stop at the head, making the insertion |
| 1609 | * O(1) instead of O(log(N)). */ |
| 1610 | ln = zsl->tail; |
| 1611 | while (llen--) { |
| 1612 | ele = ln->ele; |
| 1613 | sds new_ele = sdsdup(ele); |
| 1614 | zskiplistNode *znode = zslInsert(new_zs->zsl,ln->score,new_ele); |
| 1615 | dictAdd(new_zs->dict,new_ele,&znode->score); |
| 1616 | ln = ln->backward; |
| 1617 | } |
| 1618 | } else { |
| 1619 | serverPanic("Unknown sorted set encoding"); |
| 1620 | } |
| 1621 | return zobj; |
| 1622 | } |
| 1623 | |
| 1624 | struct zset_validate_data { |
| 1625 | long count; |
no test coverage detected