Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
new :instantiate_only_requested_columns finder option that does not i…
Browse files Browse the repository at this point in the history
…nstantiate object columns if they were not included in the :select
  • Loading branch information
tylerkovacs committed Oct 12, 2009
1 parent c27ed89 commit 69365ed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,4 +1,7 @@
0.2.8 (not yet released)
- new :instantiate_only_requested_columns finder option that does not
instantiate object columns if they were not included in the :select
- makes large queries against large tables memory-friendly
- :select option allows column families to be identified using symbols
- tests and groundwork for mutator flush_interval (asynchronous writes)
- requires Hypertable 0.9.2.7
Expand Down
Expand Up @@ -251,7 +251,7 @@ def convert_options_to_scan_spec(options={})
status, family, qualifier = is_qualified_column_name?(column)
family
end.uniq
when :table_name, :start_row, :end_row, :start_inclusive, :end_inclusive, :select, :columns, :row_keys, :conditions, :include, :readonly, :scan_spec
when :table_name, :start_row, :end_row, :start_inclusive, :end_inclusive, :select, :columns, :row_keys, :conditions, :include, :readonly, :scan_spec, :instantiate_only_requested_columns
# ignore
else
raise "Unrecognized scan spec option: #{key}"
Expand Down
11 changes: 7 additions & 4 deletions lib/hyper_record.rb
Expand Up @@ -357,13 +357,13 @@ def find_by_options(options)
# puts msg
end

convert_cells_to_instantiated_rows(cells)
convert_cells_to_instantiated_rows(cells, options)
end

# Converts cells that come back from Hypertable into hashes. Each
# hash represents a separate record (where each cell that has the same
# row key is considered one record).
def convert_cells_to_hashes(cells)
def convert_cells_to_hashes(cells, options={})
rows = []
current_row = {}

Expand Down Expand Up @@ -392,7 +392,10 @@ def convert_cells_to_hashes(cells)
# Make sure that the resulting object has attributes for all
# columns - even ones that aren't in the response (due to limited
# select)

for col in column_families_without_row_key
next if options[:instantiate_only_requested_columns] && !options[:select].include?(col.name)

if !current_row.has_key?(col.name)
if col.is_a?(ActiveRecord::ConnectionAdapters::QualifiedColumn)
current_row[col.name] = {}
Expand All @@ -410,8 +413,8 @@ def convert_cells_to_hashes(cells)
rows
end

def convert_cells_to_instantiated_rows(cells)
convert_cells_to_hashes(cells).map{|row| instantiate(row)}
def convert_cells_to_instantiated_rows(cells, options={})
convert_cells_to_hashes(cells, options).map{|row| instantiate(row)}
end

# Return the records that match a specific HQL query.
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/hyper_record_spec.rb
Expand Up @@ -280,6 +280,19 @@ module HyperRecord
qp.misc.should == {}
end

it "should only instantiate requested columns when option set" do
p = Page.find("page_1")
p.name.should == "LOLcats and more"
p.url.should == "http://www.icanhascheezburger.com"

p = Page.find("page_1",
:select => 'name',
:instantiate_only_requested_columns => true)

p.name.should == "LOLcats and more"
lambda {p.url}.should raise_error(::ActiveRecord::MissingAttributeError)
end

it "should allow user to specify ROW key as part of initialize attributes" do
p = Page.new({:ROW => 'row key'})
p.ROW.should == 'row key'
Expand Down

0 comments on commit 69365ed

Please sign in to comment.