-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshard.lua
39 lines (34 loc) · 1.02 KB
/
shard.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
local _SHARD = {
}
local ffi = require "ffi"
ffi.cdef[[
typedef struct { char *name; int weight; } shard;
typedef struct { void* tree_map; shard** shards; int shard_size;} shard_t;
void shard_init(shard_t* shart_t,shard* shards[],int shard_size);
shard* shard_select(shard_t* shard_t, char *redis_key);
]]
_SHARD.initialized = false
_SHARD.pshard_t={}
function _SHARD.select(key)
local selected= ffi.C.shard_select(_SHARD.pshard_t,ffi.cast("char *",key))
local server=ffi.string(ffi.cast("char *",selected.name))
return server
end
function _SHARD.init(shardInfos)
if _SHARD.initialized then
return
end
local shard_t=ffi.new("shard_t[?]",1)
_SHARD.pshard_t = shard_t[0]
local size = 2;
local shards=ffi.new("shard [?]",size)
local pshards=ffi.new("shard* [?]",size)
for i,shard in ipairs(shardInfos) do
shards[i-1].name=ffi.cast("char *",shard.name)
shards[i-1].weight=shard.weight
pshards[i-1]=ffi.cast("shard*",shards[i-1])
end
ffi.C.shard_init(_SHARD.pshard_t,pshards,size)
_SHARD.initialized=true;
end
return _SHARD