Skip to content

Commit

Permalink
added infinity support to View#traverse_key, View#find
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Andreev committed May 2, 2008
1 parent 78ea0eb commit 9b96a67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/strokedb/views/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ def find(key = nil, options = {})
# # => start_key == ["prefix", 10, "sfx", "a"]
# # => end_key == ["prefix", 42, "sfx", "Z"]
#
def traverse_key(key, sk = [], ek = [])
def traverse_key(key, sk = [], ek = [], s_inf = nil, e_inf = nil)
case key
when Range
[sk << key.begin, ek << key.end]
a = key.begin
b = key.end
[
s_inf || a.infinite? ? sk : (sk << a),
e_inf || b.infinite? ? ek : (ek << b),
]
when Array
key.inject([sk, ek]) do |se, i|
traverse_key(i, *se)
traverse_key(i, se[0], se[1], s_inf, e_inf)
end
else
[sk << key, ek << key]
[s_inf ? sk : (sk << key), e_inf ? ek : (ek << key)]
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/strokedb/views/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ def view.map(*args); end
@v.traverse_key([:pfx, "z".."a", 1..3]).should == [[:pfx, "z", 1], [:pfx, "a", 3]]
end

it "should traverse half-opened ranges correctly" do
@v.traverse_key(1..Infinity).should == [[1], []]
@v.traverse_key("z"..InfinityString).should == [["z"], []]
t = Time.now
@v.traverse_key([:pfx, t..InfinityTime]).should == [[:pfx, t], [:pfx]]
end

end


Expand Down

0 comments on commit 9b96a67

Please sign in to comment.