Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't iterate the whole cursor twice when using IdentityMap with #all…

…. Improve performance by avoiding explicit block bindings, extraneous method calls, and extraneous array creation.
  • Loading branch information...
commit ab77215361ccbd03753976210625da829a7c57d9 1 parent d1eb6bc
@cheald cheald authored
Showing with 8 additions and 7 deletions.
  1. +8 −7 lib/mongo_mapper/plugins/identity_map.rb
View
15 lib/mongo_mapper/plugins/identity_map.rb 100644 → 100755
@@ -59,9 +59,10 @@ def get_from_identity_map(id)
module IdentityMapQueryMethods
def all(opts={})
- query = clone.amend(opts)
- super.tap do |docs|
- model.remove_documents_from_map(docs) if query.fields?
+ [].tap do |docs|
+ find_each(opts) do |doc|
+ docs.push doc
+ end
end
end
@@ -72,16 +73,16 @@ def find_one(opts={})
document
else
super.tap do |doc|
- model.remove_documents_from_map(doc) if query.fields?
+ doc.remove_from_identity_map if doc and query.fields?
end
end
end
- def find_each(opts={}, &block)
+ def find_each(opts={})
query = clone.amend(opts)
super(opts) do |doc|
- model.remove_documents_from_map(doc) if query.fields?
- block.call(doc) unless block.nil?
+ doc.remove_from_identity_map if doc and query.fields?
+ yield doc if block_given?
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.