Beispiel #1
0
/* Rehash for an amount of time between ms milliseconds and ms+1 milliseconds */
int dictRehashMilliseconds(dict *d, int ms) {
    long long start = timeInMilliseconds();
    int rehashes = 0;

    while(dictRehash(d,100)) {
        rehashes += 100;
        if (timeInMilliseconds()-start > ms) break;
    }
    return rehashes;
}
Beispiel #2
0
/* This function performs just a step of rehashing, and only if there are
 * no safe iterators bound to our hash table. When we have iterators in the
 * middle of a rehashing we can't mess with the two hash tables otherwise
 * some element can be missed or duplicated.
 *
 * This function is called by common lookup or update operations in the
 * dictionary so that the hash table automatically migrates from H1 to H2
 * while it is actively used. */
static void _dictRehashStep(dict *d) {
    if (d->iterators == 0) dictRehash(d,1);
}
Beispiel #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;
}