class Thingfish::Metastore::Memory

An in-memory metastore for testing and tryout purposes.

Attributes

storage[R]

The raw Hash of metadata

Public Class Methods

new( storage={} ) click to toggle source

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

apply_search_criteria( ds, options ) click to toggle source

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
apply_search_direction( ds, options ) click to toggle source

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
apply_search_limit( ds, options ) click to toggle source

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
apply_search_order( ds, options ) click to toggle source

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
each_oid( &block ) click to toggle source

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
fetch( oid, *keys ) click to toggle source

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
fetch_value( oid, key ) click to toggle source

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
include?( oid ) click to toggle source

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
merge( oid, values ) click to toggle source

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
oids() click to toggle source

Return an Array of all stored OIDs.

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

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
remove_except( oid, *keys ) click to toggle source

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
save( oid, metadata ) click to toggle source

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
size() click to toggle source

Returns the number of objects the store contains.

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