Permalink
Browse files

Modified view_delegator to transparently load doc ids or docs, making…

… only a

single request in both cases.
  • Loading branch information...
1 parent dfe00c1 commit 1da09213067f2b9503d18c314b1332d50633d3db @paulcarey committed Apr 15, 2010
Showing with 70 additions and 13 deletions.
  1. +3 −0 lib/relaxdb/all_delegator.rb
  2. +4 −5 lib/relaxdb/document.rb
  3. +5 −4 lib/relaxdb/relaxdb.rb
  4. +26 −3 lib/relaxdb/view_by_delegator.rb
  5. +32 −1 spec/view_by_spec.rb
@@ -37,6 +37,9 @@ def size
size || 0
end
+ #
+ # TODO - needs updating
+ #
# TODO: destroy in a bulk_save if feasible
def destroy!
load!
View
@@ -461,7 +461,7 @@ def self.view_docs_by *atts
#
def self.view_by *atts
opts = atts.last.is_a?(Hash) ? atts.pop : {}
- opts = opts.merge :raw => true, :reduce => false
+ opts = opts.merge :reduce => false
__view_by_list__ << atts
if RelaxDB.create_views?
@@ -473,12 +473,11 @@ def self.view_by *atts
define_method by_name do |*params|
view_name = "#{self.name}_#{by_name}"
if params.empty?
- ViewByDelegator.new(RelaxDB.doc_ids(view_name, opts))
+ ViewByDelegator.new(view_name, opts)
elsif params[0].is_a? Hash
- ViewByDelegator.new(RelaxDB.doc_ids(view_name, opts.merge(params[0])))
+ ViewByDelegator.new(view_name, opts.merge(params[0]))
else
- ViewByDelegator.new(
- RelaxDB.doc_ids(view_name, opts.merge(:key => params[0]))).load!.first
+ ViewByDelegator.new(view_name, opts.merge(:key => params[0])).load!.first
end
end
end
View
@@ -150,12 +150,13 @@ def load!(ids)
end
#
- # Queries a view that doesn't emit the underlying doc as a val
- # and loads the underlying docs in a separate query.
+ # Queries a view that doesn't emit the underlying doc as a val.
#
def docs view_name, params
- ids = doc_ids view_name, params
- ids.empty? ? [] : RelaxDB.load!(ids)
+ params[:raw] = true
+ params[:include_docs] = true
+ hash = view view_name, params
+ hash["rows"].map { |row| row["doc"] ? create_obj_from_doc(row["doc"]) : nil }
end
def doc_ids view_name, params
@@ -1,11 +1,34 @@
module RelaxDB
- class ViewByDelegator < DelegateClass(Array)
+ class ViewByDelegator < Delegator
+
+ def initialize(view_name, opts)
+ super([])
+ @view_name = view_name
+ @opts = opts
+ end
+
+ def __getobj__
+ unless @ids
+ @opts[:raw] = true
+ @ids = RelaxDB.doc_ids @view_name, @opts
+ end
+ @ids
+ end
+
+ def __setobj__ obj
+ # Intentionally empty
+ end
def load!
- RelaxDB.load! self.to_a
+ if @ids
+ RelaxDB.load! @ids
+ else
+ @opts[:include_docs] = true
+ RelaxDB.docs @view_name, @opts
+ end
end
-
+
end
end
View
@@ -47,7 +47,38 @@
docs = Primitives.by_str.load!
docs.map { |d| d.str }.join.length.should == 11
end
+
+ end
+
+ describe "request count" do
+
+ before(:each) do
+ RelaxDB.db.reset_req_count
+ end
+
+ it "should not issue any requests before accessingn the delegator" do
+ doc_ids = Primitives.by_str
+ RelaxDB.db.req_count.should == 0
+ end
+
+ it "should issue a single request when retrieving ids only" do
+ doc_ids = Primitives.by_str
+ doc_ids[0]
+ RelaxDB.db.req_count.should == 1
+ end
+
+ it "should make two requests when loading docs after touching ids" do
+ doc_ids = Primitives.by_str
+ doc_ids[0].should == "id1"
+ doc_ids.load!
+ RelaxDB.db.req_count.should == 2
+ end
+
+ it "should issue a single request when retrieving docs directly" do
+ Primitives.by_str.load!
+ RelaxDB.db.req_count.should == 1
+ end
end
-end
+end

0 comments on commit 1da0921

Please sign in to comment.