Skip to content

how to assert a key is exist in a sorted set ? #792

Closed

Description

@493439861

There is a question,I want to determine a key is exist in a sorted set ?

How Can I do?

I am appreciate for your answer!

Activity

  1. AllenDou commented on Nov 26, 2012

    @AllenDou

    maybe you mean the member of the sorted set.
    use command below:
    $ zrank key member.

  2. kgcrom commented on Nov 26, 2012

    @kgcrom

    Or you can write code under the example

    //t_zset.c 889 lines
    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
                unsigned char *eptr;
    
                /* Prefer non-encoded element when dealing with ziplists. */
                ele = c->argv[3+j*2];
                if ((eptr = zzlFind(zobj->ptr,ele,&curscore)) != NULL) {
                   // assert~~~~~~!!!!! exist key
                } else {
                    /* Optimize: check if the element is too large or the list
                     * becomes too long *before* executing zzlInsert. */
                    zobj->ptr = zzlInsert(zobj->ptr,ele,score);
                    if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries)
                        zsetConvert(zobj,REDIS_ENCODING_SKIPLIST);
                    if (sdslen(ele->ptr) > server.zset_max_ziplist_value)
                        zsetConvert(zobj,REDIS_ENCODING_SKIPLIST);
    
                    signalModifiedKey(c->db,key);
                    server.dirty++;
                    if (!incr) added++;
                }
            } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
                zset *zs = zobj->ptr;
                zskiplistNode *znode;
                dictEntry *de;
    
                ele = c->argv[3+j*2] = tryObjectEncoding(c->argv[3+j*2]);
                de = dictFind(zs->dict,ele);
                if (de != NULL) {
                    // assert~~~~~~!!!!! exist key
                } else {
                    znode = zslInsert(zs->zsl,score,ele);
                    incrRefCount(ele); /* Inserted in skiplist. */
                    redisAssertWithInfo(c,NULL,dictAdd(zs->dict,ele,&znode->score) == DICT_OK);
                    incrRefCount(ele); /* Added to dictionary. */
    
                    signalModifiedKey(c->db,key);
                    server.dirty++;
                    if (!incr) added++;
                }
            }

    and issues are not for questions ;)
    Please use the Redis DB google group
    thx~ : )

  3. added a commit that references this issue on Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions