Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixes issue with find! not throwing error if ids in array #469

Closed
wants to merge 2 commits into from

1 participant

This page is out of date. Refresh to see the latest.
View
52 lib/mongo_mapper/plugins/identity_map.rb
@@ -54,18 +54,49 @@ def self.without
module ClassMethods
# Private - Looks for a document in the identity map
def get_from_identity_map(id)
- IdentityMap.repository[IdentityMap.key(self, id)]
+ IdentityMap.repository[IdentityMap.key(self, id)].tap do |doc|
+ if MongoMapper.logger and doc
+ MongoMapper.logger.info "MONGODB IDENTITY MAP HIT FOR #{id}" if IdentityMap.enabled? and MongoMapper.logger
+ else
+ MongoMapper.logger.info "MONGODB IDENTITY MAP MISS #{id} :-(" if IdentityMap.enabled? and MongoMapper.logger
+ end
+ end
end
module IdentityMapQueryMethods
+
+ # def all(opts={})
+ # MongoMapper.logger.info ">>> in all"
+ # query = clone.amend(opts)
+ # super.tap do |docs|
+ # model.remove_documents_from_map(docs) if query.fields?
+ # end
+ # end
+
def all(opts={})
+ MongoMapper.logger.info ">>> in all" if MongoMapper.logger
query = clone.amend(opts)
- super.tap do |docs|
- model.remove_documents_from_map(docs) if query.fields?
+ [].tap do |docs|
+ if IdentityMap.enabled? && query.simple?
+ if query[:_id].is_a?(Hash) && ids = query[:_id]["$in"]
+ if query[:sort].nil?
+ ids.each do |id|
+ if doc = model.get_from_identity_map(id)
+ docs << doc
+ ids.delete id
+ end
+ end
+ break docs if ids.empty?
+ end
+ else break [find_one(opts)].compact
+ end
+ end
+ find_each(query.criteria) { |doc| docs << doc }
end
end
def find_one(opts={})
+ MongoMapper.logger.info ">>> in find_one" if MongoMapper.logger
query = clone.amend(opts)
if IdentityMap.enabled? && query.simple? && (document = model.get_from_identity_map(query[:_id]))
@@ -76,7 +107,7 @@ def find_one(opts={})
end
end
end
-
+
def find_each(opts={}, &block)
query = clone.amend(opts)
super(opts) do |doc|
@@ -84,9 +115,14 @@ def find_each(opts={}, &block)
block.call(doc) unless block.nil?
end
end
+
+
end
+
+
def query(opts={})
+ MongoMapper.logger.info ">>> in query" if MongoMapper.logger
super.extend(IdentityMapQueryMethods)
end
@@ -97,6 +133,10 @@ def remove_documents_from_map(*documents)
end
def load(attrs)
+ if MongoMapper.logger
+ MongoMapper.logger.info ">>> in load"
+ MongoMapper.logger.info ">>> but leaving because attrs.nil?" if attrs.nil?
+ end
return nil if attrs.nil?
document = get_from_identity_map(attrs['_id'])
@@ -119,6 +159,7 @@ def delete
def add_to_identity_map
if IdentityMap.enabled?
+ MongoMapper.logger.info ">>> adding in add_to_identity_map" if MongoMapper.logger
key = IdentityMap.key(self.class, _id)
IdentityMap.repository[key] = self
end
@@ -126,10 +167,11 @@ def add_to_identity_map
def remove_from_identity_map
if IdentityMap.enabled?
+ MongoMapper.logger.info ">>> removing in remove_from_identity_map" if MongoMapper.logger
key = IdentityMap.key(self.class, _id)
IdentityMap.repository.delete(key)
end
end
end
end
-end
+end
View
8 lib/mongo_mapper/plugins/querying/decorator.rb
@@ -14,11 +14,11 @@ def model(model=nil)
end
def find!(*ids)
- raise DocumentNotFound, "Couldn't find without an ID" if ids.size == 0
-
+ norm_ids = ids.flatten.compact.uniq
+ raise DocumentNotFound, "Couldn't find without an ID" if norm_ids.size == 0
find(*ids).tap do |result|
- if result.nil? || ids.size != Array(result).size
- raise DocumentNotFound, "Couldn't find all of the ids (#{ids.join(',')}). Found #{Array(result).size}, but was expecting #{ids.size}"
+ if result.nil? || norm_ids.size != Array(result).size
+ raise DocumentNotFound, "Couldn't find all of the ids (#{norm_ids.join(',')}). Found #{Array(result).size}, but was expecting #{norm_ids.size}"
end
end
end
View
6 test/functional/test_querying.rb
@@ -216,6 +216,12 @@ def setup
end
end
+ should "raise error if not all found when using find! with an array" do
+ assert_raises(MongoMapper::DocumentNotFound) do
+ @document.find!([@doc1._id, BSON::ObjectId.new.to_s])
+ end
+ end
+
should "return array if array with one element" do
@document.find([@doc1._id]).should == [@doc1]
end
Something went wrong with that request. Please try again.