diff --git a/lib/resty/lmdb/prefix.lua b/lib/resty/lmdb/prefix.lua index 989530b..4f2db82 100644 --- a/lib/resty/lmdb/prefix.lua +++ b/lib/resty/lmdb/prefix.lua @@ -31,10 +31,16 @@ function _M.page(start, prefix, db, page_size) page_size = DEFAULT_OPS_SIZE end - assert(page_size >= 2, "'page_size' can not be less than 2") + local query_size = page_size + + -- the function ngx_lua_resty_lmdb_ffi_prefix requires at least 2 operations + -- special handling for the case when page_size is less than 2 + if query_size < 2 then + query_size = 2 + end local value_buf_size = get_string_buf_size() - local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", page_size) + local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", query_size) ops[0].opcode = C.NGX_LMDB_OP_PREFIX ops[0].key.data = start @@ -56,7 +62,7 @@ function _M.page(start, prefix, db, page_size) ::again:: local buf = get_string_buf(value_buf_size, false) - local ret = C.ngx_lua_resty_lmdb_ffi_prefix(ops, page_size, + local ret = C.ngx_lua_resty_lmdb_ffi_prefix(ops, query_size, buf, value_buf_size, err_ptr) if ret == NGX_ERROR then return nil, ffi_string(err_ptr[0]) @@ -74,6 +80,12 @@ function _M.page(start, prefix, db, page_size) assert(ret > 0) + -- special handling for the case when page_size is less than 2 + if ret > page_size then + -- we then blindly use ret == page_size to indicate there are more keys + ret = page_size + end + local res = table_new(ret, 0) for i = 1, ret do diff --git a/t/10-prefix.t b/t/10-prefix.t index 8ca2352..f276491 100644 --- a/t/10-prefix.t +++ b/t/10-prefix.t @@ -303,6 +303,15 @@ key: test3 value: value3 for _, pair in ipairs(res) do ngx.say("key: ", pair.key, " value: ", pair.value) end + + ngx.say("PAGE SIZE 1") + res, err = p.page("test", "test", nil, 1) + if not res then + ngx.say("page errored: ", err) + end + + assert(#res == 1) + ngx.say(res[1].key, " ", res[1].value) } } --- request @@ -321,34 +330,8 @@ key: test1 value: value1 SECOND PAGE key: test2 value: value2 key: test3 value: value3 ---- no_error_log -[error] -[warn] -[crit] - - - -=== TEST 8: prefix.page() operation with invalid page size ---- http_config eval: $::HttpConfig ---- main_config eval: $::MainConfig ---- config - location = /t { - content_by_lua_block { - local l = require("resty.lmdb") - - ngx.say(l.db_drop(true)) - local p = require("resty.lmdb.prefix") - - ngx.say(l.set("test", "value")) - ngx.say(pcall(p.page, "test", "test", nil, 1)) - } - } ---- request -GET /t ---- response_body -true -true -false.../lua-resty-lmdb/lua-resty-lmdb/lib/resty/lmdb/prefix.lua:34: 'page_size' can not be less than 2 +PAGE SIZE 1 +test value --- no_error_log [error] [warn]