class InventoryRefresh::InventoryCollection::Index::Proxy

Attributes

all_refs[R]
data_indexes[R]
inventory_collection[R]
local_db_indexes[R]
primary_ref[R]
secondary_refs[R]
skeletal_primary_index[R]

Public Class Methods

new(inventory_collection, secondary_refs = {}) click to toggle source

@param inventory_collection [InventoryRefresh::InventoryCollection] InventoryCollection object owning the proxy @param secondary_refs [Hash] Secondary_refs in format {:name_of_the_ref => [:attribute1, :attribute2]}

# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 17
def initialize(inventory_collection, secondary_refs = {})
  @inventory_collection = inventory_collection

  @primary_ref    = {primary_index_ref => @inventory_collection.manager_ref}
  @secondary_refs = secondary_refs
  @all_refs       = @primary_ref.merge(@secondary_refs)

  @data_indexes     = {}
  @local_db_indexes = {}

  @all_refs.each do |index_name, attribute_names|
    @data_indexes[index_name] = InventoryRefresh::InventoryCollection::Index::Type::Data.new(
      inventory_collection,
      index_name,
      attribute_names
    )

    @local_db_indexes[index_name] = InventoryRefresh::InventoryCollection::Index::Type::LocalDb.new(
      inventory_collection,
      index_name,
      attribute_names,
      @data_indexes[index_name]
    )
  end

  @skeletal_primary_index = InventoryRefresh::InventoryCollection::Index::Type::Skeletal.new(
    inventory_collection,
    :skeletal_primary_index_ref,
    named_ref,
    primary_index
  )
end

Public Instance Methods

build_primary_index_for(inventory_object) click to toggle source

Builds primary index for passed InventoryObject

@param inventory_object [InventoryRefresh::InventoryObject] InventoryObject we want to index @return [InventoryRefresh::InventoryObject] Passed InventoryObject

# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 54
def build_primary_index_for(inventory_object)
  # Building the object, we need to provide all keys of a primary index

  assert_index(inventory_object.data, primary_index_ref)
  primary_index.store_index_for(inventory_object)
end
build_secondary_indexes_for(inventory_object) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 61
def build_secondary_indexes_for(inventory_object)
  secondary_refs.keys.each do |ref|
    data_index(ref).store_index_for(inventory_object)
  end
end
find(reference, ref: primary_index_ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 71
def find(reference, ref: primary_index_ref)
  # TODO(lsmola) lazy_find will support only hash, then we can remove the _by variant
  # TODO(lsmola) this method should return lazy too, the rest of the finders should be deprecated
  return if reference.nil?
  assert_index(reference, ref)

  reference = inventory_collection.build_reference(reference, ref)

  case strategy
  when :local_db_find_references, :local_db_cache_all
    local_db_index_find(reference)
  when :local_db_find_missing_references
    find_in_data_or_skeletal_index(reference) || local_db_index_find(reference)
  else
    find_in_data_or_skeletal_index(reference)
  end
end
lazy_find(manager_uuid, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 89
def lazy_find(manager_uuid, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false)
  return if manager_uuid.nil?
  assert_index(manager_uuid, ref)

  ::InventoryRefresh::InventoryObjectLazy.new(inventory_collection,
                                              manager_uuid,
                                              :ref                         => ref,
                                              :key                         => key,
                                              :default                     => default,
                                              :transform_nested_lazy_finds => transform_nested_lazy_finds)
end
named_ref(ref = primary_index_ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 101
def named_ref(ref = primary_index_ref)
  all_refs[ref]
end
primary_index() click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 67
def primary_index
  data_index(primary_index_ref)
end
primary_index_ref() click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 105
def primary_index_ref
  :manager_ref
end

Private Instance Methods

assert_index(manager_uuid, ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 166
def assert_index(manager_uuid, ref)
  # TODO(lsmola) do we need some production logging too? Maybe the refresh log level could drive this
  # Let' do this really slick development and test env, but disable for production, since the checks are pretty
  # slow.
  return unless inventory_collection.assert_graph_integrity

  if manager_uuid.kind_of?(InventoryRefresh::InventoryCollection::Reference)
    # InventoryRefresh::InventoryCollection::Reference has been already asserted, skip
  elsif manager_uuid.kind_of?(Hash)
    # Test te index exists
    assert_index_exists(ref)

    # Test we are sending all keys required for the index
    unless required_index_keys_present?(manager_uuid.keys, ref)
      raise "Finder has missing keys for index :#{ref}, missing indexes are: #{missing_keys(manager_uuid.keys, ref)}"
    end
    # Test that keys, that are relations, are nil or InventoryObject or InventoryObjectlazy class
    assert_relation_keys(manager_uuid, ref)
  else
    # Test te index exists
    assert_index_exists(ref)

    # Check that other value (possibly String or Integer)) has no composite index
    if named_ref(ref).size > 1
      right_format = "collection.find(#{named_ref(ref).map { |x| ":#{x} => 'X'" }.join(", ")}"

      raise "The index :#{ref} has composite index, finder has to be called as: #{right_format})"
    end

    # Assert the that possible relation is nil or InventoryObject or InventoryObjectlazy class
    assert_relation_keys({named_ref(ref).first => manager_uuid}, ref)
  end
rescue => e
  #_log.error("Error when asserting index: #{manager_uuid}, with ref: #{ref} of: #{inventory_collection}")
  raise e
end
assert_index_exists(ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 162
def assert_index_exists(ref)
  raise "Index :#{ref} doesn't exist on #{inventory_collection}" if named_ref(ref).nil?
end
assert_relation_keys(data, ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 151
def assert_relation_keys(data, ref)
  named_ref(ref).each do |key|
    # Skip if the key is not a foreign key
    next unless association_to_foreign_key_mapping[key]
    # Skip if data on key are nil or InventoryObject or InventoryObjectLazy
    next if data[key].nil? || data[key].kind_of?(InventoryRefresh::InventoryObject) || data[key].kind_of?(InventoryRefresh::InventoryObjectLazy)
    # Raise error since relation must be nil or InventoryObject or InventoryObjectLazy
    raise "Wrong index for key :#{key}, the value must be of type Nil or InventoryObject or InventoryObjectLazy, got: #{data[key]}"
  end
end
data_index(name) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 135
def data_index(name)
  data_indexes[name] || raise("Index :#{name} not defined for #{inventory_collection}")
end
data_index_find(reference) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 127
def data_index_find(reference)
  data_index(reference.ref).find(reference.stringified_reference)
end
find_in_data_or_skeletal_index(reference) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 118
def find_in_data_or_skeletal_index(reference)
  data_index_find(reference) || skeletal_index_find(reference)
end
local_db_index(name) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 139
def local_db_index(name)
  local_db_indexes[name] || raise("Index :#{name} not defined for #{inventory_collection}")
end
local_db_index_find(reference) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 131
def local_db_index_find(reference)
  local_db_index(reference.ref).find(reference)
end
missing_keys(data_keys, ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 143
def missing_keys(data_keys, ref)
  named_ref(ref) - data_keys
end
required_index_keys_present?(data_keys, ref) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 147
def required_index_keys_present?(data_keys, ref)
  missing_keys(data_keys, ref).empty?
end
skeletal_index_find(reference) click to toggle source
# File lib/inventory_refresh/inventory_collection/index/proxy.rb, line 122
def skeletal_index_find(reference)
  # Find in skeletal index, but we are able to create skeletal index only for primary indexes
  skeletal_primary_index.find(reference.stringified_reference) if reference.primary?
end