class Sequent::Core::Persistors::ReplayOptimizedPostgresPersistor::Index

Attributes

indexed_columns[R]

Public Class Methods

new(indexed_columns) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 95
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

add(record) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 105
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
clear() click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 148
def clear
  @indexed_columns.each do |field|
    @indexes[field].clear
    @reverse_indexes[field].clear
  end
end
find(normalized_where_clause) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 127
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
remove(record) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 114
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
update(record) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 122
def update(record)
  remove(record)
  add(record)
end
use_index?(normalized_where_clause) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 155
def use_index?(normalized_where_clause)
  get_indexes(normalized_where_clause).present?
end

Private Instance Methods

get_indexes(normalized_where_clause) click to toggle source
# File lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb, line 161
def get_indexes(normalized_where_clause)
  @indexed_columns & normalized_where_clause.keys
end