zoukankan      html  css  js  c++  java
  • 源码解读 zsetAdd

    https://github.com/antirez/redis/blob/6a6471aad5e4f8d6cbab677b918b14cdee416296/src/t_zset.c

        /* Update the sorted set according to its encoding. */
        if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
            unsigned char *eptr;
    
            if ((eptr = zzlFind(zobj->ptr,ele,&curscore)) != NULL) {
                /* NX? Return, same element already exists. */
                if (nx) {
                    *flags |= ZADD_NOP;
                    return 1;
                }
    
                /* Prepare the score for the increment if needed. */
                if (incr) {
                    score += curscore;
                    if (isnan(score)) {
                        *flags |= ZADD_NAN;
                        return 0;
                    }
                    if (newscore) *newscore = score;
                }
    
                /* Remove and re-insert when score changed. */
                if (score != curscore) {
                    zobj->ptr = zzlDelete(zobj->ptr,eptr);
                    zobj->ptr = zzlInsert(zobj->ptr,ele,score);
                    *flags |= ZADD_UPDATED;
                }
                return 1;
            } else if (!xx) {
                /* 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,OBJ_ENCODING_SKIPLIST);
                if (sdslen(ele) > server.zset_max_ziplist_value)
                    zsetConvert(zobj,OBJ_ENCODING_SKIPLIST);
                if (newscore) *newscore = score;
                *flags |= ZADD_ADDED;
                return 1;
            } else {
                *flags |= ZADD_NOP;
                return 1;
            }
    

      

  • 相关阅读:
    UICollectionView的简单使用(一)
    天气预报接口IOS版OC:SmartWeather API中key的计算方法
    IOS下Base64加密
    IOS下DES加密
    IOS的URL中文转码
    CTE Recursion Performance
    走过而立之年的Coder
    iOS多线程编程之锁的理解
    iOS设置PCH文件
    程序员:伤不起的三十岁
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10240962.html
Copyright © 2011-2022 走看看