static int lketama_compare(lua_State *L){ ketama_continuum *cont = lketama_get(L, 1); const char *ipa = luaL_checkstring(L, 2); const char *ipb = luaL_checkstring(L, 3); mcs *mcsa = ketama_get_server((char*)ipa, (ketama_continuum)cont); mcs *mcsb = ketama_get_server((char*)ipb, (ketama_continuum)cont); int res = ketama_compare(mcsa, mcsb); lua_pushnumber(L, res); return 1; }
int main(int argc, char **argv) { if(argc==1){ printf("Usage: %s <ketama.servers file>\n", *argv); return 1; } ketama_continuum c; serverinfo* serverinfo; ketama_roll( &c, *++argv, serverinfo ); printf( "%s\n", ketama_error() ); int i; for ( i = 0; i < 1000000; i++ ) { char k[10]; sprintf( k, "%d", i ); unsigned int kh = ketama_hashi( k ); mcs* m = ketama_get_server( k, c ); printf( "%u %u %s\n", kh, m->point, m->ip ); } ketama_smoke(c); return 0; }
static int lketama_info(lua_State *L){ ketama_continuum *cont = lketama_get(L, 1); if(cont){ lua_pushstring(L, ketama_info((ketama_continuum)cont)); } FILE *f = fopen("../lua_test.out", "w"); char node_names[1000]; sprintf(node_names, "%s", "node1:1000,node2:1000,node3:1000,node4:1000"); sync_servers(node_names, (ketama_continuum)cont ); int i=0; for ( i = 0; i < 100; i++ ) { char k[10]; char output[20]; snprintf(k, sizeof(k), "%s%i", "aab", i); mcs* m = ketama_get_server(k, (ketama_continuum)cont ); sprintf(output, "%s - %s\n", m->ip, k ); fputs(output, f); } fclose(f); printf("output file saved\n"); return 1; }
static int lketama_get_server(lua_State *L){ ketama_continuum *cont = lketama_get(L, 1); char *key = (char *)luaL_checkstring(L, 2); mcs *result = ketama_get_server(key, (ketama_continuum)cont); lua_pushstring(L, result->ip); lua_pushnumber(L, result->point); return 2; }
PY_DEF(pyketama_Continuum_get_server, self, args) { char *key; mcs *r; PyObject *ret = NULL; if (PyArg_ParseTuple(args, "s", &key)) { /* ketama_get_server simply can not fail, so there's no point in * error checking. */ r = ketama_get_server(key, ((pyketama_Continuum *)self)->cont); ret = Py_BuildValue("Is", r->point, r->ip); } return ret; }