class Sequent::Core::Persistors::ReplayOptimizedPostgresPersistor::Index
Attributes
Public Class Methods
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 94 def initialize(indexed_columns) @indexed_columns = indexed_columns.to_set @indexes = @indexed_columns.to_h do |field| [field, {}] end @reverse_indexes = @indexed_columns.to_h do |field| [field, {}.compare_by_identity] end end
Public Instance Methods
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 104 def add(record) @indexes.map do |field, index| key = Persistors.normalize_symbols(record[field]).freeze records = index[key] || (index[key] = Set.new.compare_by_identity) records << record @reverse_indexes[field][record] = key end end
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 147 def clear @indexed_columns.each do |field| @indexes[field].clear @reverse_indexes[field].clear end end
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 126 def find(normalized_where_clause) record_sets = normalized_where_clause.map do |(field, expected_value)| if expected_value.is_a?(Array) expected_value.reduce(Set.new.compare_by_identity) do |memo, value| key = Persistors.normalize_symbols(value) memo.merge(@indexes[field][key] || []) end else key = Persistors.normalize_symbols(expected_value) @indexes[field][key] || Set.new.compare_by_identity end end smallest, *rest = record_sets.sort_by(&:size) return smallest.to_a if smallest.empty? || rest.empty? smallest.select do |record| rest.all? { |x| x.include? record } end end
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 113 def remove(record) @indexes.map do |field, index| key = @reverse_indexes[field].delete(record) remaining = index[key]&.delete(record) index.delete(key) if remaining&.empty? end end
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 121 def update(record) remove(record) add(record) end
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 154 def use_index?(normalized_where_clause) get_indexes(normalized_where_clause).present? end
Private Instance Methods
Source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 160 def get_indexes(normalized_where_clause) @indexed_columns & normalized_where_clause.keys end