From b2f97938b7914156b09b352eacf37332e71df405 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Fri, 20 Sep 2024 14:18:28 -0600 Subject: [PATCH 1/3] short-circuit the logic in extract_attribute to fix performance regression --- lib/mongoid/matcher.rb | 7 +++++++ .../association/referenced/belongs_to/proxy_spec.rb | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/lib/mongoid/matcher.rb b/lib/mongoid/matcher.rb index 034fb071ef..d0b4305e45 100644 --- a/lib/mongoid/matcher.rb +++ b/lib/mongoid/matcher.rb @@ -40,6 +40,13 @@ module Matcher # # @return [ Object | Array ] Field value or values. module_function def extract_attribute(document, key) + # Performance optimization; if the key does not include a '.' character, + # it must reference an immediate attribute of the document. + unless key.include?('.') + hash = document.respond_to?(:attributes) ? document.attributes : document + return [ hash[key] ] if hash.key?(key) + end + if document.respond_to?(:as_attributes, true) # If a document has hash fields, as_attributes would keep those fields # as Hash instances which do not offer indifferent access. diff --git a/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb b/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb index d19a69a372..373456cddc 100644 --- a/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +++ b/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb @@ -749,6 +749,10 @@ person.save! end + # NOTE: there as a bad interdependency here, with the auto_save_spec.rb + # file. If auto_save_spec.rb runs before this, the following specs fail + # with "undefined method `nullify' for an instance of Person". + context "when parent exists" do context "when child is destroyed" do From 75923ca866a31351710c6f6eef01c2647ea6f2b7 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Fri, 20 Sep 2024 14:57:50 -0600 Subject: [PATCH 2/3] short circuit on anything that's not a document or hash --- lib/mongoid/matcher.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/mongoid/matcher.rb b/lib/mongoid/matcher.rb index d0b4305e45..aa976aed33 100644 --- a/lib/mongoid/matcher.rb +++ b/lib/mongoid/matcher.rb @@ -35,11 +35,17 @@ module Matcher # from and behaves identically to association traversal for the purposes # of, for example, subsequent array element retrieval. # - # @param [ Document | Hash ] document The document to extract from. + # @param [ Document | Hash | String ] document The document to extract from. # @param [ String ] key The key path to extract. # # @return [ Object | Array ] Field value or values. module_function def extract_attribute(document, key) + # The matcher system will wind up sending atomic values to this as well, + # when attepting to match more complex types. If anything other than a + # Document or a Hash is given, we'll short-circuit the logic and just + # return an empty array. + return [] unless document.is_a?(Hash) || document.is_a?(Document) + # Performance optimization; if the key does not include a '.' character, # it must reference an immediate attribute of the document. unless key.include?('.') From 0862bcd880d4284de84f7835c92e0727f5625613 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 23 Sep 2024 09:54:24 -0600 Subject: [PATCH 3/3] handle hash/string keys --- lib/mongoid/matcher.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mongoid/matcher.rb b/lib/mongoid/matcher.rb index aa976aed33..26e8a251cf 100644 --- a/lib/mongoid/matcher.rb +++ b/lib/mongoid/matcher.rb @@ -50,7 +50,8 @@ module Matcher # it must reference an immediate attribute of the document. unless key.include?('.') hash = document.respond_to?(:attributes) ? document.attributes : document - return [ hash[key] ] if hash.key?(key) + key = find_exact_key(hash, key) + return key ? [ hash[key] ] : [] end if document.respond_to?(:as_attributes, true)