Created
October 29, 2015 15:44
-
-
Save ztellman/d0940f2d03d657f8f25c to your computer and use it in GitHub Desktop.
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
local function delta(t0, t1) | |
return ((t1[1] - t0[1]) * 1000000) + (t1[2] - t0[2]) | |
end | |
local t0,t1 | |
local s1 = redis.call("get", KEYS[1]) | |
local s2 = ARGV[1] | |
if s1 == false then | |
redis.call("set", KEYS[1], s2) | |
return s2 | |
end | |
redis.log(redis.LOG_WARNING, "hello") | |
local lshift, rshift, bor, band, byte, char, len, ceil, log, max, exp = bit.lshift, bit.rshift, bit.bor, bit.band, string.byte, string.char, string.len, math.ceil, math.log, math.max, math.exp | |
---[[ | |
local function decode(s, i) | |
local n = 0 | |
while true do | |
local b = byte(s, i) | |
i = i + 1 | |
n = n * 128 -- multiplication is faster than bit-shifts, go figure | |
if b > 127 then | |
n = n + b - 128 | |
else | |
return n + b, i | |
end | |
end | |
end | |
local encoders = { | |
function(n) | |
return char(n) | |
end, | |
function(n) | |
return char(((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/2097152) % 128) + 128, ((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/2.68435456E8) % 128) + 128, ((n/2097152) % 128) + 128, ((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/3.4359738368E10) % 128) + 128, ((n/2.68435456E8) % 128) + 128, ((n/2097152) % 128) + 128, ((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/4.398046511104E12) % 128) + 128, ((n/3.4359738368E10) % 128) + 128, ((n/2.68435456E8) % 128) + 128, ((n/2097152) % 128) + 128, ((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end, | |
function(n) | |
return char(((n/5.62949953421312E14) % 128) + 128, ((n/4.398046511104E12) % 128) + 128, ((n/3.4359738368E10) % 128) + 128, ((n/2.68435456E8) % 128) + 128, ((n/2097152) % 128) + 128, ((n/16384) % 128) + 128, ((n/128) % 128) + 128, n % 128) | |
end | |
} | |
local log2 = log(2) | |
local function encode(n) | |
local size = 0 | |
if n > 0 then size = ceil(log(n)/(log2*7)) end | |
return encoders[size](n) | |
end | |
--]] | |
--[[ | |
local function decode(s, i) | |
local n = 0 | |
for j=0,7 do | |
local b = byte(s,i+j) | |
n = lshift(n,8) + b | |
end | |
return n, i+8 | |
end | |
local function encode(n) | |
return char(band(n,255), band(rshift(n,8), 255), band(rshift(n,16), 255), band(rshift(n,24), 255), band(rshift(n,32), 255), band(rshift(n,40), 255), band(rshift(n,48), 255), band(rshift(n,56), 255)) | |
end | |
--]] | |
local function zip(s1, s2) | |
local p1, p2, n1, n2 = 1, 1, 0, 0 | |
local acc = {} | |
t0 = redis.call("time") | |
while true do | |
if p1 > len(s1) or p2 > len(s2) then | |
if true then --p1 > len(s1) and p2 > len(s2) then | |
break | |
else | |
return false | |
end | |
end | |
n1,p1 = decode(s1, p1) | |
n2,p2 = decode(s2, p2) | |
acc[#acc+1] = encode(n1+n2) | |
end | |
t1 = redis.call("time") | |
return table.concat(acc) | |
end | |
local s3 = zip(s1,s2) | |
if s3 == false then | |
return redis.error_reply("sketch size mismatch") | |
end | |
--redis.call("set", KEYS[1], s3) | |
return delta(t0,t1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment