Class: ActiveRecord::Materialized::RelationCacheWriter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/materialized/relation_cache_writer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Materializes a relation into a cache table via INSERT … SELECT, with an atomic swap on full refresh.

Instance Method Summary collapse

Constructor Details

#initialize(view_class) ⇒ RelationCacheWriter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RelationCacheWriter.



9
10
11
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 9

def initialize(view_class)
  @view_class = view_class
end

Instance Method Details

#atomic_swap!(relation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
44
45
46
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 37

def atomic_swap!(relation)
  temp_table = refresh_temp_table_name
  old_table = refresh_old_table_name

  populate_temp_table!(temp_table, relation)
  TableSwap.new(view_class).swap!(temp_table, old_table)

  view_class.reset_column_information
  cache_row_count
end

#bootstrap!(relation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 13

def bootstrap!(relation)
  CacheTableSchema.ensure_table!(view_class, relation)
  replace_all!(relation)
end

#populate_temp_table!(temp_table, relation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
52
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 48

def populate_temp_table!(temp_table, relation)
  CacheTableSchema.create_table!(view_class, temp_table, relation)
  temp_model = temporary_model(temp_table)
  self.class.new(temp_model).replace_all!(relation)
end

#refresh_old_table_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 58

def refresh_old_table_name
  "#{view_class.table_name}_old_#{SecureRandom.hex(4)}"
end

#refresh_temp_table_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 54

def refresh_temp_table_name
  "#{view_class.table_name}_refresh_#{SecureRandom.hex(4)}"
end

#replace_all!(relation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 18

def replace_all!(relation)
  view_class.transaction do
    view_class.delete_all
    insert_rows!(relation)
  end
  cache_row_count
end

#replace_partitions!(relation, key_tuples:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Scoped in-place maintenance: delete + re-insert only the affected partitions. A full recompute goes through atomic_swap! instead (see IncrementalMaintainer#recompute_all!), so this only ever handles a bounded set of partition keys.



29
30
31
32
33
34
35
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 29

def replace_partitions!(relation, key_tuples:)
  view_class.transaction do
    delete_partitions!(key_tuples)
    insert_rows!(relation)
  end
  cache_row_count
end