Skip to content
Browse files

Add dropping of databases

  • Loading branch information...
1 parent 8680698 commit fb3e6d7b6376974471c0747ab20926567d893914 @gdb committed Jul 10, 2011
View
13 lib/embedded-mongo/backend/collection.rb
@@ -4,21 +4,19 @@ module EmbeddedMongo::Backend
class Collection
class DuplicateKeyError < StandardError; end
- def initialize(name)
+ def initialize(db, name)
+ raise ArgumentError.new("Invalid collection name #{name.inspect}") if name['.'] or name['$']
+ @db = db
@name = name
@data = []
end
def insert_documents(documents)
- documents.each { |doc| EmbeddedMongo::Util.stringify_keys!(doc) }
-
documents.each { |doc| insert(doc) }
documents.map { |doc| doc['_id'] }
end
def find(selector)
- EmbeddedMongo::Util.stringify_keys!(selector)
-
results = []
@data.each do |doc|
results << doc if selector_match?(selector, doc)
@@ -28,13 +26,18 @@ def find(selector)
end
def update(selector, update, opts)
+
@data.each do |doc|
next unless selector_match?(selector, doc)
apply_update!(update, doc)
break unless opts[:multi]
end
end
+ def remove(selector={}, opts={})
+ @data.reject! { |doc| selector_match?(selector, doc) }
+ end
+
private
def check_id(doc)
View
15 lib/embedded-mongo/backend/db.rb
@@ -1,12 +1,23 @@
module EmbeddedMongo::Backend
class DB
- def initialize(name)
+ def initialize(manager, name)
+ raise ArgumentError.new("Invalid collection name #{name.inspect}") if name['.'] or name['$']
+ @manager = manager
@name = name
@collections = {}
end
+ def run_command(cmd)
+ if cmd['dropDatabase']
+ @manager.drop_db($name)
+ [{ 'dropped' => @name, 'ok' => 1.0 }]
+ else
+ raise NotImplementedError.new("Unrecognized command #{cmd.inspect}")
+ end
+ end
+
def get_collection(collection_name)
- @collections[collection_name] ||= Collection.new(collection_name)
+ @collections[collection_name] ||= Collection.new(self, collection_name)
end
end
end
View
22 lib/embedded-mongo/backend/manager.rb
@@ -6,27 +6,47 @@ def initialize(spec)
end
def insert_documents(db_name, collection_name, documents)
+ documents.each { |doc| EmbeddedMongo::Util.stringify_keys!(doc) }
EmbeddedMongo.log.info("INSERT: #{db_name.inspect} #{collection_name.inspect} #{documents.inspect}")
collection = get_collection(db_name, collection_name)
collection.insert_documents(documents)
end
def find(db_name, collection_name, selector)
+ EmbeddedMongo::Util.stringify_keys!(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)
end
def update(db_name, collection_name, selector, update, opts)
+ EmbeddedMongo::Util.stringify_keys!(selector)
+ EmbeddedMongo::Util.stringify_keys!(update)
EmbeddedMongo.log.info("FIND: #{db_name.inspect} #{collection_name.inspect} #{selector.inspect} #{update.inspect} #{opts.inspect}")
collection = get_collection(db_name, collection_name)
collection.update(selector, update, opts)
end
+ def remove(db_name, collection_name, selector, opts)
+ EmbeddedMongo::Util.stringify_keys!(selector)
+ EmbeddedMongo.log.info("REMOVE: #{db_name.inspect} #{collection_name.inspect} #{selector.inspect} #{opts.inspect}")
+ collection = get_collection(db_name, collection_name)
+ collection.remove(selector, opts)
+ end
+
+ # Management functions
+ def drop_db(db_name)
+ @dbs.delete(db_name)
+ end
+
private
def get_db(db_name)
- @dbs[db_name] ||= DB.new(db_name)
+ @dbs[db_name] ||= DB.new(self, db_name)
end
def get_collection(db_name, collection_name)
View
6 lib/embedded-mongo/collection.rb
@@ -1,6 +1,7 @@
module EmbeddedMongo
class Collection < Mongo::Collection
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
+ # TODO: do something with check_keys / safe
EmbeddedMongo.log.debug("insert_documents: #{documents.inspect}, #{collection_name.inspect}, #{check_keys.inspect}, #{safe.inspect}")
@connection.request(:insert_documents, @db.name, collection_name, documents)
end
@@ -11,6 +12,11 @@ def update(selector, document, opts={})
@connection.request(:update, @db.name, @name, selector, document, opts)
end
+ def remove(selector={}, opts={})
+ opts = { :safe => @safe }.merge(opts)
+ @connection.request(:remove, @db.name, @name, selector, opts)
+ end
+
# verbatim
def find(selector={}, opts={})
fields = opts.delete(:fields)
View
23 lib/embedded-mongo/db.rb
@@ -1,7 +1,28 @@
module EmbeddedMongo
class DB < Mongo::DB
+ # mostly verbatim
def command(selector, opts={})
- puts "COMMAND: #{selector.inspect}, opts: #{opts.inspect}"
+ check_response = opts.fetch(:check_response, true)
+ socket = opts[:socket]
+ raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
+ if selector.keys.length > 1 && RUBY_VERSION < '1.9' && selector.class != BSON::OrderedHash
+ raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
+ end
+
+ begin
+ result = Cursor.new(system_command_collection,
+ :limit => -1, :selector => selector, :socket => socket).next_document
+ rescue Mongo::OperationFailure => ex
+ raise OperationFailure, "Database command '#{selector.keys.first}' failed: #{ex.message}"
+ end
+
+ if result.nil?
+ raise Mongo::OperationFailure, "Database command '#{selector.keys.first}' failed: returned null."
+ elsif (check_response && !ok?(result))
+ raise Mongo::OperationFailure, "Database command '#{selector.keys.first}' failed: #{result.inspect}"
+ else
+ result
+ end
end
# verbatim
View
13 test/functional/test_interface.rb
@@ -8,6 +8,7 @@ def setup
# Tests should pass with either of the following lines
@conn = EmbeddedMongo::Connection.new
# @conn = Mongo::Connection.new
+
@test_db = @conn['test']
@foo_collection = @test_db['foo']
end
@@ -39,4 +40,16 @@ def test_changing_ids
assert_equal(1, cursor.count)
assert_equal({ '_id' => 'other_id' }, cursor.first)
end
+
+ def test_remove
+ id1 = @foo_collection.insert({ 'lime' => 'limit' })
+ id2 = @foo_collection.insert({ 'lemon' => 'limit' })
+ @foo_collection.remove({ 'lime' => 'limit' })
+
+ cursor = @foo_collection.find({ '_id' => id1 })
+ assert_equal(0, cursor.count)
+ cursor = @foo_collection.find({ '_id' => id2 })
+ assert_equal(1, cursor.count)
+ assert_equal({ '_id' => id2, 'lemon' => 'limit' }, cursor.first)
+ end
end

0 comments on commit fb3e6d7

Please sign in to comment.
Something went wrong with that request. Please try again.