Permalink
Browse files

old?

  • Loading branch information...
1 parent c60c987 commit bb6aba0c00b664811026aeefef9fd88c504a3104 @seki committed Mar 18, 2012
Showing with 58 additions and 1 deletion.
  1. +28 −1 lib/drip.rb
  2. +30 −0 test/basic.rb
View
@@ -64,6 +64,17 @@ def read_tag(key, tag, n=1)
}
end
+ def old?(key, tag)
+ return false if @pool.empty?
+ if tag
+ lower = lower_boundary(@tag, [tag, key])
+ upper = upper_boundary(@tag, [tag, INF])
+ return lower < upper - 1
+ else
+ return @pool[-1][0] > key
+ end
+ end
+
def head_tag(n, tag)
lower = lower_boundary(@tag, [tag, 0])
upper = upper_boundary(@tag, [tag, INF])
@@ -194,6 +205,22 @@ def head(n=1, tag=nil)
@past.head(n - ary.size, tag) + ary
end
+ def old?(key, tag=nil)
+ now = time_to_key(Time.now)
+ if tag
+ it ,= @tag.upper_bound([tag, now])
+ if it && it[0] == tag
+ return false if it[1] == key
+ return true if it[1] > key
+ end
+ else
+ k ,= @pool.upper_bound(now)
+ return false if k == key
+ return true if k.to_i > key
+ end
+ @past.old?(key, tag)
+ end
+
def older(key, tag=nil)
curr_older(key, tag) || @past.older(key, tag)
end
@@ -240,7 +267,7 @@ def curr_read_tag(key, tag, n=1, at_least=1, timeout=nil)
def curr_head(n=1, tag=nil)
ary = []
key = nil
- while it = older(key, tag)
+ while it = curr_older(key, tag)
break if n <= 0
ary.unshift(it)
key = it[0]
View
@@ -124,6 +124,22 @@ def test_duplicate_tags
oid = @drip.write('dup', 'hello', 'hello', 'hello')
assert_equal(@drip[oid], ['dup', 'hello'])
end
+
+ def test_old?
+ key = @drip.write(:start)
+ 10.times do |n|
+ @drip.write(n)
+ end
+ assert_equal(@drip.old?(key), true)
+ key = @drip.write(:stop)
+ assert_equal(@drip.old?(key), false)
+
+ key = @drip.write(:tag_start, 'tag')
+ @drip.write(:tag, 'ignore tag')
+ assert_equal(@drip.old?(key, 'tag'), false)
+ @drip.write(:tag, 'tag')
+ assert_equal(@drip.old?(key, 'tag'), true)
+ end
end
class TestDripUsingStorage < TestDrip
@@ -139,6 +155,20 @@ def setup
def teardown
remove_drip
end
+
+ def test_twice_old?
+ assert_equal(@drip.old?(1), false)
+ tag1 = @drip.write('tag1', 'tag1')
+ @drip.write('nop', 'tag1')
+ @drip.write('nop', 'tag1')
+ tag2 = @drip.write('tag2', 'tag1')
+ assert_equal(@drip.old?(1), true)
+ drip = Drip.new('test_db')
+ assert_equal(drip.old?(1), true)
+ assert_equal(drip.old?(tag1, 'tag1'), true)
+ assert_equal(drip.old?(tag2, 'tag1'), false)
+ assert_equal(drip.old?(tag2, 'tag0'), false)
+ end
def test_twice
11.times do |n|

0 comments on commit bb6aba0

Please sign in to comment.