Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

latest?, write_if_latest

  • Loading branch information...
commit 8cf7d6b29df5f14d65ddb559b9ae94605bb3b68b 1 parent bb6aba0
@seki authored
Showing with 46 additions and 21 deletions.
  1. +19 −9 lib/drip.rb
  2. +27 −12 test/basic.rb
View
28 lib/drip.rb
@@ -64,14 +64,14 @@ def read_tag(key, tag, n=1)
}
end
- def old?(key, tag)
+ def latest?(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
+ return lower == upper - 1
else
- return @pool[-1][0] > key
+ return @pool[-1][0] == key
end
end
@@ -168,6 +168,16 @@ def write_at(at, *value)
end
end
+ def write_if_latest(cond, *value)
+ make_key(Time.now) do |key|
+ cond.each {|it|
+ return nil unless latest?(it[1], it[0])
+ }
+ value = do_write(key, value)
+ @pool[key] = @store.write(key, value)
+ end
+ end
+
def fetch(key)
return @past.fetch(key) if @fence >= key
@pool[key].to_a
@@ -205,20 +215,20 @@ def head(n=1, tag=nil)
@past.head(n - ary.size, tag) + ary
end
- def old?(key, tag=nil)
+ def latest?(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
+ return true if it[1] == key
+ return false if it[1] > key
end
else
k ,= @pool.upper_bound(now)
- return false if k == key
- return true if k.to_i > key
+ return true if k == key
+ return false if k.to_i > key
end
- @past.old?(key, tag)
+ @past.latest?(key, tag)
end
def older(key, tag=nil)
View
39 test/basic.rb
@@ -125,20 +125,34 @@ def test_duplicate_tags
assert_equal(@drip[oid], ['dup', 'hello'])
end
- def test_old?
+ def test_latest?
key = @drip.write(:start)
10.times do |n|
@drip.write(n)
end
- assert_equal(@drip.old?(key), true)
+ assert_equal(@drip.latest?(key), false)
key = @drip.write(:stop)
- assert_equal(@drip.old?(key), false)
+ assert_equal(@drip.latest?(key), true)
key = @drip.write(:tag_start, 'tag')
@drip.write(:tag, 'ignore tag')
- assert_equal(@drip.old?(key, 'tag'), false)
+ assert_equal(@drip.latest?(key, 'tag'), true)
@drip.write(:tag, 'tag')
- assert_equal(@drip.old?(key, 'tag'), true)
+ assert_equal(@drip.latest?(key, 'tag'), false)
+ end
+
+ def test_write_if_latest
+ t1 = @drip.write('t1', 't1')
+ t2 = @drip.write('t2', 't2')
+ t3 = @drip.write('t3', 't3')
+ assert_equal(@drip.latest?(t1, 't1'), true)
+ assert(@drip.write_if_latest([['t1', t1],
+ ['t2', t2],
+ ['t3', t3]], 'hello', 't1'))
+ assert_equal(@drip.latest?(t1, 't1'), false)
+ assert_equal(@drip.write_if_latest([['t1', t1],
+ ['t2', t2],
+ ['t3', t3]], 'hello', 't1'), nil)
end
end
@@ -156,18 +170,19 @@ def teardown
remove_drip
end
- def test_twice_old?
- assert_equal(@drip.old?(1), false)
+ def test_twice_latest?
+ assert_equal(@drip.latest?(1), false)
tag1 = @drip.write('tag1', 'tag1')
+ assert_equal(@drip.latest?(tag1), true)
@drip.write('nop', 'tag1')
@drip.write('nop', 'tag1')
tag2 = @drip.write('tag2', 'tag1')
- assert_equal(@drip.old?(1), true)
+ assert_equal(@drip.latest?(1), false)
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)
+ assert_equal(drip.latest?(1), false)
+ assert_equal(drip.latest?(tag1, 'tag1'), false)
+ assert_equal(drip.latest?(tag2, 'tag1'), true)
+ assert_equal(drip.latest?(tag2, 'tag0'), false)
end
def test_twice
Please sign in to comment.
Something went wrong with that request. Please try again.