Memory

class
Superclass
Thingfish::Datastore
Extended With
Loggability

An in-memory datastore for testing and tryout purposes.

Public Class Methods

anchor
new( storage={} )

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

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

Public Instance Methods

anchor
each( &block )

Iterator – yield a pair: UUID => datablob of each object in the datastore to the block, or return an Enumerator for each UUID if called without a block.

# File lib/thingfish/datastore/memory.rb, line 87
def each( &block )
        return @storage.each( &block )
end
anchor
each_oid( &block )

Iterator – yield the UUID of each object in the datastore to the block, or return an Enumerator for each UUID if called without a block.

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

Fetch the data corresponding to the given oid as an IOish object.

# File lib/thingfish/datastore/memory.rb, line 54
def fetch( oid )
        oid = normalize_oid( oid )
        self.log.debug "Fetching data for OID %s" % [ oid ]
        data = @storage[ oid ] or return nil
        return StringIO.new( data )
end
anchor
include?( oid )

Return true if the datastore has data associated with the specified oid.

# File lib/thingfish/datastore/memory.rb, line 70
def include?( oid )
        oid = normalize_oid( oid )
        return @storage.include?( oid )
end
anchor
remove( oid )

Remove the data associated with oid from the Datastore.

# File lib/thingfish/datastore/memory.rb, line 63
def remove( oid )
        oid = normalize_oid( oid )
        @storage.delete( oid )
end
anchor
replace( oid, io )

Replace the existing object associated with oid with the data read from the given io.

# File lib/thingfish/datastore/memory.rb, line 40
def replace( oid, io )
        offset = io.pos
        data = io.read.dup
        oid = normalize_oid( oid )

        self.log.debug "Replacing data under OID %s with %d bytes" % [ oid, data.bytesize ]
        @storage[ oid ] = data

        io.pos = offset
        return true
end
anchor
save( io )

Save the data read from the specified io and return an ID that can be used to fetch it later.

# File lib/thingfish/datastore/memory.rb, line 25
def save( io )
        oid = make_object_id()
        offset = io.pos
        data = io.read.dup

        self.log.debug "Saving %d bytes of data under OID %s" % [ data.bytesize, oid ]
        @storage[ oid ] = data

        io.pos = offset
        return oid
end