Class: InventoryRefresh::InventoryCollection::Index::Type::LocalDb

Inherits:
Base
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_collection/index/type/local_db.rb

Instance Method Summary collapse

Methods inherited from Base

#index_data, #reindex!, #store_index_for

Constructor Details

#initialize(inventory_collection, index_name, attribute_names, data_index) ⇒ LocalDb

Returns a new instance of LocalDb.

Parameters:



14
15
16
17
18
19
20
21
# File 'lib/inventory_refresh/inventory_collection/index/type/local_db.rb', line 14

def initialize(inventory_collection, index_name, attribute_names, data_index)
  super

  @index                 = nil
  @loaded_references     = Set.new
  @data_index            = data_index
  @all_references_loaded = false
end

Instance Method Details

#find(reference) ⇒ Object

Finds reference in the DB. Using a configured strategy we cache obtained data in the index, so the same find will not hit database twice. Also if we use lazy_links and this is called when data_collection_finalized?, we load all data from the DB, referenced by lazy_links, in one query.

Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/inventory_refresh/inventory_collection/index/type/local_db.rb', line 28

def find(reference)
  # Use the cached index only data_collection_finalized?, meaning no new reference can occur
  if data_collection_finalized? && all_references_loaded? && index
    return index[reference.stringified_reference]
  else
    return index[reference.stringified_reference] if index && index[reference.stringified_reference]

    # We haven't found the reference, lets add it to the list of references and load it
    add_reference(reference)
  end

  # Put our existing data_index keys into loaded references
  loaded_references.merge(data_index.keys)
  # Load the rest of the references from the DB
  populate_index!

  self.all_references_loaded = true if data_collection_finalized?

  index[reference.stringified_reference]
end