Skip to content

Commit

Permalink
feat(page): add page_size parameter to allow overriding page size from
Browse files Browse the repository at this point in the history
caller side

This is useful for DB-less reads as DB-less defines it's own page size
which might differ from what this library provides

KAG-5342
  • Loading branch information
dndx committed Sep 8, 2024
1 parent 5016b11 commit c2adeba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ from the `txn` table when `commit()` returned an error is undefined.

#### page

**syntax:** *res, err = prefix.page(start, prefix, db?)*
**syntax:** *res, err_or_more = prefix.page(start, prefix, db?, page_size?)*

**context:** *any context*

Expand All @@ -239,6 +239,11 @@ corresponds to the first key and value, `res[2].key` and `res[2].value` correspo
second and etc. If no keys matched the provided criteria, then an empty table will be
returned.

In case of success, the second return value will be a boolean indicating if more keys are
possibly present. However, even when this value is `true`, it is possible subsequent `page`
might return an empty list. If this value is `false`, then it is guaranteed no more keys
matching the `prefix` is available.

In case of errors, `nil` and an string describing the reason of the failure will be returned.

[Back to TOC](#table-of-contents)
Expand Down
14 changes: 9 additions & 5 deletions lib/resty/lmdb/prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ local get_string_buf_size = base.get_string_buf_size
local assert = assert


function _M.page(start, prefix, db)
function _M.page(start, prefix, db, page_size)
if not page_size then
page_size = DEFAULT_OPS_SIZE
end

local value_buf_size = get_string_buf_size()
local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", DEFAULT_OPS_SIZE)
local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", page_size)

ops[0].opcode = C.NGX_LMDB_OP_PREFIX
ops[0].key.data = start
Expand All @@ -50,7 +54,7 @@ function _M.page(start, prefix, db)

::again::
local buf = get_string_buf(value_buf_size, false)
local ret = C.ngx_lua_resty_lmdb_ffi_prefix(ops, DEFAULT_OPS_SIZE,
local ret = C.ngx_lua_resty_lmdb_ffi_prefix(ops, page_size,
buf, value_buf_size, err_ptr)
if ret == NGX_ERROR then
return nil, ffi_string(err_ptr[0])
Expand Down Expand Up @@ -83,8 +87,8 @@ function _M.page(start, prefix, db)
res[i] = pair
end

-- if ret == DEFAULT_OPS_SIZE, then it is possible there are more keys
return res, ret == DEFAULT_OPS_SIZE
-- if ret == page_size, then it is possible there are more keys
return res, ret == page_size
end


Expand Down

0 comments on commit c2adeba

Please sign in to comment.