Skip to content

Commit

Permalink
Merge pull request cassandra-rb#122 from shyamalprasad/batchkeycount
Browse files Browse the repository at this point in the history
get_range_batch now correctly counts results for :key_count.
  • Loading branch information
ryanking committed Oct 6, 2011
2 parents 32b7dfd + d000181 commit 00a48e1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/cassandra/cassandra.rb
Expand Up @@ -741,40 +741,42 @@ def get_range_single(column_family, options = {})
# range of keys in the column_family you request.
#
# If a block is passed in we will yield the row key and columns for
# each record returned.
# each record returned and return a nil value instead of a Cassandra::OrderedHash.
#
# See Cassandra#get_range for more details.
#
def get_range_batch(column_family, options = {})
batch_size = options.delete(:batch_size) || 100
count = options.delete(:key_count)
result = {}
result = (!block_given? && {}) || nil
num_results = 0

options[:start_key] ||= ''
last_key = nil

while options[:start_key] != last_key && (count.nil? || count > result.length)
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,
:key_count => batch_size,
:return_empty_rows => true
))
res.each do |key, columns|
next if options[:start_key] == key
next if result.length == count
next if num_results == count

unless columns == {}
if block_given?
yield key, columns
else
result[key] = columns
end
num_results += 1
end
last_key = key
end
end

result if !block_given?
result
end

##
Expand Down

0 comments on commit 00a48e1

Please sign in to comment.