Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/RJ/ketama
- Loading branch information
Showing
5 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
CC = gcc | ||
|
||
SRCNAME = lketama.c | ||
OBJNAME = lketama.o | ||
LIBNAME = ketama.so | ||
|
||
OMIT_FRAME_POINTER = -fomit-frame-pointer | ||
CFLAGS = -I/usr/local/include/ -fPIC -O3 -Wall | ||
LIBS = lketama | ||
LFLAGS = -shared $(OMIT_FRAME_POINTER) | ||
INSTALL_PATH = usr/local/lib/lua/5.1 | ||
|
||
all: $(LIBNAME) | ||
|
||
$(OBJNAME): $(SRCNAME) | ||
$(CC) -o $(OBJNAME) -c $(SRCNAME) $(CFLAGS) | ||
|
||
$(LIBNAME): $(OBJNAME) | ||
$(CC) -o $(LIBNAME) -shared $(OBJNAME) $(LFLAGS) -$(LIBS) | ||
|
||
install: $(LIBNAME) | ||
install -D -s $(LIBNAME) $(DESTDIR)/$(INSTALL_PATH)/$(LIBNAME) | ||
|
||
clean: | ||
rm -f $(LIBNAME) $(OBJNAME) | ||
|
||
.PHONY: clean | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
|
||
A Lua binding to libketama. | ||
|
||
Copyright (c) 2009, Phoenix Sol - http://github.com/phoenixsol | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
|
||
Review 'test.lua' for usage example. | ||
Review 'lketama.c', and provide feedback to help me grow. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
A Lua binding to libketama. | ||
Copyright (c) 2009, Phoenix Sol - http://github.com/phoenixsol | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include <string.h> | ||
#include <errno.h> | ||
#include <lua.h> | ||
#include <lauxlib.h> | ||
#include <ketama.h> | ||
#define LIB_NAME "ketama" | ||
#define MT_NAME "KETAMA_MT" | ||
|
||
|
||
typedef struct{ | ||
ketama_continuum *cont; | ||
} contdata; | ||
|
||
static int | ||
nil_error(lua_State *L, int code, const char *msg){ | ||
lua_pushnil(L); | ||
lua_pushstring(L, msg); | ||
lua_pushinteger(L, code); | ||
return 3; | ||
} | ||
|
||
static int | ||
bool_error(lua_State *L, int code, const char *msg){ | ||
lua_pushboolean(L, 0); | ||
lua_pushstring(L, msg); | ||
lua_pushinteger(L, code); | ||
return 3; | ||
} | ||
|
||
static int | ||
default_error(lua_State *L){ | ||
return nil_error(L, errno, strerror(errno)); | ||
} | ||
|
||
static int | ||
lketama_error(lua_State *L){ | ||
return bool_error(L, 0, ketama_error()); | ||
} | ||
|
||
static int | ||
lketama_roll(lua_State *L){ | ||
size_t fsiz; | ||
char *filename = (char *)luaL_checklstring(L, 1, &fsiz); | ||
if( fsiz > 255 ) return luaL_argerror(L, 1, "filename must not be longer than 255 bytes"); | ||
contdata *data = lua_newuserdata(L, sizeof(*data)); | ||
if(!data) return default_error(L); | ||
if(ketama_roll((ketama_continuum*)&data->cont, filename) == 0) return nil_error(L, 0, "continuum fail"); | ||
luaL_getmetatable(L, MT_NAME); | ||
lua_setmetatable(L, -2); | ||
return 1; | ||
} | ||
|
||
ketama_continuum* | ||
lketama_get(lua_State *L, int index){ | ||
contdata *data = luaL_checkudata(L, index, MT_NAME); | ||
return (ketama_continuum *)data->cont; | ||
} | ||
|
||
static int | ||
lketama_smoke(lua_State *L){ | ||
contdata *data = luaL_checkudata(L, 1, MT_NAME); | ||
if(data->cont){ | ||
ketama_smoke((ketama_continuum)data->cont); | ||
data->cont = NULL; | ||
} | ||
return 0; | ||
} | ||
|
||
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; | ||
} | ||
|
||
static int | ||
lketama_print_continuum(lua_State *L){ | ||
ketama_continuum *cont = lketama_get(L, 1); | ||
ketama_print_continuum((ketama_continuum)cont); | ||
return 0; | ||
} | ||
|
||
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; | ||
} | ||
|
||
static int | ||
lketama_hashi(lua_State *L){ | ||
const char *str = luaL_checkstring(L, 2); | ||
unsigned int res = ketama_hashi((char*)str); | ||
lua_pushnumber(L, res); | ||
return 1; | ||
} | ||
|
||
static int | ||
lketama_md5digest(lua_State *L){ | ||
const char *str = luaL_checkstring(L, 2); | ||
unsigned char md5pword[16]; | ||
ketama_md5_digest((char*)str, md5pword); | ||
lua_pushfstring(L, "%p", md5pword); | ||
return 1; | ||
} | ||
|
||
static luaL_reg pfuncs[] = { {"roll", lketama_roll}, {NULL, NULL} }; | ||
static luaL_reg mmethods[] = { | ||
{"smoke", lketama_smoke}, | ||
{"get_server", lketama_get_server}, | ||
{"print_continuum", lketama_print_continuum}, | ||
{"compare", lketama_compare}, | ||
{"hashi", lketama_hashi}, | ||
{"md5digest", lketama_md5digest}, | ||
{NULL, NULL} | ||
}; | ||
|
||
#define register_constant(s) lua_pushinteger(L,s); lua_setfield(L, -2, #s); | ||
int luaopen_ketama(lua_State *L){ | ||
luaL_newmetatable(L, MT_NAME); | ||
lua_createtable(L, 0, sizeof(mmethods) / sizeof(luaL_reg) -1); | ||
luaL_register(L, NULL, mmethods); | ||
lua_setfield(L, -2, "__index"); | ||
|
||
lua_pushcfunction(L, lketama_smoke); | ||
lua_setfield(L, -2, "__gc"); | ||
lua_pop(L, 1); | ||
|
||
luaL_register(L, LIB_NAME, pfuncs); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--[[ | ||
Copyright (c) 2009, Phoenix Sol - http://github.com/phoenixsol | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
]]-- | ||
|
||
local ketama = require 'ketama' | ||
|
||
local ket = ketama.roll('../ketama.servers') | ||
--ket:print_continuum() --don't like! | ||
|
||
local function compare(ket, a, b) | ||
--return the greater, or nil if they are equal | ||
local result = ket:compare(a,b) | ||
if result == 0 then return nil, 'equal' | ||
elseif result == 1 then return a | ||
else return b end | ||
end | ||
|
||
print("server for 'foo':", ket:get_server('foo')) | ||
print("greater of 'foo' and 'bar':", compare(ket, 'foo', 'bar')) | ||
print("hashi of 'foo':", ket:hashi('foo')) | ||
print("md5 of 'foo':", ket:md5digest('foo')) | ||
|
||
ket:smoke() | ||
print("closed") | ||
collectgarbage("collect") | ||
print("kthxbye") |