Memory

class
Superclass
Thingfish::Metastore
Included Modules
Thingfish::Normalization
Extended With
Loggability

An in-memory metastore for testing and tryout purposes.

Attributes

storage[R]

The raw Hash of metadata

Public Class Methods

anchor
new( storage={} )

Create a new MemoryMetastore, using the given storage object to store data in. The storage should quack like a Hash.

# File lib/thingfish/metastore/memory.rb, line 19
def initialize( storage={} )
        @storage = storage
end

Public Instance Methods

anchor
apply_search_criteria( ds, options )

Apply the search :criteria from the specified options to the collection in ds and return the modified dataset.

# File lib/thingfish/metastore/memory.rb, line 113
def apply_search_criteria( ds, options )
        if (( criteria = options[:criteria] ))
                criteria.each do |field, value|
                        self.log.debug "  applying criteria: %p => %p" % [ field.to_s, value ]
                        ds = ds.select {|uuid| @storage[uuid][field.to_s] == value }
                end
        end

        return ds
end
anchor
apply_search_direction( ds, options )

Apply the search :direction from the specified options to the collection in ds and return the modified dataset.

# File lib/thingfish/metastore/memory.rb, line 140
def apply_search_direction( ds, options )
        ds.reverse! if options[:direction] && options[:direction] == 'desc'
        return ds
end
anchor
apply_search_limit( ds, options )

Apply the search :limit from the specified options to the collection in ds and return the modified dataset.

# File lib/thingfish/metastore/memory.rb, line 148
def apply_search_limit( ds, options )
        if (( limit = options[:limit] ))
                self.log.debug "  limiting to %s results" % [ limit ]
                offset = options[:offset] || 0
                ds = ds.to_a.slice( offset, limit )
        end

        return ds
end
anchor
apply_search_order( ds, options )

Apply the search :order from the specified options to the collection in ds and return the modified dataset.

# File lib/thingfish/metastore/memory.rb, line 127
def apply_search_order( ds, options )
        if (( fields = options[:order] ))
                ds = ds.to_a.sort_by do |uuid|
                        @storage[ uuid ].values_at( *fields.compact ).map {|val| val || ''}
                end
        end

        return ds
end
anchor
each_oid( &block )

Iterate over the OID of each entry in the store, yielding to the block if one is given or returning an Enumerator if one is not.

# File lib/thingfish/metastore/memory.rb, line 37
def each_oid( &block )
        return @storage.each_key( &block )
end
anchor
fetch( oid, *keys )

Fetch the data corresponding to the given oid as a Hash-ish object.

# File lib/thingfish/metastore/memory.rb, line 50
def fetch( oid, *keys )
        oid = normalize_oid( oid )
        metadata = @storage[ oid ] or return nil

        if keys.empty?
                self.log.debug "Fetching metadata for OID %s" % [ oid ]
                return metadata.dup
        else
                self.log.debug "Fetching metadata for %p for OID %s" % [ keys, oid ]
                keys = normalize_keys( keys )
                values = metadata.values_at( *keys )
                return Hash[ [keys, values].transpose ]
        end
end
anchor anchor
fetch_value( oid, key )

Fetch the value of the metadata associated with the given key for the specified oid.

# File lib/thingfish/metastore/memory.rb, line 68
def fetch_value( oid, key )
        oid = normalize_oid( oid )
        key = normalize_key( key )
        data = @storage[ oid ] or return nil

        return data[ key ]
end
anchor
include?( oid )

Returns true if the metastore has metadata associated with the specified oid.

# File lib/thingfish/metastore/memory.rb, line 188
def include?( oid )
        oid = normalize_oid( oid )
        return @storage.include?( oid )
end
anchor
merge( oid, values )

Update the metadata for the given oid with the specified values hash.

# File lib/thingfish/metastore/memory.rb, line 160
def merge( oid, values )
        oid = normalize_oid( oid )
        values = normalize_keys( values )
        @storage[ oid ].merge!( values )
end
anchor
oids()

Return an Array of all stored OIDs.

# File lib/thingfish/metastore/memory.rb, line 30
def oids
        return @storage.keys
end
anchor anchor
remove( oid, *keys )

Remove all metadata associated with oid from the Metastore.

# File lib/thingfish/metastore/memory.rb, line 168
def remove( oid, *keys )
        oid = normalize_oid( oid )
        if keys.empty?
                @storage.delete( oid )
        else
                keys = normalize_keys( keys )
                @storage[ oid ].delete_if {|key, _| keys.include?(key) }
        end
end
anchor
remove_except( oid, *keys )

Remove all metadata associated with oid except for the specified keys.

# File lib/thingfish/metastore/memory.rb, line 180
def remove_except( oid, *keys )
        oid = normalize_oid( oid )
        keys = normalize_keys( keys )
        @storage[ oid ].keep_if {|key,_| keys.include?(key) }
end
anchor
save( oid, metadata )

Save the metadata Hash for the specified oid.

# File lib/thingfish/metastore/memory.rb, line 43
def save( oid, metadata )
        oid = normalize_oid( oid )
        @storage[ oid ] = metadata.dup
end
anchor
search( options={} )

Search the metastore for UUIDs which match the specified criteria and return them as an iterator.

# File lib/thingfish/metastore/memory.rb, line 87
def search( options={} )
        ds = @storage.each_key
        self.log.debug "Starting search with %p" % [ ds ]

        ds = self.omit_related_resources( ds, options )
        ds = self.apply_search_criteria( ds, options )
        ds = self.apply_search_order( ds, options )
        ds = self.apply_search_direction( ds, options )
        ds = self.apply_search_limit( ds, options )

        return ds.to_a
end
anchor
size()

Returns the number of objects the store contains.

# File lib/thingfish/metastore/memory.rb, line 195
def size
        return @storage.size
end