Sha256: 72a7d0ca36240e7160f783eed1fdd1c09c0f68451d3bb859aff45bc5c932af81

Contents?: true

Size: 1.17 KB

Versions: 16

Compression:

Stored size: 1.17 KB

Contents

module Swift
  # Weak hash set.
  #--
  # TODO: Is 'hash set' the real name for a hash where both the keys and values must be unique?
  class IdentityMap
    def initialize
      @cache, @reverse_cache, @finalize = {}, {}, method(:finalize)
    end

    def get key
      value_id = @cache[key]
      return ObjectSpace._id2ref(value_id) unless value_id.nil?
      nil
    end

    #--
    # TODO: Barf if the value.object_id already exists in the cache.
    def set key, value
      @reverse_cache[value.object_id] = key
      @cache[key]                     = value.object_id
      ObjectSpace.define_finalizer(value, @finalize)
    end

    private
      def finalize value_id
        @cache.delete @reverse_cache.delete value_id
      end
  end # IdentityMap

  class Adapter
    def identity_map
      @identity_map ||= IdentityMap.new
    end
  end

  class Scheme
    #--
    # TODO: Redefined method :(
    def self.load tuple
      im = [self, *tuple.values_at(*header.keys)]
      unless scheme = Swift.db.identity_map.get(im)
        scheme       = allocate
        scheme.tuple = tuple
        Swift.db.identity_map.set(im, scheme)
      end
      scheme
    end
  end # Scheme
end # Swift

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
swift-0.14.0 lib/swift/identity_map.rb
swift-0.13.0 lib/swift/identity_map.rb
swift-0.12.0 lib/swift/identity_map.rb
swift-0.11.0 lib/swift/identity_map.rb
swift-0.10.0 lib/swift/identity_map.rb
swift-0.9.1 lib/swift/identity_map.rb
swift-0.9.0 lib/swift/identity_map.rb
swift-0.8.1 lib/swift/identity_map.rb
swift-0.8.0 lib/swift/identity_map.rb
swift-0.7.2 lib/swift/identity_map.rb
swift-0.7.1 lib/swift/identity_map.rb
swift-0.7.0 lib/swift/identity_map.rb
swift-0.6.1 lib/swift/identity_map.rb
swift-0.6.0 lib/swift/identity_map.rb
swift-0.5.1 lib/swift/identity_map.rb
swift-0.5.0 lib/swift/identity_map.rb