Class: Katello::ContentUnitIndexer

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/content_unit_indexer.rb

Defined Under Namespace

Classes: RepoAssociationTracker

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type:, repository: nil, pulp_content_ids: nil, optimized: true) ⇒ ContentUnitIndexer

Returns a new instance of ContentUnitIndexer.



3
4
5
6
7
8
9
10
11
# File 'app/services/katello/content_unit_indexer.rb', line 3

def initialize(content_type:, repository: nil, pulp_content_ids: nil, optimized: true)
  @content_type = content_type
  @model_class = content_type.model_class
  @service_class = SmartProxy.pulp_primary!.content_service(content_type)
  @repository = repository
  @content_type = content_type
  @pulp_content_ids = pulp_content_ids
  @optimized = optimized
end

Class Method Details

.insert_timestamps(model_class, units) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'app/services/katello/content_unit_indexer.rb', line 125

def self.insert_timestamps(model_class, units)
  dates = model_class.columns.map(&:name).include?("created_at")
  return units unless dates
  units.each do |row|
    row[:created_at] = DateTime.now
    row[:updated_at] = DateTime.now
  end
  units
end

.pulp_id_to_id_map(content_type, pulp_ids) ⇒ Object



71
72
73
74
75
76
77
# File 'app/services/katello/content_unit_indexer.rb', line 71

def self.pulp_id_to_id_map(content_type, pulp_ids)
  map = {}
  content_type.model_class.with_pulp_id(pulp_ids).select(:id, :pulp_id).each do |model|
    map[model.pulp_id] = model.id
  end
  map
end

Instance Method Details

#association_class_uniqiness_attributesObject



153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/katello/content_unit_indexer.rb', line 153

def association_class_uniqiness_attributes
  columns = [@model_class.unit_id_field, 'repository_id']
  found = ActiveRecord::Base.connection.indexes(@model_class.repository_association_class.table_name).find do |index|
    index.columns.sort == columns.sort
  end
  if found
    found.columns
  else
    fail "Unable to find unique index for #{columns} on table #{self.repository_association_class.table_name}"
  end
end

#fetch_only_idsObject



135
136
137
138
139
140
# File 'app/services/katello/content_unit_indexer.rb', line 135

def fetch_only_ids
  @optimized && @repository &&
    !@repository.content_view.default? &&
    !@repository.repository_type.unique_content_per_repo &&
    @service_class.supports_id_fetch?
end

#import_all(filtered_indexing = false) ⇒ Object



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
49
50
51
52
53
54
55
56
# File 'app/services/katello/content_unit_indexer.rb', line 24

def import_all(filtered_indexing = false)
  association_tracker = RepoAssociationTracker.new(@content_type, @service_class, @repository)
  units_from_pulp.each do |units|
    units.each do |unit|
      association_tracker.push(unit)
      remove_duplicates(unit)
    end

    unless fetch_only_ids
      to_insert = units.map do |unit|
        if @content_type.generic?
          @service_class.generate_model_row(unit, @content_type)
        else
          @service_class.generate_model_row(unit)
        end
      end

      next if to_insert.empty?
      insert_timestamps(to_insert)
      if @content_type.mutable
        @model_class.upsert_all(to_insert, unique_by: :pulp_id)
      else
        @model_class.insert_all(to_insert, unique_by: :pulp_id)
      end
    end

    import_associations(units) if @repository
  end

  if @model_class.many_repository_associations && @repository
    sync_repository_associations(association_tracker, additive: filtered_indexing)
  end
end

#import_associations(units) ⇒ Object



58
59
60
61
# File 'app/services/katello/content_unit_indexer.rb', line 58

def import_associations(units)
  pulp_id_to_id = self.class.pulp_id_to_id_map(@content_type, units.map { |unit| unit[@service_class.unit_identifier] })
  @service_class.insert_child_associations(units, pulp_id_to_id) if @service_class.respond_to?(:insert_child_associations)
end

#insert_timestamps(units) ⇒ Object



121
122
123
# File 'app/services/katello/content_unit_indexer.rb', line 121

def insert_timestamps(units)
  self.class.insert_timestamps(@model_class, units)
end

#remove_duplicates(unit) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'app/services/katello/content_unit_indexer.rb', line 13

def remove_duplicates(unit)
  #when we are uploading units, we need to remove any duplicates from our indexed data
  if @content_type.label == 'rpm' && @repository && @pulp_content_ids
    rpms_to_disassociate = ::Katello::Rpm.where(name: unit[:name], version: unit[:version], release: unit[:release],
                                                epoch: unit[:epoch], arch: unit[:arch]).select(:id)
    if rpms_to_disassociate.any?
      ::Katello::RepositoryRpm.where(rpm_id: rpms_to_disassociate, repository_id: @repository.id).destroy_all
    end
  end
end

#sync_repository_associations(assocication_tracker, additive: false) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'app/services/katello/content_unit_indexer.rb', line 142

def sync_repository_associations(assocication_tracker, additive: false)
  unless additive
    ActiveRecord::Base.connection.uncached do
      @model_class.repository_association_class.where(repository_id: @repository.id).where.
        not(@model_class.unit_id_field => assocication_tracker.unit_ids).delete_all
    end
  end
  return if assocication_tracker.db_values.empty?
  @model_class.repository_association_class.upsert_all(assocication_tracker.db_values, :unique_by => association_class_uniqiness_attributes)
end

#units_from_pulp(&block) ⇒ Object



63
64
65
66
67
68
69
# File 'app/services/katello/content_unit_indexer.rb', line 63

def units_from_pulp(&block)
  if @pulp_content_ids
    @service_class.pulp_units_batch_all(@pulp_content_ids, &block)
  elsif @repository
    @service_class.pulp_units_batch_for_repo(@repository, fetch_identifiers: fetch_only_ids, content_type: @content_type, &block)
  end
end