Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup unused code #79

Merged
merged 16 commits into from
Apr 18, 2019
Merged
2 changes: 0 additions & 2 deletions lib/inventory_refresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
require "inventory_refresh/null_logger"
require "inventory_refresh/persister"
require "inventory_refresh/save_inventory"
require "inventory_refresh/target"
require "inventory_refresh/target_collection"
require "inventory_refresh/version"
9 changes: 0 additions & 9 deletions lib/inventory_refresh/inventory_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class InventoryCollection
:lazy_find_by,
:named_ref,
:primary_index,
:reindex_secondary_indexes!,
:skeletal_primary_index,
:to => :index_proxy

Expand Down Expand Up @@ -448,13 +447,11 @@ def inspect

# @return [Integer] default batch size for talking to the DB
def batch_size
# TODO(lsmola) mode to the settings
1000
end

# @return [Integer] default batch size for talking to the DB if not using ApplicationRecord objects
def batch_size_pure_sql
# TODO(lsmola) mode to the settings
10_000
end

Expand Down Expand Up @@ -569,11 +566,5 @@ def record_identity(record)
:id => identity
}
end

# TODO: Not used!
# @return [Array<Symbol>] all association attributes and foreign keys of the model class
def association_attributes
model_class.reflect_on_all_associations.map { |x| [x.name, x.foreign_key] }.flatten.compact.map(&:to_sym)
end
end
end
9 changes: 0 additions & 9 deletions lib/inventory_refresh/inventory_collection/data_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ def find_or_build_by(hash)
build(hash)
end

# Finds InventoryObject.
#
# @param hash [Hash] Hash that needs to contain attributes defined in :manager_ref of the InventoryCollection
# @return [InventoryRefresh::InventoryObject] Found or built InventoryObject object
def find_in_data(hash)
_hash, _uuid, inventory_object = primary_index_scan(hash)
inventory_object
end

# Finds of builds a new InventoryObject. By building it, we also put in into the InventoryCollection's storage.
#
# @param hash [Hash] Hash that needs to contain attributes defined in :manager_ref of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ def saveable?
dependencies.all?(&:saved?)
end

# @return [Boolean] true if we are using a saver strategy that allows saving in parallel processes
def parallel_safe?
true
end

# @return [Boolean] true if the model_class supports STI
def supports_sti?
@supports_sti_cache = model_class.column_names.include?("type") if @supports_sti_cache.nil?
Expand Down
40 changes: 6 additions & 34 deletions lib/inventory_refresh/inventory_collection/index/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ def build_secondary_indexes_for(inventory_object)
end
end

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

index.reindex!
end
end

def primary_index
data_index(primary_index_ref)
end
Expand All @@ -94,30 +86,16 @@ def find(reference, ref: primary_index_ref)
end
end

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

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

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)
manager_uuid,
:ref => ref,
:key => key,
:default => default,
:transform_nested_lazy_finds => transform_nested_lazy_finds)
end

def named_ref(ref = primary_index_ref)
Expand All @@ -132,19 +110,13 @@ def primary_index_ref

delegate :association_to_foreign_key_mapping,
:build_stringified_reference,
:parallel_safe?,
:strategy,
:to => :inventory_collection

attr_reader :all_refs, :data_indexes, :inventory_collection, :primary_ref, :local_db_indexes, :secondary_refs

def find_in_data_or_skeletal_index(reference)
if parallel_safe?
# With parallel safe strategies, we create skeletal nodes that we can look for
data_index_find(reference) || skeletal_index_find(reference)
else
data_index_find(reference)
end
data_index_find(reference) || skeletal_index_find(reference)
end

