Sha256: 3e9bd568b9f889c656d57985fdcb0be0250aa4dc2fa5064f20e72813b0f3dba7

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module DataMapper
  
  # Tracks objects to help ensure that each object gets loaded only once.
  # See: http://www.martinfowler.com/eaaCatalog/identityMap.html
  class IdentityMap
    
    def initialize
      # WeakHash is much more expensive, and not necessary if the IdentityMap is tied to Session instead of Database.
      # @cache = Hash.new { |h,k| h[k] = Support::WeakHash.new }
      @cache = Hash.new { |h,k| h[k] = Hash.new }
    end

    # Pass a Class and a key, and to retrieve an instance.
    # If the instance isn't found, nil is returned.
    def get(klass, key)
      @cache[mapped_class(klass)][key]
    end

    # Pass an instance to add it to the IdentityMap.
    # The instance must have an assigned key.
    def set(instance)
      instance_key = instance.key
      raise "Can't store an instance with a nil key in the IdentityMap" if instance_key.nil?
      
      @cache[mapped_class(instance.class)][instance_key] = instance
    end
    
    # Remove an instance from the IdentityMap.
    def delete(instance)
      @cache[mapped_class(instance.class)].delete(instance.key)
    end
    
    # Clears a particular set of classes from the IdentityMap.
    def clear!(klass)
      @cache.delete(klass)
    end
    
    private
    def mapped_class(klass)
      unless klass.superclass.respond_to?(:persistent?)
        klass
      else
        mapped_class(klass.superclass)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datamapper-0.3.1 lib/data_mapper/identity_map.rb
datamapper-0.3.0 lib/data_mapper/identity_map.rb
datamapper-0.3.2 lib/data_mapper/identity_map.rb