Class: InventoryRefresh::InventoryCollection::DataStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_collection/data_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_collection, secondary_refs) ⇒ DataStorage



31
32
33
34
35
36
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 31

def initialize(inventory_collection, secondary_refs)
  @inventory_collection = inventory_collection
  @data                 = []

  @index_proxy = InventoryRefresh::InventoryCollection::Index::Proxy.new(inventory_collection, secondary_refs)
end

Instance Attribute Details

#dataArray<InventoryObject>



7
8
9
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 7

def data
  @data
end

#index_proxyObject (readonly)

Returns the value of attribute index_proxy.



9
10
11
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 9

def index_proxy
  @index_proxy
end

#inventory_collectionObject (readonly)

Returns the value of attribute inventory_collection.



9
10
11
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 9

def inventory_collection
  @inventory_collection
end

Instance Method Details

#<<(inventory_object) ⇒ InventoryRefresh::InventoryCollection Also known as: push

Adds passed InventoryObject into the InventoryCollection’s storage



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 42

def <<(inventory_object)
  if inventory_object.manager_uuid.present? && !primary_index.find(inventory_object.manager_uuid)
    data << inventory_object

    # TODO(lsmola) Maybe we do not need the secondary indexes here?
    # Maybe we should index it like LocalDb indexes, on demand, and storing what was
    # indexed? Maybe we should allow only lazy access and no direct find from a parser. Since for streaming
    # refresh, things won't be parsed together and no full state will be taken.
    build_primary_index_for(inventory_object)
    build_secondary_indexes_for(inventory_object)
  end
  inventory_collection
end

#build(hash) ⇒ InventoryRefresh::InventoryObject

Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 87

def build(hash)
  hash, uuid, inventory_object = primary_index_scan(hash)

  # Return InventoryObject if found in primary index
  return inventory_object unless inventory_object.nil?

  # We will take existing skeletal record, so we don't duplicate references for saving. We can have duplicated
  # reference from local_db index, (if we are using .find in parser, that causes N+1 db queries), but that is ok,
  # since that one is not being saved.
  inventory_object = skeletal_primary_index.delete(uuid)

  # We want to update the skeletal record with actual data
  inventory_object&.assign_attributes(hash)

  # Build the InventoryObject
  inventory_object ||= new_inventory_object(enrich_data(hash))

  # Store new InventoryObject and return it
  push(inventory_object)
  inventory_object
end

#build_partial(hash) ⇒ InventoryRefresh::InventoryObject

Finds of builds a new InventoryObject with incomplete data.



114
115
116
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 114

def build_partial(hash)
  skeletal_primary_index.build(hash)
end

#find_in_data(hash) ⇒ InventoryRefresh::InventoryObject

Finds InventoryObject.



77
78
79
80
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 77

def find_in_data(hash)
  _hash, _uuid, inventory_object = primary_index_scan(hash)
  inventory_object
end

#find_or_build(manager_uuid) ⇒ InventoryRefresh::InventoryObject

Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.



62
63
64
65
66
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 62

def find_or_build(manager_uuid)
  raise "The uuid consists of #{manager_ref.size} attributes, please find_or_build_by method" if manager_ref.size > 1

  find_or_build_by(manager_ref.first => manager_uuid)
end

#find_or_build_by(hash) ⇒ InventoryRefresh::InventoryObject

Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection’s storage.



69
70
71
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 69

def find_or_build_by(hash)
  build(hash)
end

#from_hash(inventory_objects_data, available_inventory_collections) ⇒ Object



129
130
131
132
133
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 129

def from_hash(inventory_objects_data, available_inventory_collections)
  InventoryRefresh::InventoryCollection::Serialization
    .new(inventory_collection)
    .from_hash(inventory_objects_data, available_inventory_collections)
end

#to_aArray<InventoryRefresh::InventoryObject>

Returns array of built InventoryObject objects



121
122
123
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 121

def to_a
  data
end

#to_hashObject



125
126
127
# File 'lib/inventory_refresh/inventory_collection/data_storage.rb', line 125

def to_hash
  InventoryRefresh::InventoryCollection::Serialization.new(inventory_collection).to_hash
end