Class: ExcADG::DataStore
- Inherits:
-
Object
- Object
- ExcADG::DataStore
- Defined in:
- lib/excadg/data_store.rb
Overview
collection of VStateData for Broker
Defined Under Namespace
Classes: DataSkew
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#<<(new) ⇒ Object
add new element to the store adds it to the two hashes to access lated by either attribute.
-
#[](key) ⇒ Object
retrieves VStateData by key.
-
#initialize ⇒ DataStore
constructor
A new instance of DataStore.
-
#key?(key) ⇒ Boolean
checks if there is data for a given key.
-
#to_a ⇒ Object
retrieve all vertices state data, could contain huge amount of data and be slow to work with; prefer to access vertices data by name using [] instead.
Constructor Details
#initialize ⇒ DataStore
Returns a new instance of DataStore.
11 12 13 14 15 16 |
# File 'lib/excadg/data_store.rb', line 11 def initialize # two hashes to store VStateData and access them fast by either key @by_name = {} @by_vertex = {} @size = 0 end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
9 10 11 |
# File 'lib/excadg/data_store.rb', line 9 def size @size end |
Instance Method Details
#<<(new) ⇒ Object
add new element to the store adds it to the two hashes to access lated by either attribute
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/excadg/data_store.rb', line 21 def << new Assertions.is_a? new, VStateData::Full if (by_name = @by_name[new.name]) && !(by_name.vertex.eql? new.vertex) raise DataSkew, "Vertex named #{new.name} - #{new.vertex} is recorded as #{by_name.vertex} in state" end if (by_vertex = @by_vertex[new.vertex]) && !(by_vertex.name.eql? new.name) raise DataSkew, "Vertex #{new.vertex} named #{new.name} is already named #{by_vertex.name}" end @size += 1 unless key? new @by_name[new.name] = new if new.name @by_vertex[new.vertex] = new if new.vertex end |
#[](key) ⇒ Object
retrieves VStateData by key
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/excadg/data_store.rb', line 42 def [] key Assertions.is_a? key, [Vertex, VStateData::Key, Symbol, String] case key when Vertex then @by_vertex[key] when Symbol, String then @by_name[key] when VStateData::Key if key.name && @by_name.key?(key.name) @by_name[key.name] elsif key.vertex && @by_vertex.key?(key.vertex) @by_vertex[key.vertex] else nil end end end |
#key?(key) ⇒ Boolean
checks if there is data for a given key
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/excadg/data_store.rb', line 67 def key? key case key when Vertex then @by_vertex.key? key when Symbol, String then @by_name.key? key when VStateData::Key @by_name.key?(key.name) || @by_vertex.key?(key.vertex) else false end end |
#to_a ⇒ Object
retrieve all vertices state data, could contain huge amount of data and be slow to work with; prefer to access vertices data by name using [] instead
62 63 64 |
# File 'lib/excadg/data_store.rb', line 62 def to_a (@by_name.values + @by_vertex.values).uniq end |