Last active
October 30, 2017 16:58
-
-
Save z4r/6765636 to your computer and use it in GitHub Desktop.
ZRANGEBYSCORESTORE
This file contains 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
redis.call('ZUNIONSTORE', KEYS[1], 1, KEYS[2]) | |
local lbound, ubound = ARGV[1], ARGV[2] | |
if lbound ~= '-inf' then | |
lbound = lbound:sub(1, 1) == '(' and lbound:sub(2) or '('..lbound | |
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', lbound) | |
end | |
if ubound ~= '+inf' then | |
ubound = ubound:sub(1, 1) == '(' and ubound:sub(2) or '('..ubound | |
redis.call('ZREMRANGEBYSCORE', KEYS[1], ubound, '+inf') | |
end |
This file contains 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
import redis | |
from functools import partial | |
from itertools import product | |
def ztest(zrangebyscorestore, dest, source, lbound, ubound): | |
zrangebyscorestore(keys=[dest, source], args=[lbound, ubound]) | |
got = client.zrangebyscore(dest, '-inf', '+inf', withscores=True) | |
expected = client.zrangebyscore(source, lbound, ubound, withscores=True) | |
assert got == expected, '{0} != {1}'.format(got, expected) | |
client = redis.StrictRedis(db=8) | |
client.script_flush() | |
client.flushdb() | |
client.zadd('SOURCE', **dict([('p{0}'.format(i), i) for i in range(5)])) | |
with open('zrangebyscorestore.lua') as fp: | |
test = partial(ztest, client.register_script(fp.read()), 'DEST', 'SOURCE') | |
for lbound, ubound in product(['-inf', 1, '(1'], [3, '(3', '+inf']): | |
test(lbound, ubound) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment