Permalink
Browse files

Add upsert, extend query support, and improve last_error

  • Loading branch information...
1 parent d85b363 commit 2231005290150e531dee2fa4c4ccc1bfae16e9a1 @gdb committed Jul 11, 2011
View
20 lib/embedded-mongo/backend/collection.rb
@@ -26,11 +26,25 @@ def find(selector)
end
def update(selector, update, opts)
+ # TODO: return value?
+ multi = opts.delete(:multi)
+ upsert = opts.delete(:upsert)
+ safe = opts.delete(:safe) # TODO: do something with this
+ raise ArgumentError.new("Unrecognized opts: #{opts.inspect}") unless opts.empty?
+ n = 0
@data.each do |doc|
next unless selector_match?(selector, doc)
apply_update!(update, doc)
- break unless opts[:multi]
+ n += 1
+ break unless multi
+ end
+
+ if n == 0 and upsert
+ insert(update)
+ @db.set_last_error({ 'updatedExisting' => false, 'upserted' => update['_id'], 'n' => 1 })
+ else
+ @db.set_last_error({ 'updatedExisting' => n > 0, 'n' => n })
end
end
@@ -69,7 +83,7 @@ def selector_match?(selector, doc)
def partial_match?(partial_selector, value)
EmbeddedMongo.log.debug("partial_match? #{partial_selector.inspect} #{value.inspect}")
case partial_selector
- when Array, String, BSON::ObjectId, nil
+ when Array, Numeric, String, BSON::ObjectId, Boolean, nil
partial_selector == value
when Hash
if partial_selector.all? { |k, v| !k.start_with?('$') }
@@ -102,6 +116,8 @@ def directive_match?(directive_key, directive_value, value)
when '$in'
raise NotImplementedError.new("Only implemented for arrays: #{directive_value.inspect}") unless directive_value.kind_of?(Array)
directive_value.include?(value)
+ when '$ne'
+ directive_value != value
else
raise NotImplementedError.new("Have yet to implement: #{directive_key}")
# raise Mongo::OperationFailure.new("invalid operator: #{directive_key}")
View
18 lib/embedded-mongo/backend/db.rb
@@ -6,8 +6,7 @@ def initialize(manager, name)
@name = name
@collections = {}
- @last_error = nil
- @n = 0
+ set_last_error({})
end
def run_command(cmd)
@@ -29,19 +28,22 @@ def run_command(cmd)
'ok' => 1.0
}]
elsif cmd['getlasterror']
- # TODO: populate @last_error / @n
- [{
- 'err' => @last_error,
- 'n' => @n,
- 'ok' => 1.0
- }]
+ [@last_error]
elsif cmd.has_key?('$eval')
raise NotImplementedError.new('Does not currently support $eval commands')
else
raise NotImplementedError.new("Unrecognized command #{cmd.inspect}")
end
end
+ def set_last_error(opts)
+ @last_error = {
+ 'err' => nil,
+ 'n' => 0,
+ 'ok' => 1.0
+ }.merge(opts)
+ end
+
def get_collection(collection_name)
@collections[collection_name] ||= Collection.new(self, collection_name)
end
View
2 lib/embedded-mongo/backend/manager.rb
@@ -26,7 +26,7 @@ def find(db_name, collection_name, selector)
def update(db_name, collection_name, selector, update, opts)
EmbeddedMongo::Util.stringify_hash!(selector)
EmbeddedMongo::Util.stringify_hash!(update)
- EmbeddedMongo.log.info("FIND: #{db_name.inspect} #{collection_name.inspect} #{selector.inspect} #{update.inspect} #{opts.inspect}")
+ EmbeddedMongo.log.info("UPDATE: #{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
View
2 lib/embedded-mongo/util.rb
@@ -19,7 +19,7 @@ def self.stringify!(struct)
struct.each_with_index { |entry, i| struct[i] = stringify!(entry) }
when Symbol
struct.to_s
- when String, Integer, BSON::ObjectId, nil
+ when String, Numeric, BSON::ObjectId, Regexp, Boolean, nil
struct
else
raise "Cannot stringify #{struct.inspect}"

0 comments on commit 2231005

Please sign in to comment.