libredis/ketama.c
void Ketama_create_continuum(Ketama *ketama)
{
	if (ketama->numservers == 0 || ketama->continuum) {
		DEBUG(("Ketama_create_continuum called in incorrect state"));
		return;
	}

	DEBUG(("Server definitions read: %u servers, total memory: %lu.\n", ketama->numservers, ketama->memory ));

    /* Continuum will hold one mcs for each point on the circle: */
    ketama->continuum = Alloc_alloc(ketama->numservers * sizeof(mcs) * 160);
    unsigned k, cont = 0;

    for(int i = 0; i < ketama->numservers; i++)
    {
        serverinfo *sinfo = &(ketama->servers[i]);
    	float pct = (float)sinfo->memory / (float)ketama->memory;
        unsigned int ks = floorf( pct * 40.0 * (float)ketama->numservers );
#ifndef NDEBUG
        int hpct = floorf( pct * 100.0 );
        DEBUG(("Server no. %d: %s (mem: %lu = %u%% or %d of %d)\n", i, sinfo->addr, sinfo->memory, hpct, ks, ketama->numservers * 40 ));
#endif

        for( k = 0; k < ks; k++ )
        {
            /* 40 hashes, 4 numbers per hash = 160 points per server */
            char ss[ADDR_SIZE + 11];
            unsigned char digest[16];

            int len = snprintf( ss, sizeof(ss), "%s-%d", sinfo->addr, k );
            if (len > sizeof(ss) - 1)
            	len = sizeof(ss) - 1;
            Ketama_md5_digest( ss, len, digest );

            /* Use successive 4-bytes from hash as numbers 
             * for the points on the circle: */
            int h;
            for( h = 0; h < 4; h++ )
            {
                ketama->continuum[cont].point = ( digest[3+h*4] << 24 )
                                      | ( digest[2+h*4] << 16 )
                                      | ( digest[1+h*4] <<  8 )
                                      |   digest[h*4];

				ketama->continuum[cont].ordinal = i;
                cont++;
            }
        }
    }

    DEBUG(("cont: %d\n", cont));
    ketama->numpoints = cont;
    //free( slist );

    /* Sorts in ascending order of "point" */
    qsort( (void*) ketama->continuum, cont, sizeof( mcs ), (compfn)Ketama_compare );

}
/** \brief Generates the continuum of servers (each server as many points on a circle).
  * \param key Shared memory key for storing the newly created continuum.
  * \param filename Server definition file, which will be parsed to create this continuum.
  * \return 0 on failure, 1 on success. */
\brief Generates the continuum of servers (each server as many points on a circle).
\param key Shared memory key for storing the newly created continuum.
\param filename Server definition file, which will be parsed to create this continuum.
\return 0 on failure, 1 on success.
\ brief Generates the continuum of servers ( each server as many points on a circle ) . \ param key Shared memory key for storing the newly created continuum . \ param filename Server definition file which will be parsed to create this continuum . \ return 0 on failure 1 on success .
void Ketama_create_continuum(Ketama *ketama)
{
	if (ketama->numservers == 0 || ketama->continuum) {
		DEBUG(("Ketama_create_continuum called in incorrect state"));
		return;
	}
	DEBUG(("Server definitions read: %u servers, total memory: %lu.\n", ketama->numservers, ketama->memory ));
    ketama->continuum = Alloc_alloc(ketama->numservers * sizeof(mcs) * 160);
    unsigned k, cont = 0;
    for(int i = 0; i < ketama->numservers; i++)
    {
        serverinfo *sinfo = &(ketama->servers[i]);
    	float pct = (float)sinfo->memory / (float)ketama->memory;
        unsigned int ks = floorf( pct * 40.0 * (float)ketama->numservers );
#ifndef NDEBUG
        int hpct = floorf( pct * 100.0 );
        DEBUG(("Server no. %d: %s (mem: %lu = %u%% or %d of %d)\n", i, sinfo->addr, sinfo->memory, hpct, ks, ketama->numservers * 40 ));
#endif
        for( k = 0; k < ks; k++ )
        {
            char ss[ADDR_SIZE + 11];
            unsigned char digest[16];
            int len = snprintf( ss, sizeof(ss), "%s-%d", sinfo->addr, k );
            if (len > sizeof(ss) - 1)
            	len = sizeof(ss) - 1;
            Ketama_md5_digest( ss, len, digest );
            int h;
            for( h = 0; h < 4; h++ )
            {
                ketama->continuum[cont].point = ( digest[3+h*4] << 24 )
                                      | ( digest[2+h*4] << 16 )
                                      | ( digest[1+h*4] <<  8 )
                                      |   digest[h*4];
				ketama->continuum[cont].ordinal = i;
                cont++;
            }
        }
    }
    DEBUG(("cont: %d\n", cont));
    ketama->numpoints = cont;
    qsort( (void*) ketama->continuum, cont, sizeof( mcs ), (compfn)Ketama_compare );
}
void Ketama_create_continuum ( Ketama * ketama ) { if ( ketama -> numservers == 0 || ketama -> continuum ) { DEBUG ( ( " " ) ) ; return ; } DEBUG ( ( " \n " , ketama -> numservers , ketama -> memory ) ) ; ketama -> continuum = Alloc_alloc ( ketama -> numservers * sizeof ( mcs ) * 160 ) ; unsigned k , cont = 0 ; for ( int i = 0 ; i < ketama -> numservers ; i ++ ) { serverinfo * sinfo = & ( ketama -> servers [ i ] ) ; float pct = ( float ) sinfo -> memory / ( float ) ketama -> memory ; unsigned int ks = floorf ( pct * 40.0 * ( float ) ketama -> numservers ) ; #ifndef NDEBUG int hpct = floorf ( pct * 100.0 ) ; DEBUG ( ( " \n " , i , sinfo -> addr , sinfo -> memory , hpct , ks , ketama -> numservers * 40 ) ) ; #endif for ( k = 0 ; k < ks ; k ++ ) { char ss [ ADDR_SIZE + 11 ] ; unsigned char digest [ 16 ] ; int len = snprintf ( ss , sizeof ( ss ) , " " , sinfo -> addr , k ) ; if ( len > sizeof ( ss ) - 1 ) len = sizeof ( ss ) - 1 ; Ketama_md5_digest ( ss , len , digest ) ; int h ; for ( h = 0 ; h < 4 ; h ++ ) { ketama -> continuum [ cont ] . point = ( digest [ 3 + h * 4 ] << 24 ) | ( digest [ 2 + h * 4 ] << 16 ) | ( digest [ 1 + h * 4 ] << 8 ) | digest [ h * 4 ] ; ketama -> continuum [ cont ] . ordinal = i ; cont ++ ; } } } DEBUG ( ( " \n " , cont ) ) ; ketama -> numpoints = cont ; qsort ( ( void * ) ketama -> continuum , cont , sizeof ( mcs ) , ( compfn ) Ketama_compare ) ; }
\brief Generates the continuum of servers (each server as many points on a circle).
\ brief Generates the continuum of servers ( each server as many points on a circle ) .
/* Continuum will hold one mcs for each point on the circle: */ /* 40 hashes, 4 numbers per hash = 160 points per server */ /* Use successive 4-bytes from hash as numbers 
             * for the points on the circle: */ //free( slist ); /* Sorts in ascending order of "point" */