Class: ExcADG::DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/excadg/data_store.rb

Overview

collection of VStateData for Broker

Defined Under Namespace

Classes: DataSkew

Instance Method Summary collapse

Constructor Details

#initializeDataStore

Returns a new instance of DataStore.



9
10
11
12
13
14
# File 'lib/excadg/data_store.rb', line 9

def initialize
  # two hashes to store VStateData and access them fast by either key
  @by_name = {}
  @by_vertex = {}
  @size = 0
end

Instance Method Details

#<<(new) ⇒ Object

add new element to the store adds it to the two hashes to access lated by either attribute

Returns:

  • current number of elements



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/excadg/data_store.rb', line 19

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

Parameters:

Returns:

  • VStateData::Full for the respective key

Raises:

  • StandardError if key is not of a supported type



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/excadg/data_store.rb', line 40

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

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/excadg/data_store.rb', line 76

def empty?
  @size.zero?
end

#key?(key) ⇒ Boolean

checks if there is data for a given key

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/excadg/data_store.rb', line 65

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_aObject

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



60
61
62
# File 'lib/excadg/data_store.rb', line 60

def to_a
  (@by_name.values + @by_vertex.values).uniq
end