Created
February 5, 2011 21:51
-
-
Save rjp/812828 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'redis' | |
$redis = Redis.new() | |
$redis.select(3) | |
# do a zdiffstore only on scores that match | |
# ie [23:12345] will only be removed by [23:12345], not [23:12350] | |
def zdiffstore(x, y) | |
$redis.zunionstore "tmp.zu", [x, y], :weights=>[1,-1], :aggregate=>:sum | |
$redis.zremrangebyscore("tmp.zu", "0", "+inf") | |
$redis.zinterstore "o", ["tmp.zu", y], :weights=>[1,1], :aggregate=>:max | |
x = $redis.zrange "o", 0, -1, :with_scores => true | |
return Hash[*x.flatten] | |
end | |
# do a zdiffstore ignoring scores that differ | |
# ie [23:12345] will be removed by [23:12350] | |
def zdiffstore_justkeys(x, y) | |
$redis.zunionstore "tmp.zu", [x, y], :weights=>['-inf',1], :aggregate=>:sum | |
$redis.zremrangebyscore("tmp.zu", "-inf", "0") | |
$redis.zinterstore "o", ["tmp.zu", y], :weights=>[1,1], :aggregate=>:max | |
x = $redis.zrange "o", 0, -1, :with_scores => true | |
return Hash[*x.flatten] | |
end | |
a = zdiffstore("user:rjp:read", "all:messages") | |
p a.size |
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
require 'rubygems' | |
require 'redis' | |
require 'test/unit' | |
$redis = Redis.new() | |
puts "creating data structures" | |
$redis.del("folder:mince") | |
$redis.del("user:rjp:read") | |
$redis.zadd("folder:mince",12345,23) | |
$redis.zadd("folder:mince",12346,24) | |
$redis.zadd("folder:mince",12349,26) | |
$redis.zadd("user:rjp:read",12345,23) | |
# 25 is in another folder | |
$redis.zadd("user:rjp:read",12345,25) | |
# do a zdiffstore only on scores that match | |
# ie [23:12345] will only be removed by [23:12345], not [23:12350] | |
def zdiffstore(x, y) | |
$redis.zunionstore "tmp.zu", [x, y], :weights=>[1,-1], :aggregate=>:sum | |
$redis.zremrangebyscore("tmp.zu", "0", "+inf") | |
$redis.zinterstore "o", ["tmp.zu", y], :weights=>[1,1], :aggregate=>:max | |
x = $redis.zrange "o", 0, -1, :with_scores => true | |
return Hash[*x.flatten] | |
end | |
# do a zdiffstore ignoring scores that differ | |
# ie [23:12345] will be removed by [23:12350] | |
def zdiffstore_justkeys(x, y) | |
$redis.zunionstore "tmp.zu", [x, y], :weights=>['-inf',1], :aggregate=>:sum | |
$redis.zremrangebyscore("tmp.zu", "-inf", "0") | |
$redis.zinterstore "o", ["tmp.zu", y], :weights=>[1,1], :aggregate=>:max | |
x = $redis.zrange "o", 0, -1, :with_scores => true | |
return Hash[*x.flatten] | |
end | |
$fm_clean = {'23'=>'12345','24'=>'12346','26'=>'12349'} | |
$fm_no23 = {'24'=>'12346','26'=>'12349'} | |
$fm_after23 = {'24'=>'12346','26'=>'12349'} | |
# folder:mince has 23:12345, 24:12346, 26:12349 | |
# rjp has read 23:12345, 25:12345 | |
class TestRedisZDIFF < Test::Unit::TestCase | |
def test_all | |
# we've only read 23, we get 24,26 as unread | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a, $fm_no23) | |
# we've only read 23, we get 24,26 as unread | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b, $fm_no23) | |
# 23 is annotated at 12350 | |
$redis.zadd "folder:mince", 12350, 23 | |
# rjp now hasn't read 23 because the timestamps don't match | |
# this matches "mark-unread-on-edit" | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a,{'23'=>'12350','24'=>'12346','26'=>'12349'}) | |
# if we ignore the timestamps, it's still read | |
# this matches "no-change-on-edit" | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b, $fm_no23) | |
# 24 is annotated at 12351 | |
$redis.zadd "folder:mince", 12351, 24 | |
# 24 is still unread because it's not on user:rjp:read anyway | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a,{'23'=>'12350','24'=>'12351','26'=>'12349'}) | |
# ditto | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b,{'24'=>'12351','26'=>'12349'}) | |
# 25 is annotated at 12352 | |
$redis.zadd "user:rjp:read", 12350, 25 | |
# this makes no difference because 25 is not in folder:mince | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a,{'23'=>'12350','24'=>'12351','26'=>'12349'}) | |
# ditto | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b,{'24'=>'12351','26'=>'12349'}) | |
# now rjp reads 24 | |
$redis.zadd "user:rjp:read", 12351, 24 | |
# 24 disappears because we've read it | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a,{'23'=>'12350','26'=>'12349'}) | |
# ditto | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b, {'26'=>'12349'}) | |
# now rjp reads 26 | |
$redis.zadd "user:rjp:read", 12349, 26 | |
# 26 disappears because we've read it | |
a = zdiffstore("user:rjp:read", "folder:mince") | |
assert_equal(a,{'23'=>'12350'}) | |
# ditto | |
b = zdiffstore_justkeys("user:rjp:read", "folder:mince") | |
assert_equal(b, {}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment