Permalink
Browse files

adding bsearch was started

  • Loading branch information...
1 parent 411ca19 commit 16f1110988542b19e0aeb26c1d4f0d69b1b8bfe2 @seki committed Sep 12, 2011
Showing with 131 additions and 0 deletions.
  1. +73 −0 lib/drip.rb
  2. +58 −0 test/basic.rb
View
@@ -7,6 +7,79 @@ class Drip
include DRbUndumped
def inspect; to_s; end
+ class ImmutableDrip
+ def initialize(pool=[], tag=[])
+ @pool = pool
+ @tag = tag
+ end
+
+ def fetch(key)
+ idx = lower_boundary(@pool, key)
+ k, v = @pool[idx]
+ k == key ? v.to_a : nil
+ end
+
+ def read(key, n=1)
+ idx = lower_boundary(@pool, key + 1)
+ return [] unless idx
+ @pool[idx, n].collect {|kv|
+ kv[0] + kv[1].to_a
+ }
+ end
+
+ 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
+ }
+ end
+
+ def head(n=1, tag=nil)
+ end
+
+ def older(key, tag=nil)
+ return nil if @pool.empty?
+ key = @pool[-1][0] + 1 unless key
+ if tag
+ else
+ idx = upper_bound(@pool, key - 1)
+ kv = @pool[idx]
+ end
+ end
+
+ def newer()
+ end
+
+ def lower_boundary(ary, key)
+ lower = -1
+ upper = ary.size
+ while lower + 1 != upper
+ mid = (lower + upper).div(2)
+ if key > ary[mid][0]
+ lower = mid
+ else
+ upper = mid
+ end
+ end
+ return upper
+ end
+
+ def upper_boundary(ary, key)
+ lower = -1
+ upper = ary.size
+ while lower + 1 != upper
+ mid = (lower + upper).div(2)
+ if key >= ary[mid][0]
+ lower = mid
+ else
+ upper = mid
+ end
+ end
+ return lower + 1
+ end
+ end
+
def initialize(dir, option={})
@pool = RBTree.new
@tag = RBTree.new
View
@@ -158,4 +158,62 @@ def test_twice
ary = drip.read(ary[2][0], 3)
assert_equal(ary.size, 2)
end
+
+ def ignore_test_huge
+ str = File.read(__FILE__)
+
+ 10.times do
+ 1000.times do |n|
+ @drip.write(str, "n=#{n}")
+ end
+ @drip = Drip.new('test_db')
+ end
+
+ assert_equal(10000, @drip.read(0, 12000, 10000).size)
+ end
+end
+
+class TestImmutableDrip < Test::Unit::TestCase
+ def test_bsearch
+ im = Drip::ImmutableDrip.new
+
+ assert_equal(0, im.lower_boundary([], 'c'))
+ assert_equal(0, im.upper_boundary([], 'c'))
+
+ ary = %w(a b c c c d e f).collect {|x| [x]}
+
+ assert_equal(0, im.lower_boundary(ary, ''))
+ assert_equal(0, im.lower_boundary(ary, 'a'))
+ assert_equal(1, im.lower_boundary(ary, 'b'))
+ assert_equal(2, im.lower_boundary(ary, 'c'))
+ assert_equal(5, im.lower_boundary(ary, 'd'))
+ assert_equal(6, im.lower_boundary(ary, 'e'))
+ assert_equal(7, im.lower_boundary(ary, 'f'))
+ assert_equal(8, im.lower_boundary(ary, 'g'))
+
+ assert_equal(0, im.upper_boundary(ary, ''))
+ assert_equal(1, im.upper_boundary(ary, 'a'))
+ assert_equal(2, im.upper_boundary(ary, 'b'))
+ assert_equal(5, im.upper_boundary(ary, 'c'))
+ assert_equal(6, im.upper_boundary(ary, 'd'))
+ assert_equal(7, im.upper_boundary(ary, 'e'))
+ assert_equal(8, im.upper_boundary(ary, 'f'))
+ assert_equal(8, im.upper_boundary(ary, 'g'))
+ end
+
+ def test_fetch
+ ary = []
+ ary << [21, ['a']]
+ ary << [39, ['b']]
+ ary << [60, ['c']]
+ ary << [99, ['d']]
+
+ im = Drip::ImmutableDrip.new(ary, [])
+ assert_equal(nil, im.fetch(20))
+ assert_equal(['a'], im.fetch(21))
+ assert_equal(nil, im.fetch(23))
+ assert_equal(['b'], im.fetch(39))
+ assert_equal(['d'], im.fetch(99))
+ assert_equal(nil, im.fetch(990))
+ end
end

0 comments on commit 16f1110

Please sign in to comment.