Class: Thingfish::Metastore::Memory
- Inherits:
-
Thingfish::Metastore
- Object
- Thingfish::Metastore
- Thingfish::Metastore::Memory
- Extended by:
- Loggability
- Includes:
- Normalization
- Defined in:
- lib/thingfish/metastore/memory.rb
Overview
An in-memory metastore for testing and tryout purposes.
Instance Attribute Summary collapse
-
#storage ⇒ Object
readonly
The raw Hash of metadata.
Instance Method Summary collapse
-
#apply_search_criteria(ds, options) ⇒ Object
Apply the search :criteria from the specified
optionsto the collection indsand return the modified dataset. -
#apply_search_direction(ds, options) ⇒ Object
Apply the search :direction from the specified
optionsto the collection indsand return the modified dataset. -
#apply_search_limit(ds, options) ⇒ Object
Apply the search :limit from the specified
optionsto the collection indsand return the modified dataset. -
#apply_search_order(ds, options) ⇒ Object
Apply the search :order from the specified
optionsto the collection indsand return the modified dataset. -
#each_oid(&block) ⇒ Object
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.
-
#fetch(oid, *keys) ⇒ Object
Fetch the data corresponding to the given
oidas a Hash-ish object. -
#fetch_related_oids(oid) ⇒ Object
Fetch OIDs related to the given
oid. -
#fetch_value(oid, key) ⇒ Object
Fetch the value of the metadata associated with the given
keyfor the specifiedoid. -
#include?(oid) ⇒ Boolean
Returns
trueif the metastore has metadata associated with the specifiedoid. -
#initialize(storage = {}) ⇒ Memory
constructor
Create a new MemoryMetastore, using the given
storageobject to store data in. -
#merge(oid, values) ⇒ Object
Update the metadata for the given
oidwith the specifiedvalueshash. -
#oids ⇒ Object
Return an Array of all stored OIDs.
-
#omit_related_resources(ds, options) ⇒ Object
Omit related resources from the search dataset
dsunless the givenoptionsspecify otherwise. -
#remove(oid, *keys) ⇒ Object
Remove all metadata associated with
oidfrom the Metastore. -
#remove_except(oid, *keys) ⇒ Object
Remove all metadata associated with
oidexcept for the specifiedkeys. -
#save(oid, metadata) ⇒ Object
Save the
metadataHash for the specifiedoid. -
#search(options = {}) ⇒ Object
Search the metastore for UUIDs which match the specified
criteriaand return them as an iterator. -
#size ⇒ Object
Returns the number of objects the store contains.
Methods included from Normalization
make_object_id, normalize_key, normalize_keys, normalize_oid
Methods inherited from Thingfish::Metastore
Constructor Details
#initialize(storage = {}) ⇒ Memory
Create a new MemoryMetastore, using the given storage object to store data in. The storage should quack like a Hash.
20 21 22 |
# File 'lib/thingfish/metastore/memory.rb', line 20 def initialize( storage={} ) @storage = storage end |
Instance Attribute Details
#storage ⇒ Object (readonly)
The raw Hash of metadata
27 28 29 |
# File 'lib/thingfish/metastore/memory.rb', line 27 def storage @storage end |
Instance Method Details
#apply_search_criteria(ds, options) ⇒ Object
Apply the search :criteria from the specified options to the collection in ds and return the modified dataset.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/thingfish/metastore/memory.rb', line 114 def apply_search_criteria( ds, ) if (( criteria = [: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) ⇒ Object
Apply the search :direction from the specified options to the collection in ds and return the modified dataset.
141 142 143 144 |
# File 'lib/thingfish/metastore/memory.rb', line 141 def apply_search_direction( ds, ) ds.reverse! if [:direction] && [:direction] == 'desc' return ds end |
#apply_search_limit(ds, options) ⇒ Object
Apply the search :limit from the specified options to the collection in ds and return the modified dataset.
149 150 151 152 153 154 155 156 157 |
# File 'lib/thingfish/metastore/memory.rb', line 149 def apply_search_limit( ds, ) if (( limit = [:limit] )) self.log.debug " limiting to %s results" % [ limit ] offset = [:offset] || 0 ds = ds.to_a.slice( offset, limit ) end return ds end |
#apply_search_order(ds, options) ⇒ Object
Apply the search :order from the specified options to the collection in ds and return the modified dataset.
128 129 130 131 132 133 134 135 136 |
# File 'lib/thingfish/metastore/memory.rb', line 128 def apply_search_order( ds, ) if (( fields = [: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) ⇒ Object
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.
38 39 40 |
# File 'lib/thingfish/metastore/memory.rb', line 38 def each_oid( &block ) return @storage.each_key( &block ) end |
#fetch(oid, *keys) ⇒ Object
Fetch the data corresponding to the given oid as a Hash-ish object.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/thingfish/metastore/memory.rb', line 51 def fetch( oid, *keys ) oid = normalize_oid( oid ) = @storage[ oid ] or return nil if keys.empty? self.log.debug "Fetching metadata for OID %s" % [ oid ] return .dup else self.log.debug "Fetching metadata for %p for OID %s" % [ keys, oid ] keys = normalize_keys( keys ) values = .values_at( *keys ) return Hash[ [keys, values].transpose ] end end |
#fetch_related_oids(oid) ⇒ Object
Fetch OIDs related to the given oid.
79 80 81 82 83 |
# File 'lib/thingfish/metastore/memory.rb', line 79 def ( oid ) oid = normalize_oid( oid ) self.log.debug "Fetching OIDs of resources related to %s" % [ oid ] return self.search( :criteria => {:relation => oid}, :include_related => true ) end |
#fetch_value(oid, key) ⇒ Object
Fetch the value of the metadata associated with the given key for the specified oid.
69 70 71 72 73 74 75 |
# File 'lib/thingfish/metastore/memory.rb', line 69 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) ⇒ Boolean
Returns true if the metastore has metadata associated with the specified oid.
189 190 191 192 |
# File 'lib/thingfish/metastore/memory.rb', line 189 def include?( oid ) oid = normalize_oid( oid ) return @storage.include?( oid ) end |
#merge(oid, values) ⇒ Object
Update the metadata for the given oid with the specified values hash.
161 162 163 164 165 |
# File 'lib/thingfish/metastore/memory.rb', line 161 def merge( oid, values ) oid = normalize_oid( oid ) values = normalize_keys( values ) @storage[ oid ].merge!( values ) end |
#oids ⇒ Object
Return an Array of all stored OIDs.
31 32 33 |
# File 'lib/thingfish/metastore/memory.rb', line 31 def oids return @storage.keys end |
#omit_related_resources(ds, options) ⇒ Object
Omit related resources from the search dataset ds unless the given options specify otherwise.
104 105 106 107 108 109 |
# File 'lib/thingfish/metastore/memory.rb', line 104 def ( ds, ) unless [:include_related] ds = ds.reject {|uuid| @storage[uuid]['relationship'] } end return ds end |
#remove(oid, *keys) ⇒ Object
Remove all metadata associated with oid from the Metastore.
169 170 171 172 173 174 175 176 177 |
# File 'lib/thingfish/metastore/memory.rb', line 169 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) ⇒ Object
Remove all metadata associated with oid except for the specified keys.
181 182 183 184 185 |
# File 'lib/thingfish/metastore/memory.rb', line 181 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) ⇒ Object
Save the metadata Hash for the specified oid.
44 45 46 47 |
# File 'lib/thingfish/metastore/memory.rb', line 44 def save( oid, ) oid = normalize_oid( oid ) @storage[ oid ] = .dup end |
#search(options = {}) ⇒ Object
Search the metastore for UUIDs which match the specified criteria and return them as an iterator.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/thingfish/metastore/memory.rb', line 88 def search( ={} ) ds = @storage.each_key self.log.debug "Starting search with %p" % [ ds ] ds = self.( ds, ) ds = self.apply_search_criteria( ds, ) ds = self.apply_search_order( ds, ) ds = self.apply_search_direction( ds, ) ds = self.apply_search_limit( ds, ) return ds.to_a end |
#size ⇒ Object
Returns the number of objects the store contains.
196 197 198 |
# File 'lib/thingfish/metastore/memory.rb', line 196 def size return @storage.size end |