Skip to content
Browse files

Merge pull request #40 from rightscale/azure_12935_get_indexed_optimi…

…zation

Optimize memory usage for get_indexed in cassandra_model
  • Loading branch information...
2 parents 938ed9d + a9a4485 commit 358a610e89059816f2a78619a03835d8c541f2e5 @ryanwilliamson ryanwilliamson committed Jun 22, 2012
Showing with 24 additions and 31 deletions.
  1. +1 −1 Gemfile.lock
  2. +21 −28 lib/right_support/db/cassandra_model.rb
  3. +2 −2 right_support.gemspec
View
2 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- right_support (2.1.0)
+ right_support (2.2.0)
GEM
remote: http://rubygems.org/
View
49 lib/right_support/db/cassandra_model.rb
@@ -273,7 +273,7 @@ def real_get(k, opt = {})
opt = opt.clone
opt[:count] = DEFAULT_COUNT
columns = Cassandra::OrderedHash.new
- while true
+ loop do
chunk = do_op(:get, column_family, k, opt)
columns.merge!(chunk)
if chunk.size == opt[:count]
@@ -296,6 +296,10 @@ def real_get(k, opt = {})
# columns(Array|nil):: Names of columns to be retrieved, defaults to all
# opt(Hash):: Request options with only :consistency used
#
+ # === Block
+ # Optional block that is yielded each chunk as it is retrieved as an array
+ # like the normally returned result
+ #
# === Return
# (OrderedHash):: Rows retrieved with each key, value is columns
def get_all_indexed_slices(index, key, columns = nil, opt = {})
@@ -329,40 +333,29 @@ def get_all_indexed_slices(index, key, columns = nil, opt = {})
#
# === Return
# (Array):: Rows retrieved with each member being an instantiated object of the
- # given class as value, but object only contains values for the columns retrieved
+ # given class as value, but object only contains values for the columns retrieved;
+ # array is always empty if a block is given
def get_indexed(index, key, columns = nil, opt = {})
- if rows = real_get_indexed(index, key, columns, opt)
- rows.map do |key, columns|
- attrs = columns.inject({}) { |a, c| a[c.column.name] = c.column.value; a }
- new(key, attrs)
- end
- else
- []
- end
- end
-
- # Get all raw rows for specified secondary key
- #
- # === Parameters
- # index(String):: Name of secondary index
- # key(String):: Index value that each selected row is required to match
- # columns(Array|nil):: Names of columns to be retrieved, defaults to all
- # opt(Hash):: Request options with only :consistency used
- #
- # === Return
- # (Hash):: Rows retrieved with primary key as key and value being an array
- # of CassandraThrift::ColumnOrSuperColumn with attributes :name, :timestamp,
- # and :value
- def real_get_indexed(index, key, columns = nil, opt = {})
- rows = {}
+ rows = []
start = ""
count = DEFAULT_COUNT
expr = do_op(:create_idx_expr, index, key, "EQ")
opt = opt[:consistency] ? {:consistency => opt[:consistency]} : {}
- while true
+ loop do
clause = do_op(:create_idx_clause, [expr], start, count)
chunk = do_op(:get_indexed_slices, column_family, clause, columns, opt)
- rows.merge!(chunk)
+ chunk_rows = []
+ chunk.each do |row_key, row_columns|
+ if row_columns && row_key != start
+ attrs = row_columns.inject({}) { |a, c| a[c.column.name] = c.column.value; a }
+ chunk_rows << new(row_key, attrs)
+ end
+ end
+ if block_given?
+ yield chunk_rows
+ else
+ rows.concat(chunk_rows)
+ end
if chunk.size == count
# Assume there are more chunks, use last key as start of next get
start = chunk.keys.last
View
4 right_support.gemspec
@@ -7,8 +7,8 @@ spec = Gem::Specification.new do |s|
s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
s.name = 'right_support'
- s.version = '2.1.0'
- s.date = '2012-06-16'
+ s.version = '2.2.0'
+ s.date = '2012-06-22'
s.authors = ['Tony Spataro', 'Sergey Sergyenko', 'Ryan Williamson', 'Lee Kirchhoff', 'Sergey Enin']
s.email = 'support@rightscale.com'

0 comments on commit 358a610

Please sign in to comment.
Something went wrong with that request. Please try again.