Ejemplo n.º 1
0
void SetupRedisGlobals(LPVOID redisData, size_t redisDataSize, uint32_t dictHashSeed)
{
#ifndef NO_QFORKIMPL
    memcpy(&server, redisData, redisDataSize);
    dictSetHashFunctionSeed(dictHashSeed);
#endif
}
Ejemplo n.º 2
0
Archivo: redis.c Proyecto: abgood/redis
int main (int argc, char **argv) {
    struct timeval tv;

    // 初始化相关环境变量与库
#ifdef INIT_SETPROCTITLE_REPLACEMENT
    spt_init(argc, argv);
#endif

    // 设置当前的`locale`命令里的内容
    setlocale(LC_COLLATE, "");

    // 自动分配线程安全
    zmalloc_enable_thread_safeness();

    // 设置自动分配函数句柄(out of memory)
    zmalloc_set_oom_handler(redisOutOfMemoryHandler);

    // 时间异或pid生成随机数种子
    srand(time(NULL) ^ getpid());
    gettimeofday(&tv, NULL);

    // 设置字典hash值种子
    dictSetHashFunctionSeed(tv.tv_sec ^ tv.tv_usec ^ getpid());

    // 定点模式检测
    server.sentinel_mode = checkForSentinelMode(argc, argv);

    // 初始化服务配置
    initServerConfig();

    // 定点模式处理
    if (server.sentinel_mode) {
        printf("deal with sentinel mode\n");
    }

    if (argc >= 2) {
        int j = 1;
        // init empty string
        sds options = sdsempty();
        char *configfile = NULL;

        if (strcmp(argv[1], "-v") == 0 ||
            strcmp(argv[1], "--version") == 0) version();
        if (strcmp(argv[1], "--help") == 0 ||
            strcmp(argv[1], "-h") == 0) usage();
        if (strcmp(argv[1], "--test-memory") == 0) {
            if (argc == 3) {
                // 内存测试还未完全写完
                memtest(atoi(argv[2]), 50);
                exit(0);
            } else {
                fprintf(stderr, "Please specify the amount of memory to test in megabytes.\n");
                fprintf(stderr, "Example: ./redis-server --test-memory 4096\n\n");
                exit(1);
            }
        }

        /*
         * 第一个参数为'--'开头, j始终为1
         * 第一个参数不为'--'开头, j始终为2
         *
         */

        // j为1,第1个参数不是'--'开头的是配置文件
        if (argv[j][0] != '-' || argv[j][1] != '-') {
            configfile = argv[j++];
        }

        // 配置文件参数后还有内容
        while (j != argc) {
            // 所有参数有包括'--'开头的参数
            if (argv[j][0] == '-' && argv[j][1] == '-') {
                if (sdslen(options)) options = sdscat(options, "\n");
                options = sdscat(options, " ");
            } else {
                // append new string, 转义特殊字符, return new pointer
                options = sdscatrepr(options, argv[j], strlen(argv[j]));
                options = sdscat(options, " ");
            }
            j++;
        }

        if (server.sentinel_mode && configfile && *configfile == '-') {
            printf("need redis log\n");
            exit(1);
        }

        if (configfile) {
            server.configfile = getAbsolutePath(configfile);
        }
        resetServerSaveParams();
        loadServerConfig(configfile, options);
        sdsfree(options);
    } else {
        printf("warning: no config file specified\n");
    }

    // 后台模式
    if (server.daemonize) {
        daemonize();
    }

    // init server
    initServer();

    printf("redis done\n");
    return 0;
}
Ejemplo n.º 3
0
/*
 * main function.
 */
int main()
{
    struct timeval tv;

    srand(time(NULL)^getpid());
    gettimeofday(&tv,NULL);


    // rand seed or fixed seed.
    unsigned int mod = tv.tv_sec^tv.tv_usec^getpid(); 
    printf("%u\n", mod);
    //dictSetHashFunctionSeed(mod);
    dictSetHashFunctionSeed(12391);



    // create <k,v> = <str, str>.
    dictType myType = {
        myHash,                 /* hash function */
        NULL,                   /* key dup */
        NULL,                   /* val dup */
        myCompare,              /* key compare */
        myDestructor,           /* key destructor */
        NULL                    /* val destructor */
    };


    // step 1: create.
    dict* myDict = dictCreate(&myType, NULL);
    assert(myDict != NULL);

    printf("-------------------\n");
    printState(myDict);

    char* key[10] = {"hello0", "hello1", "hello2", "hello3", "hello4", 
                "hello5", "hello6", "hello7", "hello8", "hello9"};

    char* val[10] = {"h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"};


    for(int i=0; i<10; i++)
    {
        unsigned int hash = myHash(key[i]);
        unsigned int idx = hash & 3;
        printf("real key: %u, real idx=%d\n", hash, idx);
    }



    // step 2: add
    printf("----------add first 5-----------------\n");
    for(int i = 0; i<5; i++)
    {
        printf("add %d\n", i);
        int ret = dictAdd(myDict, key[i], val[i]);
        printState(myDict);
        assert(ret==0);
    }

    printf("----------start rehashing..------------\n");
    for(int i=0; i<5; i++)
    {
        dictRehash(myDict, 1);
        printState(myDict);
    }

    printf("----------add  last 5.-----------------\n");
    for(int i = 5; i<10; i++)
    {
        printf("add %d\n", i);
        int ret = dictAdd(myDict, key[i], val[i]);
        printState(myDict);
        assert(ret==0);
    }




    // index.
    printf("------------index---------------\n");
    for(int i = 0; i < 10; i++)
    {
        printf("i=%d\n", i);
        char* v = dictFetchValue(myDict, key[i]);
        int ret = strcmp(v, val[i]); 
        assert(ret == 0);
    }

    char* v = dictFetchValue(myDict, "hello world2");
    assert(v == NULL);
    

    // foreach dict.
    unsigned long cur = 0;
    while(1) 
    {
        cur = dictScan(myDict, cur, dictScanCallback, NULL); 
        if(cur == 0)
        {
            break;
        }
    }

    // release. 
    dictRelease(myDict);

    return 0;
}