Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dndx committed Sep 29, 2024
1 parent c2adeba commit 8e96159
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ from the `txn` table when `commit()` returned an error is undefined.
Return all keys `>= start` and starts with `prefix`. If `db` is omitted,
it defaults to `"_default"`.

If `page_size` is specified, up to `page_size` results will be returned. However,
`page_size` can not be set to less than `2` due to internal implementation limitations.

The return value of this function is a table `res` where `res[1].key` and `res[1].value`
corresponds to the first key and value, `res[2].key` and `res[2].value` corresponds to the
second and etc. If no keys matched the provided criteria, then an empty table will be
Expand All @@ -246,6 +249,9 @@ matching the `prefix` is available.

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

This is a low level function, most of the use case should instead use the higher level
[lmdb.prefix](#prefix) iterator instead.

[Back to TOC](#table-of-contents)

## Directives
Expand Down
2 changes: 2 additions & 0 deletions lib/resty/lmdb/prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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 value_buf_size = get_string_buf_size()
local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", page_size)

Expand Down
111 changes: 111 additions & 0 deletions t/10-prefix.t
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,114 @@ done
[error]
[warn]
[crit]



=== TEST 6: prefix.page() operation
--- 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))
ngx.say(l.set("test", "value"))
ngx.say(l.set("test1", "value1"))
ngx.say(l.set("test2", "value2"))
ngx.say(l.set("test3", "value3"))
ngx.say(l.set("u", "value4"))
ngx.say(l.set("u1", "value5"))
local p = require("resty.lmdb.prefix")
local res, err = p.page("test", "test")
if not res then
ngx.say("page errored: ", err)
end
for _, pair in ipairs(res) do
ngx.say("key: ", pair.key, " value: ", pair.value)
end
}
}
--- request
GET /t
--- response_body
true
true
true
true
true
true
true
key: test value: value
key: test1 value: value1
key: test2 value: value2
key: test3 value: value3
--- no_error_log
[error]
[warn]
[crit]



=== TEST 7: prefix.page() operation with custom 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))
ngx.say(l.set("test", "value"))
ngx.say(l.set("test1", "value1"))
ngx.say(l.set("test2", "value2"))
ngx.say(l.set("test3", "value3"))
ngx.say(l.set("u", "value4"))
ngx.say(l.set("u1", "value5"))
local p = require("resty.lmdb.prefix")
local res, err = p.page("test", "test", nil, 2)
if not res then
ngx.say("page errored: ", err)
end
ngx.say("FIRST PAGE")
for _, pair in ipairs(res) do
ngx.say("key: ", pair.key, " value: ", pair.value)
end
res, err = p.page("test1\x00", "test", nil, 2)
if not res then
ngx.say("page errored: ", err)
end
ngx.say("SECOND PAGE")
for _, pair in ipairs(res) do
ngx.say("key: ", pair.key, " value: ", pair.value)
end
}
}
--- request
GET /t
--- response_body
true
true
true
true
true
true
true
FIRST PAGE
key: test value: value
key: test1 value: value1
SECOND PAGE
key: test2 value: value2
key: test3 value: value3
--- no_error_log
[error]
[warn]
[crit]

0 comments on commit 8e96159

Please sign in to comment.