Skip to content

Add support for shdict_get flag comparison #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ install:
- git clone https://github.com/openresty/openresty.git ../openresty
- git clone https://github.com/openresty/openresty-devel-utils.git
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/webcore-no/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/lua-resty-lrucache.git
Expand All @@ -72,7 +72,7 @@ install:
- git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module
- git clone https://github.com/openresty/mockeagain.git
- git clone https://github.com/openresty/test-nginx.git
- git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module
- git clone https://github.com/webcore-no/stream-lua-nginx-module.git ../stream-lua-nginx-module

script:
- cd luajit2/
Expand Down
51 changes: 37 additions & 14 deletions lib/resty/core/shdict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if subsystem == 'http' then
int ngx_http_lua_ffi_shdict_get(void *zone, const unsigned char *key,
size_t key_len, int *value_type, unsigned char **str_value_buf,
size_t *str_value_len, double *num_value, int *user_flags,
int get_stale, int *is_stale, char **errmsg);
int *user_flags_neq, int get_stale, int *is_stale, char **errmsg);

int ngx_http_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
size_t key_len, double *value, char **err, int has_init,
Expand Down Expand Up @@ -101,7 +101,7 @@ elseif subsystem == 'stream' then
int ngx_stream_lua_ffi_shdict_get(void *zone, const unsigned char *key,
size_t key_len, int *value_type, unsigned char **str_value_buf,
size_t *str_value_len, double *num_value, int *user_flags,
int get_stale, int *is_stale, char **errmsg);
int *user_flags_neq, int get_stale, int *is_stale, char **errmsg);

int ngx_stream_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
size_t key_len, double *value, char **err, int has_init,
Expand Down Expand Up @@ -162,6 +162,7 @@ end

local value_type = ffi_new("int[1]")
local user_flags = ffi_new("int[1]")
local user_flags_neq = ffi_new("int[1]")
local num_value = ffi_new("double[1]")
local is_stale = ffi_new("int[1]")
local forcible = ffi_new("int[1]")
Expand Down Expand Up @@ -292,13 +293,20 @@ local function shdict_delete(zone, key)
end


local function shdict_get(zone, key)
local function shdict_get(zone, key, flags)
zone = check_zone(zone)

if key == nil then
return nil, "nil key"
end

if flags ~= nil then
user_flags_neq[0] = 1
user_flags[0] = flags
else
user_flags_neq[0] = 0
end

if type(key) ~= "string" then
key = tostring(key)
end
Expand All @@ -319,8 +327,9 @@ local function shdict_get(zone, key)

local rc = ngx_lua_ffi_shdict_get(zone, key, key_len, value_type,
str_value_buf, value_len,
num_value, user_flags, 0,
is_stale, errmsg)
num_value, user_flags,
user_flags_neq, 0, is_stale,
errmsg)
if rc ~= 0 then
if errmsg[0] ~= nil then
return nil, ffi_str(errmsg[0])
Expand All @@ -332,10 +341,13 @@ local function shdict_get(zone, key)
local typ = value_type[0]

if typ == 0 then -- LUA_TNIL
if flags ~= nil and user_flags_neq[0] == 0 then -- user_flegs are equal
return nil, nil, true
end
return nil
end

local flags = tonumber(user_flags[0])
local flags_ret = tonumber(user_flags[0])

local val

Expand All @@ -359,21 +371,28 @@ local function shdict_get(zone, key)
error("unknown value type: " .. typ)
end

if flags ~= 0 then
return val, flags
if flags_ret ~= 0 then
return val, flags_ret
end

return val
end


local function shdict_get_stale(zone, key)
local function shdict_get_stale(zone, key, flags)
zone = check_zone(zone)

if key == nil then
return nil, "nil key"
end

if flags ~= nil then
user_flags_neq[0] = 1
user_flags[0] = flags
else
user_flags_neq[0] = 0
end

if type(key) ~= "string" then
key = tostring(key)
end
Expand All @@ -394,8 +413,9 @@ local function shdict_get_stale(zone, key)

local rc = ngx_lua_ffi_shdict_get(zone, key, key_len, value_type,
str_value_buf, value_len,
num_value, user_flags, 1,
is_stale, errmsg)
num_value, user_flags,
user_flags_neq, 1, is_stale,
errmsg)
if rc ~= 0 then
if errmsg[0] ~= nil then
return nil, ffi_str(errmsg[0])
Expand All @@ -407,10 +427,13 @@ local function shdict_get_stale(zone, key)
local typ = value_type[0]

if typ == 0 then -- LUA_TNIL
if flags ~= nil and user_flags_neq[0] == 0 then -- user_flegs are equal
return nil, nil, is_stale[0] == 1, true
end
return nil
end

local flags = tonumber(user_flags[0])
local flags_ret = tonumber(user_flags[0])
local val

if typ == 4 then -- LUA_TSTRING
Expand All @@ -433,8 +456,8 @@ local function shdict_get_stale(zone, key)
error("unknown value type: " .. typ)
end

if flags ~= 0 then
return val, flags, is_stale[0] == 1
if flags_ret ~= 0 then
return val, flags_ret, is_stale[0] == 1
end

return val, nil, is_stale[0] == 1
Expand Down