Module: MassiveRecord::ORM::IdentityMap

Extended by:
ActiveSupport::Concern
Defined in:
lib/massive_record/orm/identity_map.rb

Overview

The goal of the IdentiyMap is to make sure that the same object is not loaded twice from the database, but uses the same object if you do 2.times { AClass.find(1) }.

To get a quick introduction on IdentityMap see: www.martinfowler.com/eaaCatalog/identityMap.html

You can enable / disable Identity map by doing: MassiveRecord::ORM::IdentityMap.enabled = flag

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: Middleware, RecordIsSuperClassOfQueriedClass

Class Method Summary collapse

Class Method Details

.add(record) ⇒ Object



95
96
97
98
99
# File 'lib/massive_record/orm/identity_map.rb', line 95

def add(record)
  return if record.nil?

  repository[record_class_to_repository_key(record)][record.id] = record
end

.enabledObject Also known as: enabled?



38
39
40
# File 'lib/massive_record/orm/identity_map.rb', line 38

def enabled
  !!Thread.current[:identity_map_enabled]
end

.enabled=(boolean) ⇒ Object

Switch to either turn on or off the identity map



34
35
36
# File 'lib/massive_record/orm/identity_map.rb', line 34

def enabled=(boolean)
  Thread.current[:identity_map_enabled] = boolean
end

.get(klass, *ids) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/massive_record/orm/identity_map.rb', line 68

def get(klass, *ids)
  get_many = ids.first.is_a?(Array)

  ids.flatten!

  result =  case ids.length
            when 0
              raise ArgumentError.new("Must have at least one ID!")
            when 1
              result = get_one(klass, ids.first)
              get_many ? [result].compact : result
            else
              get_some(klass, ids)
            end

  if records = Array(result).compact and records.any?
    ActiveSupport::Notifications.instrument("identity_map.massive_record", {
      :name => [klass, 'loaded from identity map'].join(' '),
      :records => records
    }) do
      result
    end
  end

  result
end

.remove(record) ⇒ Object



101
102
103
# File 'lib/massive_record/orm/identity_map.rb', line 101

def remove(record)
  remove_by_id record.class, record.id
end

.remove_by_id(klass, id) ⇒ Object



105
106
107
# File 'lib/massive_record/orm/identity_map.rb', line 105

def remove_by_id(klass, id)
  repository[class_to_repository_key(klass)].delete id
end

.useObject

Call this with a block to ensure that IdentityMap is enabled for that block and reset to it’s origianl setting thereafter



48
49
50
51
52
53
# File 'lib/massive_record/orm/identity_map.rb', line 48

def use
  original_value, self.enabled = enabled, true
  yield
ensure
  self.enabled = original_value
end

.withoutObject

Call this with a block to ensure that IdentityMap is disabled for that block and reset to it’s origianl setting thereafter



59
60
61
62
63
64
# File 'lib/massive_record/orm/identity_map.rb', line 59

def without
  original_value, self.enabled = enabled, false
  yield
ensure
  self.enabled = original_value
end