Browse files

with tag

  • Loading branch information...
1 parent 9aaaf8b commit 8227c882f0b0409774ef6b33a56f7f1a5703ec02 @seki committed Sep 15, 2011
Showing with 31 additions and 1 deletion.
  1. +7 −1 lib/drip.rb
  2. +24 −0 test/basic.rb
View
8 lib/drip.rb
@@ -51,7 +51,7 @@ def read_tag(key, tag, n=1)
idx = lower_boundary(@tag, [tag, key + 1])
return [] unless idx
@tag[idx, n].find_all {|kv| kv[0][0] == tag}.collect {|kv|
- kv[0][1] + kv[1].to_a
+ [kv[0][1], *kv[1].to_a]
}
end
@@ -72,6 +72,12 @@ def head(n=1, tag=nil)
}
end
+ def older_tag(key, tag)
+ idx = upper_boundary(@tag, [tag, key-1])
+ k, v = @tag[idx - 1]
+ k && k[0] == tag ? [k[1], *v.to_a] : nil
+ end
+
def older(key, tag=nil)
key = @pool[-1][0] + 1 unless key
return older_tag(key, tag) if tag
View
24 test/basic.rb
@@ -259,5 +259,29 @@ def test_read_w_tag
assert_equal([[99, 'd', 'tag', 'd']], im.head(1, 'tag'))
assert_equal([[99, 'd', 'tag', 'd']], im.head(1, 'd'))
+ assert_equal([[60, 'c', 'c', 'tag'], [99, 'd', 'tag', 'd']],
+ im.head(2, 'tag'))
+
+ assert_equal([[159, 'e', 'tag2', 'e']], im.read_tag(1, 'tag2'))
+ assert_equal([[99, 'd', 'tag', 'd']], im.read_tag(60, 'tag', 3))
+ assert_equal([[60, 'c', 'c', 'tag'], [99, 'd', 'tag', 'd']],
+ im.read_tag(39, 'tag', 3))
+ assert_equal([[39, 'b', 'b', 'tag'],
+ [60, 'c', 'c', 'tag'],
+ [99, 'd', 'tag', 'd']],
+ im.read_tag(21, 'tag', 5))
+ assert_equal([[39, 'b', 'b', 'tag'],
+ [60, 'c', 'c', 'tag']],
+ im.read_tag(21, 'tag', 2))
+ assert_equal([[39, 'b', 'b', 'tag'],
+ [60, 'c', 'c', 'tag']],
+ im.read_tag(38, 'tag', 2))
+
+ assert_equal([99, 'd', 'tag', 'd'], im.older(nil, 'tag'))
+ assert_equal([60, 'c', 'c', 'tag'], im.older(99, 'tag'))
+ assert_equal(nil, im.older(21), 'tag')
+
+ assert_equal([60, 'c', 'c', 'tag'], im.newer(39, 'tag'))
+ assert_equal(nil, im.newer(99, 'tag'))
end
end

0 comments on commit 8227c88

Please sign in to comment.