Skip to content

Commit

Permalink
Extract lua into a script file
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaum committed Sep 24, 2019
1 parent b1a7e13 commit 3ab26a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
27 changes: 6 additions & 21 deletions lib/coverband/adapters/hash_redis_store.rb
Expand Up @@ -127,27 +127,12 @@ def save_report_script_input(key:, file:, file_hash:, data:, report_time:, updat
end

def hash_incr_script
@hash_incr_script ||= @redis.script(:load, <<~LUA)
local first_updated_at = table.remove(ARGV, 1)
local last_updated_at = table.remove(ARGV, 1)
local file = table.remove(ARGV, 1)
local file_hash = table.remove(ARGV, 1)
local ttl = table.remove(ARGV, 1)
local file_length = table.remove(ARGV, 1)
local hash_key = table.remove(KEYS, 1)
redis.call('HMSET', hash_key, '#{LAST_UPDATED_KEY}', last_updated_at, '#{FILE_KEY}', file, '#{FILE_HASH}', file_hash, '#{FILE_LENGTH_KEY}', file_length)
redis.call('HSETNX', hash_key, '#{FIRST_UPDATED_KEY}', first_updated_at)
for i, key in ipairs(KEYS) do
if ARGV[i] == '-1' then
redis.call("HSET", hash_key, key, ARGV[i])
else
redis.call("HINCRBY", hash_key, key, ARGV[i])
end
end
if ttl ~= '-1' then
redis.call("EXPIRE", hash_key, ttl)
end
LUA
@hash_incr_script ||= @redis.script(:load, lua_script_content)
end

def lua_script_content
File.read(File.join(
File.dirname(__FILE__), '../../../lua/lib/persist-coverage.lua'))
end

def values_from_redis(local_type, files)
Expand Down
19 changes: 19 additions & 0 deletions lua/lib/persist-coverage.lua
@@ -0,0 +1,19 @@
local first_updated_at = table.remove(ARGV, 1)
local last_updated_at = table.remove(ARGV, 1)
local file = table.remove(ARGV, 1)
local file_hash = table.remove(ARGV, 1)
local ttl = table.remove(ARGV, 1)
local file_length = table.remove(ARGV, 1)
local hash_key = table.remove(KEYS, 1)
redis.call('HMSET', hash_key, 'last_updated_at', last_updated_at, 'file', file, 'file_hash', file_hash, 'file_length', file_length)
redis.call('HSETNX', hash_key, 'first_updated_at', first_updated_at)
for i, key in ipairs(KEYS) do
if ARGV[i] == '-1' then
redis.call("HSET", hash_key, key, ARGV[i])
else
redis.call("HINCRBY", hash_key, key, ARGV[i])
end
end
if ttl ~= '-1' then
redis.call("EXPIRE", hash_key, ttl)
end

0 comments on commit 3ab26a6

Please sign in to comment.