tarantool/tarantool

Execute any function after each N iterations on `index:pairs()`

Open

#10,945 opened on 2024年12月16日

GitHub で見る
 (0 comments) (0 reactions) (0 assignees)Lua (406 forks)batch import
featuregood first issueraw idea

Repository metrics

Stars
 (3,633 stars)
PR merge metrics
 (平均マージ 13d 2h) (30d で 82 merged PRs)

説明

When iterating over a potentially large table, one needs to do something like:

local count = 0
for _, t in index:pairs(key) do
    process_tuple(t)
    count = count + 1
    if count == 100 do
        fiber.yield()
        count = 0
    end
end

We could provide a way to do it more conveniently. For example:

for _, t in index:pairs(key):after_each(100, fiber.yield) do
    process_tuple(t)
end

Such function could be used for other purposes:

-- This scenario requires to pass `count` parameter to the handler
for _, t in index:pairs(key):after_each(1000, function(cnt) log.info("Processed %d tuples", cnt) end) do
    process_tuple(t)
end

Or, instead of a general-purpose solution, we can just add a new option to pairs to handle only periodic yields:

for _, t in index:pairs(key, {yield_period = 100}) do
    process_tuple(t)
end

コントリビューターガイド