• Memcached源码分析——hash


    以下为memcached中关于使用的hash算法的一点记录

    memcached中默认使用的是Bob Jenkins的jenkins_hash算法

    以下4段代码均在memcached-1.4.22/hash.h

    以下为hash函数的声明,hashfunc_type的定义,以及hash_init函数的声明

     /**
     * 定义一个函数指针类型
     */
    typedef uint32_t (*hash_func)(const void *key, size_t length);
    
    /**
     * 定义函数指针为hash
     */
    hash_func hash;
    
    /**
     * 定义枚举类型,其中JENKINS_HASH=0,MURMUR3_HASH=1
     */
    enum hashfunc_type {
        JENKINS_HASH=0, MURMUR3_HASH
    };
    
    /**
     * 声明hash初始化函数 hash_init
     */
    int hash_init(enum hashfunc_type type);
    /**
     * 判断:如果hashfunc_type为0,则hash指针指向函数jenkins_hash,并返回0;
     *       如果hashfunc_type为1,则hash指针指向函数MurmurHash3_x86_32,并返回0;
     *       否则,返回-1
     * 以下代码在memcached-1.4.22/hash.c
     */
    int hash_init(enum hashfunc_type type) {
        switch(type) {
            case JENKINS_HASH:
                hash = jenkins_hash;
                settings.hash_algorithm = "jenkins";
                break;
            case MURMUR3_HASH:
                hash = MurmurHash3_x86_32;
                settings.hash_algorithm = "murmur3";
                break;
            default:
                return -1;
        }
        return 0;
    }
    /**
     * hash初始化在memcached-1.4.22/memcached.c 中的main函数第5413到5416行进行的
     */
        if (hash_init(hash_type) != 0) {
            fprintf(stderr, "Failed to initialize hash_algorithm!
    ");
            exit(EX_USAGE);
        }
        
    /**
     * jenkins_hash函数的声明在memcached-1.4.22/jenkins_hash.h中。
     * 详细定义在memcached-1.4.22/jenkins_hash.c中
     */
  • 相关阅读:
    【原】list<T>排序
    [原]unity3d刀光剑影(二)
    [原]unity3D bug记录
    [原]重要工具集
    [原]IOS 后台发送邮件
    [转]IOS 崩溃日志大全(二)
    动态二维数组的建立
    struts 学习
    sizeof()与strlen()的区别
    struts配置。泪奔...
  • 原文地址:https://www.cnblogs.com/lrxing/p/4273333.html
Copyright © 2020-2023  润新知