Tarantool: limit/offset in index.indexName:pairs call

Tarantool: limit/offset in index.indexName:pairs call

我需要从 space users 中获取一些记录。 这个 space 有一个二级索引 category_status_rating。 我需要 select 个 category=1status=1rating<=123456789:

的用户
for _, user in box.space.users.index.category_status_rating:pairs({ 1, 1, 123456789 }, { limit = 20, offset = 5, iterator = box.index.LE }) do
    if user[categoryIdx] ~= 1 or user[statusIdx] ~= 1 then break end
    table.insert(users, user)
end

据我所知,indexName:pairs 的迭代不支持 limit,我只能使用自己的计数器。但是 offset 呢?我可以使用此参数并从我需要的 "page" 开始吗?或者我会在没有任何 offset 的情况下进行迭代并传递无用的记录(大约 100000)并在我的 "page" 开始时开始 table.insert(users, user) 吗? 谢谢!

如果您真的需要,您可以保存您的位置(最后检查的元组)而不是使用偏移量。 例如:

local last = 123456789
for i = 1, 2 do
    local count = 0
    for _, user in box.space.users.index.category_status_rating:pairs({1, 1, last}, { iterator = box.index.LE }) do
        if user[categoryIdx] ~= 1 or user[statusIdx] ~= 1 or count > 20 then
            break
        end
        table.insert(users, user)
        last = user[LAST_INDEX_FIELD]
        count = count + 1
    end
    -- process your tuples
end

或者,使用 luafun(其中 drop_n 是限制的模拟,保存到 last 是偏移的模拟):

local last = 123456789
for i = 1, 2 do
    local users = box.space.users.index.category_status_rating:pairs({1, 1, last}, { iterator = box.index.LE }):take_n(20):map(function(user)
        last = user[LAST_INDEX_FIELD]
        return user
    end):totable()
    -- process your tuples
end

Documentation on LuaFun, which is embedded into Tarantool.