def skeletal_index_find(reference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ def store_index_for(inventory_object)
index[build_stringified_reference(inventory_object.data, attribute_names)] = inventory_object
end

# Rebuilds the indexes for all InventoryObject objects
def reindex!
self.index = {}
data.each do |inventory_object|
store_index_for(inventory_object)
end
end

# @return [Array] Returns index data
def index_data
index.values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@ def <<(reference_data)
add_reference(reference_data)
end

# Adds array of references to the storage. The reference can be already existing, otherwise we attempt to build
# it.
#
# @param references_array [Array] Array of reference objects acceptable by add_reference method.
# @param ref [Symbol] A key to specific reference, if it's a reference pointing to something else than primary
# index.
# @return [InventoryRefresh::InventoryCollection::ReferencesStorage] Returns self
def merge!(references_array, ref: nil)
references_array.each { |reference_data| add_reference(reference_data, :ref => ref) }
self
end

# @return [Hash{String => InventoryRefresh::InventoryCollection::Reference}] Hash of indexed Reference objects
def primary_references
references[primary_index_ref]
end

# Builds a Reference object
#
# @param reference_data [InventoryRefresh::InventoryCollection::References, Hash, Object] Either existing Reference
Expand Down
6 changes: 0 additions & 6 deletions lib/inventory_refresh/inventory_collection/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ def hash_to_data(hash, available_inventory_collections, depth = 0)
hash.transform_values do |value|
if value.kind_of?(Hash) && value['inventory_collection_name']
hash_to_lazy_relation(value, available_inventory_collections, depth)
elsif value.kind_of?(Array) && value.first.kind_of?(Hash) && value.first['inventory_collection_name']
# TODO(lsmola) do we need to compact it sooner? What if first element is nil? On the other hand, we want to
# deprecate this Vm HABTM assignment because it's not effective
value.compact.map { |x| hash_to_lazy_relation(x, available_inventory_collections, depth) }
else
value
end
Expand All @@ -128,8 +124,6 @@ def data_to_hash(data, depth = 0)
data.transform_values do |value|
if inventory_object_lazy?(value) || inventory_object?(value)
lazy_relation_to_hash(value, depth)
elsif value.kind_of?(Array) && (inventory_object_lazy?(value.compact.first) || inventory_object?(value.compact.first))
value.compact.map { |x| lazy_relation_to_hash(x, depth) }
else
value
end
Expand Down
53 changes: 0 additions & 53 deletions lib/inventory_refresh/inventory_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,6 @@ def key
nil
end

# Transforms InventoryObject object data into hash format with keys that are column names and resolves correct
# values of the foreign keys (even the polymorphic ones)
#
# @param inventory_collection_scope [InventoryRefresh::InventoryCollection] parent InventoryCollection object
# @return [Hash] Data in DB format
def attributes(inventory_collection_scope = nil)
# We should explicitly pass a scope, since the inventory_object can be mapped to more InventoryCollections with
# different blacklist and whitelist. The generic code always passes a scope.
inventory_collection_scope ||= inventory_collection

attributes_for_saving = {}
# First transform the values
data.each do |key, value|
if !allowed?(inventory_collection_scope, key)
next
elsif value.kind_of?(Array) && value.any? { |x| loadable?(x) }
# Lets fill also the original data, so other InventoryObject referring to this attribute gets the right
# result
data[key] = value.compact.map(&:load).compact
# We can use built in _ids methods to assign array of ids into has_many relations. So e.g. the :key_pairs=
# relation setter will become :key_pair_ids=
attributes_for_saving[(key.to_s.singularize + "_ids").to_sym] = data[key].map(&:id).compact.uniq
elsif loadable?(value) || inventory_collection_scope.association_to_foreign_key_mapping[key]
# Lets fill also the original data, so other InventoryObject referring to this attribute gets the right
# result
data[key] = value.load if value.respond_to?(:load)
if (foreign_key = inventory_collection_scope.association_to_foreign_key_mapping[key])
# We have an association to fill, lets fill also the :key, cause some other InventoryObject can refer to it
record_id = data[key].try(:id)
attributes_for_saving[foreign_key.to_sym] = record_id

if (foreign_type = inventory_collection_scope.association_to_foreign_type_mapping[key])
# If we have a polymorphic association, we need to also fill a base class name, but we want to nullify it
# if record_id is missing
base_class = data[key].try(:base_class_name) || data[key].class.try(:base_class).try(:name)
attributes_for_saving[foreign_type.to_sym] = record_id ? base_class : nil
end
elsif data[key].kind_of?(::InventoryRefresh::InventoryObject)
# We have an association to fill but not an Activerecord association, so e.g. Ancestry, lets just load
# it here. This way of storing ancestry is ineffective in DB call count, but RAM friendly
attributes_for_saving[key.to_sym] = data[key].base_class_name.constantize.find_by(:id => data[key].id)
else
# We have a normal attribute to fill
attributes_for_saving[key.to_sym] = data[key]
end
else
attributes_for_saving[key.to_sym] = value
end
end

attributes_for_saving
end

# Transforms InventoryObject object data into hash format with keys that are column names and resolves correct
# values of the foreign keys (even the polymorphic ones)
#
Expand Down
11 changes: 1 addition & 10 deletions lib/inventory_refresh/inventory_object_lazy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def initialize(inventory_collection, index_data, ref: :manager_ref, key: nil, de

# @return [String] stringified reference
def to_s
# TODO(lsmola) do we need this method?
stringified_reference
end

Expand Down Expand Up @@ -71,10 +70,6 @@ def transitive_dependency?

# @return [Boolean] true if the key is an association on inventory_collection_scope model class
def association?(key)
# TODO(lsmola) remove this if there will be better dependency scan, probably with transitive dependencies filled
# in a second pass, then we can get rid of this hardcoded symbols. Right now we are not able to introspect these.
return true if [:parent, :genealogy_parent].include?(key)

inventory_collection.dependency_attributes.key?(key) ||
!inventory_collection.association_to_foreign_key_mapping[key].nil?
end
Expand All @@ -100,7 +95,7 @@ def transform_nested_secondary_indexes!(depth = 0)

private

delegate :parallel_safe?, :saved?, :saver_strategy, :skeletal_primary_index, :to => :inventory_collection
delegate :saved?, :saver_strategy, :skeletal_primary_index, :to => :inventory_collection
delegate :nested_secondary_index?, :primary?, :full_reference, :keys, :primary?, :to => :reference

attr_writer :reference
Expand All @@ -112,10 +107,6 @@ def transform_nested_secondary_indexes!(depth = 0)
#
# @return [InventoryRefresh::InventoryObject, NilClass] Returns pre-created InventoryObject or nil
def skeletal_precreate!
# We can do skeletal pre-create only for strategies using unique indexes. Since this can build records out of
# the given :arel scope, we will always attempt to create the recod, so we need unique index to avoid duplication
# of records.
return unless parallel_safe?
# Pre-create only for strategies that will be persisting data, i.e. are not saved already
return if saved?
# We can only do skeletal pre-create for primary index reference, since that is needed to create DB unique index
Expand Down
16 changes: 6 additions & 10 deletions lib/inventory_refresh/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ class Persister
require 'json'
require 'yaml'

attr_reader :manager, :target, :collections
attr_reader :manager, :collections

attr_accessor :refresh_state_uuid, :refresh_state_part_uuid, :total_parts, :sweep_scope, :retry_count, :retry_max

# @param manager [ManageIQ::Providers::BaseManager] A manager object
# @param target [Object] A refresh Target object
def initialize(manager, target = nil)
def initialize(manager)
@manager = manager
@target = target

@collections = {}

Expand Down Expand Up @@ -122,18 +120,16 @@ class << self
#
# @param json_data [String] input JSON data
# @return [ManageIQ::Providers::Inventory::Persister] Persister object loaded from a passed JSON
def from_json(json_data, manager, target = nil)
from_hash(JSON.parse(json_data), manager, target)
def from_json(json_data, manager)
from_hash(JSON.parse(json_data), manager)
end

# Returns Persister object built from serialized data
#
# @param persister_data [Hash] serialized Persister object in hash
# @return [ManageIQ::Providers::Inventory::Persister] Persister object built from serialized data
def from_hash(persister_data, manager, target = nil)
target ||= InventoryRefresh::TargetCollection.new(:manager => manager)

new(manager, target).tap do |persister|
def from_hash(persister_data, manager)
new(manager).tap do |persister|
persister_data['collections'].each do |collection|
inventory_collection = persister.collections[collection['name'].try(:to_sym)]
raise "Unrecognized InventoryCollection name: #{collection['name']}" if inventory_collection.blank?
Expand Down
Loading