Class: ActiveRecord::Searchable::IndexManager

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/searchable/index_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, config, adapter) ⇒ IndexManager

Returns a new instance of IndexManager.



8
9
10
11
12
# File 'lib/activerecord/searchable/index_manager.rb', line 8

def initialize(model_class, config, adapter)
  @model_class = model_class
  @config = config
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/activerecord/searchable/index_manager.rb', line 6

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/activerecord/searchable/index_manager.rb', line 6

def config
  @config
end

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/activerecord/searchable/index_manager.rb', line 6

def model_class
  @model_class
end

Instance Method Details

#create(record) ⇒ Object



14
15
16
17
18
19
# File 'lib/activerecord/searchable/index_manager.rb', line 14

def create(record)
  validate_schema!
  values = extract_values(record)
  sql = adapter.insert_sql(config, record.id, values)
  execute_sql(sql)
end

#delete(record) ⇒ Object



36
37
38
39
# File 'lib/activerecord/searchable/index_manager.rb', line 36

def delete(record)
  sql = adapter.delete_sql(config, record.id)
  execute_sql(sql)
end

#reindex_allObject



41
42
43
# File 'lib/activerecord/searchable/index_manager.rb', line 41

def reindex_all
  model_class.find_each(&:reindex)
end

#update(record) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activerecord/searchable/index_manager.rb', line 21

def update(record)
  validate_schema!
  values = extract_values(record)
  sql = adapter.update_sql(config, record.id, values)

  # Atomic upsert pattern: Try UPDATE first (common case), fall back to INSERT if needed.
  # This is more efficient than INSERT OR REPLACE, which would always do DELETE+INSERT.
  # The transaction ensures no other process can INSERT between our UPDATE and INSERT attempts,
  # preventing race conditions while maintaining optimal performance for the update-heavy path.
  model_class.transaction do
    updated = execute_sql(sql)
    create(record) unless updated
  end
end