finalized lazy-loading work on Finder

This commit is contained in:
2011-03-09 01:25:02 +00:00
parent 7e8e1dacc7
commit 33e9477552
2 changed files with 71 additions and 31 deletions

View File

@@ -49,6 +49,10 @@ module Redistat
@result ||= find
end
def total
all.total
end
def each(&block)
all.each(&block)
end
@@ -61,10 +65,6 @@ module Redistat
all.each_with_index(&block)
end
def reset!
@result = nil
end
def connection_ref(ref)
reset! if @options[:connection_ref] != ref
@options[:connection_ref] = ref
@@ -168,9 +168,9 @@ module Redistat
end
col
end
def db
super(@options[:connection_ref])
def reset!
@result = nil
end
def valid_options?
@@ -204,5 +204,9 @@ module Redistat
sum
end
def db
super(@options[:connection_ref])
end
end
end

View File

@@ -61,29 +61,6 @@ describe Redistat::Finder do
stats.first.should == stats.total
end
it "should be lazy-loaded" do
first_stat, last_stat = create_example_stats
finder = Redistat::Finder.new
finder.from(first_stat).till(last_stat)
finder.scope(@scope).label(@label)
finder.depth(:hour)
finder.instance_variable_get("@result").should be_nil
stats = finder.all
finder.instance_variable_get("@result").should_not be_nil
stats.total.should == { "views" => 12, "visitors" => 8 }
stats.total.from.should == first_stat
stats.total.till.should == last_stat
stats.first.should == stats.total
finder.all.object_id.should == stats.object_id
finder.from(first_stat + 1.hour)
finder.instance_variable_get("@result").should be_nil
finder.all.object_id.should_not == stats.object_id
end
it "should fetch data per unit when interval option is specified" do
first_stat, last_stat = create_example_stats
@@ -112,6 +89,65 @@ describe Redistat::Finder do
lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions)
end
describe "Lazy-Loading" do
before(:each) do
@first_stat, @last_stat = create_example_stats
@finder = Redistat::Finder.new
@finder.from(@first_stat).till(@last_stat).scope(@scope).label(@label).depth(:hour)
@match = [{}, {"visitors"=>"4", "views"=>"6"},
{"visitors"=>"2", "views"=>"3"},
{"visitors"=>"2", "views"=>"3"}, {}]
end
it "should lazy-load" do
@finder.instance_variable_get("@result").should be_nil
stats = @finder.all
@finder.instance_variable_get("@result").should_not be_nil
stats.should == @finder.find # find method directly fetches results
stats.total.should == @finder.total
stats.total.should == { "views" => 12, "visitors" => 8 }
stats.total.from.should == @first_stat
stats.total.till.should == @last_stat
stats.first.should == stats.total
@finder.all.object_id.should == stats.object_id
@finder.from(@first_stat + 2.hours)
@finder.instance_variable_get("@result").should be_nil
@finder.all.object_id.should_not == stats.object_id
stats = @finder.all
stats.total.should == { "views" => 6, "visitors" => 4 }
end
it "should handle #map" do
@finder.interval(:hour)
@finder.map { |r| r }.should == @match
end
it "should handle #each" do
@finder.interval(:hour)
res = []
@finder.each { |r| res << r }
res.should == @match
end
it "should handle #each_with_index" do
@finder.interval(:hour)
res = {}
match = {}
@finder.each_with_index { |r, i| res[i] = r }
@match.each_with_index { |r, i| match[i] = r }
res.should == match
end
end
# helper methods
@@ -120,7 +156,7 @@ describe Redistat::Finder do
Redistat::Summary.update(key, @stats, :hour)
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 13:53"))
Redistat::Summary.update(key, @stats, :hour)
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 14:32"))
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 14:52"))
Redistat::Summary.update(key, @stats, :hour)
key = Redistat::Key.new(@scope, @label, (last = Time.parse("2010-05-14 15:02")))
Redistat::Summary.update(key, @stats, :hour)