Class: InventoryRefresh::InventoryCollection::Index::Proxy

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/inventory_refresh/inventory_collection/index/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(inventory_collection, secondary_refs = {}) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • inventory_collection (InventoryRefresh::InventoryCollection)

    InventoryCollection object owning the proxy

  • secondary_refs (Hash) (defaults to: {})

    Secondary_refs in format => [:attribute1, :attribute2]



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#skeletal_primary_indexObject (readonly)

Returns the value of attribute skeletal_primary_index.



13
14
15
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 13

def skeletal_primary_index
  @skeletal_primary_index
end

Instance Method Details

#build_primary_index_for(inventory_object) ⇒ InventoryRefresh::InventoryObject

Builds primary index for passed InventoryObject

Parameters:

Returns:



54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 61

def build_secondary_indexes_for(inventory_object)
  secondary_refs.each_key do |ref|
    data_index(ref).store_index_for(inventory_object)
  end
end

#find(reference, ref: primary_index_ref) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 79

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

#find_by(manager_uuid_hash, ref: primary_index_ref) ⇒ Object



98
99
100
101
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 98

def find_by(manager_uuid_hash, ref: primary_index_ref)
  # TODO(lsmola) deprecate this, it's enough to have find method
  find(manager_uuid_hash, :ref => ref)
end

#lazy_find(manager_uuid, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 109

def lazy_find(manager_uuid, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false)
  # TODO(lsmola) also, it should be enough to have only 1 find method, everything can be lazy, until we try to
  # access the data
  # TODO(lsmola) lazy_find will support only hash, then we can remove the _by variant
  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

#lazy_find_by(manager_uuid_hash, ref: primary_index_ref, key: nil, default: nil) ⇒ Object



103
104
105
106
107
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 103

def lazy_find_by(manager_uuid_hash, ref: primary_index_ref, key: nil, default: nil)
  # TODO(lsmola) deprecate this, it's enough to have lazy_find method

  lazy_find(manager_uuid_hash, :ref => ref, :key => key, :default => default)
end

#named_ref(ref = primary_index_ref) ⇒ Object



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

def named_ref(ref = primary_index_ref)
  all_refs[ref]
end

#primary_indexObject



75
76
77
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 75

def primary_index
  data_index(primary_index_ref)
end

#primary_index_refObject



129
130
131
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 129

def primary_index_ref
  :manager_ref
end

#reindex_secondary_indexes!Object



67
68
69
70
71
72
73
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 67

def reindex_secondary_indexes!
  data_indexes.each do |ref, index|
    next if ref == primary_index_ref

    index.reindex!
  end
end