Class: Thingfish::Datastore::Memory

Inherits:
Thingfish::Datastore show all
Extended by:
Loggability
Defined in:
lib/thingfish/datastore/memory.rb

Overview

An in-memory datastore for testing and tryout purposes.

Instance Method Summary collapse

Methods inherited from Thingfish::Datastore

#inspect, #transaction

Methods included from Normalization

make_object_id, normalize_key, normalize_keys, normalize_oid

Constructor Details

#initialize(storage = {}) ⇒ Memory

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



19
20
21
# File 'lib/thingfish/datastore/memory.rb', line 19

def initialize( storage={} )
  @storage = storage
end

Instance Method Details

#each(&block) ⇒ Object

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.



88
89
90
# File 'lib/thingfish/datastore/memory.rb', line 88

def each( &block )
  return @storage.each( &block )
end

#each_oid(&block) ⇒ Object

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.



79
80
81
# File 'lib/thingfish/datastore/memory.rb', line 79

def each_oid( &block )
  return @storage.each_key( &block )
end

#fetch(oid) ⇒ Object

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



55
56
57
58
59
60
# File 'lib/thingfish/datastore/memory.rb', line 55

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

#include?(oid) ⇒ Boolean

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

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/thingfish/datastore/memory.rb', line 71

def include?( oid )
  oid = normalize_oid( oid )
  return @storage.include?( oid )
end

#remove(oid) ⇒ Object

Remove the data associated with oid from the Datastore.



64
65
66
67
# File 'lib/thingfish/datastore/memory.rb', line 64

def remove( oid )
  oid = normalize_oid( oid )
  @storage.delete( oid )
end

#replace(oid, io) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/thingfish/datastore/memory.rb', line 41

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(io) ⇒ Object

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



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/thingfish/datastore/memory.rb', line 26

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