Skip to content

Commit

Permalink
Optimize each and to_a to use find_each instead of all.
Browse files Browse the repository at this point in the history
all loads every doc into memory, find_each creates a cursor and loads as you keep reqeusting.
  • Loading branch information
jnunemaker committed Apr 23, 2011
1 parent 412cdeb commit 83ad596
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/plucky/query.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -157,7 +161,7 @@ def exists?(options={})
alias :exist? :exists?

def to_a
all
find_each.to_a
end

def [](key)
Expand Down

0 comments on commit 83ad596

Please sign in to comment.