Permalink
Browse files

Force get_range to respect :start_key when specified. Resolves #127.

  • Loading branch information...
1 parent 9cbd441 commit d278a0de202203c5c779f4908a293e3b37f65313 Robert Jackson committed Nov 8, 2011
Showing with 27 additions and 14 deletions.
  1. +1 −0 CHANGELOG
  2. +6 −5 lib/cassandra/cassandra.rb
  3. +20 −9 test/cassandra_test.rb
View
@@ -1,4 +1,5 @@
v0.12.2
+- Respect the start_key in get_range. Resolves Issue #127.
- Fix issue with differences in gemspec and what is required. Resolves Issue #125.
- Update to Cassandra 0.8.7 and 1.0.2.
View
@@ -751,17 +751,16 @@ def get_range_batch(column_family, options = {})
result = (!block_given? && {}) || nil
num_results = 0
- options[:start_key] ||= ''
+ first_key = options[:start_key] || ''
last_key = nil
- while options[:start_key] != last_key && (count.nil? || count > num_results)
- options[:start_key] = last_key
- res = get_range_single(column_family, options.merge!(:start_key => last_key,
+ while first_key != last_key && (count.nil? || count > num_results)
+ res = get_range_single(column_family, options.merge!(:start_key => first_key,
:key_count => batch_size,
:return_empty_rows => true
))
res.each do |key, columns|
- next if options[:start_key] == key
+ next if last_key == key
next if num_results == count
unless columns == {}
@@ -774,6 +773,8 @@ def get_range_batch(column_family, options = {})
end
last_key = key
end
+
+ first_key = last_key
end
result
View
@@ -280,35 +280,46 @@ def test_get_range_with_count
def test_get_range_block
k = key
+
+ values = {}
5.times do |i|
- @twitter.insert(:Statuses, k+i.to_s, {"body-#{i.to_s}" => 'v'})
+ values[k+i.to_s] = {"body-#{i.to_s}" => 'v'}
end
- values = (0..4).collect{|n| { :key => "test_get_range_block#{n}", :columns => { "body-#{n}" => "v" }} }.reverse
+ values.each {|key, columns| @twitter.insert(:Statuses, key, columns) }
returned_value = @twitter.get_range(:Statuses, :start_key => k.to_s, :key_count => 5) do |key,columns|
- expected = values.pop
- assert_equal expected[:key], key
- assert_equal expected[:columns], columns
+ expected = values.delete(key)
+ assert_equal expected, columns
end
- assert_equal [], values
+ assert values.length < 5
assert_nil returned_value
end
-
+
def test_get_range_reversed
data = 3.times.map { |i| ["body-#{i.to_s}", "v"] }
hash = Cassandra::OrderedHash[data]
reversed_hash = Cassandra::OrderedHash[data.reverse]
-
+
@twitter.insert(:Statuses, "all-keys", hash)
-
+
columns = @twitter.get_range(:Statuses, :reversed => true)["all-keys"]
columns.each do |column|
assert_equal reversed_hash.shift, column
end
end
+ def test_get_range_with_start_key_and_key_count
+ hash = {"name" => "value"}
+ @twitter.insert(:Statuses, "a-key", hash)
+ @twitter.insert(:Statuses, "b-key", hash)
+ @twitter.insert(:Statuses, "c-key", hash)
+
+ results = @twitter.get_range(:Statuses, :start_key => "b-key", :key_count => 1)
+ assert_equal ["b-key"], results.keys
+ end
+
def test_each_key
k = key
keys_yielded = []

0 comments on commit d278a0d

Please sign in to comment.