Skip to content

Commit

Permalink
Merge branch 'load_speed_fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheald committed Jul 3, 2013
2 parents 55b5930 + a60b04c commit 61aa557
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 133 deletions.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/extensions/binary.rb
Expand Up @@ -3,7 +3,7 @@ module MongoMapper
module Extensions
module Binary
def to_mongo(value)
if value.is_a?(::BSON::Binary)
if value.instance_of?(::BSON::Binary)
value
else
value.nil? ? nil : ::BSON::Binary.new(value)
Expand Down
39 changes: 20 additions & 19 deletions lib/mongo_mapper/extensions/boolean.rb
Expand Up @@ -3,37 +3,38 @@ module MongoMapper
module Extensions
module Boolean
Mapping = {
true => true,
'true' => true,
'TRUE' => true,
'True' => true,
't' => true,
'T' => true,
'1' => true,
1 => true,
true => true,
'true' => true,
'TRUE' => true,
'True' => true,
't' => true,
'T' => true,
'1' => true,
1 => true,
1.0 => true,
false => false,
'false' => false,
'FALSE' => false,
'False' => false,
'f' => false,
'F' => false,
'0' => false,
0 => false,
0.0 => false,
false => false,
'false' => false,
'FALSE' => false,
'False' => false,
'f' => false,
'F' => false,
'0' => false,
0 => false,
0.0 => false,
nil => nil
}

def to_mongo(value)
if value.is_a?(Boolean)
if value.instance_of?(Boolean)
value
else
Mapping[value]
end
end

def from_mongo(value)
value.nil? ? nil : !!value
return nil if value == nil
!!value
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo_mapper/extensions/date.rb
Expand Up @@ -3,18 +3,18 @@ module MongoMapper
module Extensions
module Date
def to_mongo(value)
if value.nil? || value == ''
if value.nil? || (value.instance_of?(String) && '' === value)
nil
else
date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
date = value.instance_of?(::Date) || value.instance_of?(::Time) ? value : ::Date.parse(value.to_s)
::Time.utc(date.year, date.month, date.day)
end
rescue
nil
end

def from_mongo(value)
value.to_date if value.present?
value.to_date if value
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo_mapper/extensions/string.rb
Expand Up @@ -3,11 +3,11 @@ module MongoMapper
module Extensions
module String
def to_mongo(value)
value.nil? ? nil : value.to_s
value && value.to_s
end

def from_mongo(value)
value.nil? ? nil : value.to_s
value && value.to_s
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions lib/mongo_mapper/extensions/time.rb
@@ -1,19 +1,21 @@
require 'active_support/core_ext/time/zones'

# encoding: UTF-8
module MongoMapper
module Extensions
module Time
def to_mongo(value)
if !value.present?
if !value || '' == value
nil
else
time_class = ::Time.try(:zone) || ::Time
time_class = ::Time.zone || ::Time
time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
at(time.to_f).utc if time # ensure milliseconds are preserved with to_f (issue #308)
end
end

def from_mongo(value)
if value and zone = ::Time.try(:zone)
if value and zone = ::Time.zone
value.in_time_zone(zone)
else
value
Expand Down
6 changes: 2 additions & 4 deletions lib/mongo_mapper/plugins/associations.rb
Expand Up @@ -78,10 +78,8 @@ def build_proxy(association)
end

def get_proxy(association)
unless proxy = self.instance_variable_get(association.ivar)
proxy = build_proxy(association)
end
proxy
proxy = self.instance_variable_get(association.ivar) if instance_variable_defined?(association.ivar)
proxy ||= build_proxy(association)
end

def save_to_collection(options={})
Expand Down
Expand Up @@ -12,7 +12,7 @@ def replace(values)

private
def find_target
(@_values || []).map do |attrs|
(@_values ||= []).map do |attrs|
klass.load(attrs).tap do |child|
assign_references(child)
end
Expand Down
1 change: 0 additions & 1 deletion lib/mongo_mapper/plugins/clone.rb
Expand Up @@ -8,7 +8,6 @@ def initialize_copy(other)
@_new = true
@_destroyed = false
remove_instance_variable :@_id
remove_instance_variable :@_id_before_type_cast

associations.each do |name, association|
instance_variable_set(association.ivar, nil)
Expand Down
12 changes: 2 additions & 10 deletions lib/mongo_mapper/plugins/dirty.rb
Expand Up @@ -11,14 +11,6 @@ def initialize(*)
super.tap { changed_attributes.delete('_id') }
end

def initialize_from_database(*)
@initializing_from_database = true
super.tap {
changed_attributes.clear
@initializing_from_database = false
}
end

def save(*)
clear_changes { super }
end
Expand Down Expand Up @@ -47,10 +39,10 @@ def attribute_method?(attr_name)
private

def write_key(key, value)
if @initializing_from_database
key = key.to_s
if !keys.key?(key)
super
else
key = key.to_s
attribute_will_change!(key) unless attribute_changed?(key)
super.tap do
changed_attributes.delete(key) unless attribute_value_changed?(key)
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo_mapper/plugins/document.rb
Expand Up @@ -11,11 +11,11 @@ def embeddable?
end

def new?
@_new
!!(@_new ||= false)
end

def destroyed?
@_destroyed == true
!!(@_destroyed ||= false)
end

def reload
Expand Down
71 changes: 42 additions & 29 deletions lib/mongo_mapper/plugins/identity_map.rb
Expand Up @@ -57,34 +57,10 @@ def get_from_identity_map(id)
IdentityMap.repository[IdentityMap.key(self, id)]
end

module IdentityMapQueryMethods
def find_one(opts={})
query = clone.amend(opts)

if IdentityMap.enabled? && query.simple? && (document = model.get_from_identity_map(query[:_id]))
document
else
super.tap do |doc|
doc.remove_from_identity_map if doc && query.fields?
end
end
end

def find_each(opts={})
query = clone.amend(opts)
super(opts) do |doc|
doc.remove_from_identity_map if doc && query.fields?
yield doc if block_given?
end
end

# Ensure that these aliased methods in plucky also get overridden.
alias_method :first, :find_one
alias_method :each, :find_each
end

def query(opts={})
super.extend(IdentityMapQueryMethods)
super.tap do |query|
query.identity_map = self if Thread.current[:mongo_mapper_identity_map_enabled]
end
end

def remove_documents_from_map(*documents)
Expand All @@ -94,10 +70,11 @@ def remove_documents_from_map(*documents)
end

def load(attrs)
return nil if attrs.nil?
return super unless Thread.current[:mongo_mapper_identity_map_enabled]
return nil unless attrs
document = get_from_identity_map(attrs['_id'])

if document.nil?
if !document
document = super
document.add_to_identity_map
end
Expand Down Expand Up @@ -130,3 +107,39 @@ def remove_from_identity_map
end
end
end

module PluckyMethods
module ClassMethods
extend ActiveSupport::Concern

included do
attr_accessor :identity_map

# Ensure that these aliased methods in plucky also get overridden.
alias_method :first, :find_one
alias_method :each, :find_each
end

def find_one(opts={})
query = clone.amend(opts)

if identity_map && query.simple? && (document = identity_map.get_from_identity_map(query[:_id]))
document
else
super.tap do |doc|
doc.remove_from_identity_map if doc && query.fields?
end
end
end

def find_each(opts={})
query = clone.amend(opts)
super(opts) do |doc|
doc.remove_from_identity_map if doc && query.fields?
yield doc if block_given?
end
end
end
end

::MongoMapper::Plugins::Querying::DecoratedPluckyQuery.send :include, ::PluckyMethods::ClassMethods

0 comments on commit 61aa557

Please sign in to comment.