Last active
February 20, 2018 00:35
-
-
Save Andrey2G/2497e4057da3774d3b8de2e1991502ab to your computer and use it in GitHub Desktop.
Redis, LUA, execute Redis command in chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- to avoid the problem with unpack ("too many results to unpack") | |
-- we can increase LUAI_MAXCSTACK in luaconf.h https://www.lua.org/source/5.1/luaconf.h.html | |
-- or execute the command with using table.unpack (https://www.lua.org/manual/5.1/manual.html#pdf-table.unpack) in chunks by specific length | |
-- table.merge - separated function to merge two tables | |
-- size of args should be >4000 (chunk length) | |
local executeRedisCommandInChunks = function (command, key, args) | |
local results = {} | |
local chunkResult = {} | |
local args_len = #args | |
-- chunk length (any positive value less then 8000) | |
local chunk_len = 4000 | |
if (string.len(key)>0) then | |
-- for commands with one key like HMGET/HMSET | |
for i = 1, args_len, chunk_len do | |
chunkResult = redis.call(command, key, unpack(args, i, math.min(i + chunk_len - 1, args_len))) | |
results = table.merge(results,chunkResult) | |
end | |
else | |
-- for commands like DEL/MGET/MSET/etc | |
for i = 1, args_len, chunk_len do | |
chunkResult = redis.call(command, unpack(args, i, math.min(i + chunk_len - 1, args_len))) | |
results = table.merge(results,chunkResult) | |
end | |
end | |
return results | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useful links
https://github.com/antirez/redis/blob/b903145/deps/lua/src/luaconf.h#L439
http://rosettacode.org/wiki/Array_concatenation#Lua
http://stackoverflow.com/questions/1410862/concatenation-of-tables-in-lua
http://lua-users.org/wiki/CopyTable