An in-memory datastore for testing and tryout purposes.
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
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
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
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
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
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
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
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