Browse files

Optimize each and to_a to use find_each instead of all.

all loads every doc into memory, find_each creates a cursor and loads as you keep reqeusting.
  • Loading branch information...
1 parent 412cdeb commit 83ad596c4ec8eccf37c0e288f96b46f64a5501a3 @jnunemaker jnunemaker committed Apr 23, 2011
Showing with 6 additions and 2 deletions.
  1. +6 −2 lib/plucky/query.rb
View
8 lib/plucky/query.rb
@@ -14,7 +14,7 @@ class Query
attr_reader :criteria, :options, :collection
def_delegator :criteria, :simple?
def_delegator :options, :fields?
- def_delegators :to_a, :each, :include?
+ def_delegators :to_a, :include?
def initialize(collection, opts={})
@collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
@@ -84,6 +84,10 @@ def last(opts={})
clone.update(opts).reverse.find_one
end
+ def each
+ find_each.each { |doc| yield(doc) }
+ end
+
def remove(opts={})
query = clone.update(opts)
query.collection.remove(query.criteria.to_hash)
@@ -157,7 +161,7 @@ def exists?(options={})
alias :exist? :exists?
def to_a
- all
+ find_each.to_a
end
def [](key)

0 comments on commit 83ad596

Please sign in to comment.