Permalink
Browse files

Add limit to finds; fix drop_db

  • Loading branch information...
1 parent 2231005 commit 81157c25edacec50a3e7a68c4e68fc900afb553b @gdb committed Jul 11, 2011
View
@@ -8,7 +8,7 @@ module EmbeddedMongo
def self.log
unless @log
@log = Logger.new(STDOUT)
- @log.level = Logger::DEBUG
+ @log.level = Logger::WARN
end
@log
end
@@ -16,10 +16,16 @@ def insert_documents(documents)
documents.map { |doc| doc['_id'] }
end
- def find(selector)
+ def find(selector, opts)
+ limit = opts.delete(:limit)
+ raise ArgumentError.new("Unrecognized opts: #{opts.inspect}") unless opts.empty?
+
results = []
@data.each do |doc|
- results << doc if selector_match?(selector, doc)
+ if selector_match?(selector, doc)
+ results << doc
+ break if limit > 0 and results.length >= limit
+ end
end
EmbeddedMongo.log.info("Query has #{results.length} matches")
results
@@ -11,7 +11,7 @@ def initialize(manager, name)
def run_command(cmd)
if cmd['dropDatabase']
- @manager.drop_db($name)
+ @manager.drop_db(@name)
[{
'dropped' => @name,
'ok' => 1.0
@@ -12,15 +12,15 @@ def insert_documents(db_name, collection_name, documents)
collection.insert_documents(documents)
end
- def find(db_name, collection_name, selector)
+ def find(db_name, collection_name, selector, opts)
EmbeddedMongo::Util.stringify_hash!(selector)
EmbeddedMongo.log.info("FIND: #{db_name.inspect} #{collection_name.inspect} #{selector.inspect}")
if collection_name == '$cmd'
db = get_db(db_name)
return db.run_command(selector)
end
collection = get_collection(db_name, collection_name)
- collection.find(selector)
+ collection.find(selector, opts)
end
def update(db_name, collection_name, selector, update, opts)
@@ -8,7 +8,7 @@ def send_initial_query
if @query_run
false
else
- results = @connection.request(:find, @db.name, @collection.name, selector)
+ results = @connection.request(:find, @db.name, @collection.name, selector, :limit => @limit)
@returned += results.length
@cache += results
@query_run = true

0 comments on commit 81157c2

Please sign in to comment.