Skip to content

Commit

Permalink
Redis store: Lua code: add debug logging, put comments
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Jul 25, 2023
1 parent 96133ce commit e6aeb85
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions stores/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const ATOMS = 'a'
const READ_MATCHING_COLLECTION = `\
local collections_prefix = ARGV[1];
local rows_prefix = ARGV[2];
local created_min = tonumber(ARGV[3]);
local created_max = tonumber(ARGV[4]);
local when_min = tonumber(ARGV[5]);
local when_max = tonumber(ARGV[6]);
local created_min = tonumber(ARGV[3]); -- UNIX epoch in seconds
local created_max = tonumber(ARGV[4]); -- UNIX epoch in seconds
local when_min = tonumber(ARGV[5]); -- UNIX epoch in milliseconds
local when_max = tonumber(ARGV[6]); -- UNIX epoch in milliseconds
local function read_collection (id)
local rows = {};
redis.log(redis.LOG_DEBUG, 'scanning for collection rows (rows_prefix: ' .. rows_prefix .. ')');
local cursor = "0";
while true do
-- todo: pass in collection rows prefix
Expand All @@ -43,22 +44,28 @@ local function read_collection (id)
if when >= when_min and when <= when_max
then
redis.log(redis.LOG_VERBOSE, 'collection row ' .. i .. ' matches');
local row = redis.call("get", key);
table.insert(rows, {i, row});
else
redis.log(redis.LOG_VERBOSE, 'collection row ' .. i .. ' doesn\\'t match (when: ' .. when .. ')');
end
end
if cursor == "0" then
redis.log(redis.LOG_VERBOSE, 'done scanning for collection rows');
break
end
end
return rows;
end
redis.log(redis.LOG_DEBUG, 'scanning for collections (collections_prefix: ' .. collections_prefix .. ')');
local cursor = "0";
while true do
-- todo: scan in reverse order to, when in doubt, get the latest collection
-- todo: COUNT 30 instead?
local res = redis.call("scan", cursor, "match", collections_prefix .. "*", "COUNT", 100);
cursor = res[1];
Expand All @@ -70,17 +77,24 @@ while true do
then
local col = redis.call("get", key);
local _, __, id, when, duration = string.find(col, "([^:]+):([^:]+):([^:]+)");
redis.log(redis.LOG_VERBOSE, 'id: ' .. id .. 'when: ' .. when .. ' duration: ' .. duration);
when = tonumber(when);
duration = tonumber(duration);
if when <= when_min and (when + duration) >= when_max
then
redis.log(redis.LOG_VERBOSE, 'collection ' .. id .. ' matches');
return read_collection(id);
else
redis.log(redis.LOG_VERBOSE, 'collection ' .. id .. ' doesn\\'t match (when: ' .. when .. ' duration: ' .. duration .. ')');
end
else
redis.log(redis.LOG_VERBOSE, 'collection ' .. id .. ' doesn\\'t match (created: ' .. created .. ')');
end
end
if cursor == "0" then
redis.log(redis.LOG_VERBOSE, 'done scanning for collections');
break
end
end
Expand All @@ -90,9 +104,10 @@ return nil;

const READ_MATCHING_ATOM = `\
local prefix = ARGV[1];
local created_min = tonumber(ARGV[2]);
local created_max = tonumber(ARGV[3]);
local created_min = tonumber(ARGV[2]); -- UNIX epoch in seconds
local created_max = tonumber(ARGV[3]); -- UNIX epoch in seconds
redis.log(redis.LOG_DEBUG, 'scanning for atoms (prefix: ' .. prefix .. ')');
local cursor = "0";
while true do
-- todo: scan in reverse order to, when in doubt, get the latest atom
Expand All @@ -107,10 +122,13 @@ while true do
then
local atom = redis.call("get", key);
return atom;
else
redis.log(redis.LOG_VERBOSE, 'atom doesn\\'t match (created: ' .. created .. ')');
end
end
if cursor == "0" then
redis.log(redis.LOG_VERBOSE, 'done scanning for atoms');
break
end
end
Expand Down

0 comments on commit e6aeb85

Please sign in to comment.