Class: Sequent::Core::Persistors::ReplayOptimizedPostgresPersistor::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexed_columns) ⇒ Index

Returns a new instance of Index.



94
95
96
97
98
99
100
101
102
# 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

Instance Attribute Details

#indexed_columnsObject (readonly)

Returns the value of attribute indexed_columns.



92
93
94
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 92

def indexed_columns
  @indexed_columns
end

Instance Method Details

#add(record) ⇒ Object



104
105
106
107
108
109
110
111
# 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

#clearObject



147
148
149
150
151
152
# 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

#find(normalized_where_clause) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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

#remove(record) ⇒ Object



113
114
115
116
117
118
119
# 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

#update(record) ⇒ Object



121
122
123
124
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 121

def update(record)
  remove(record)
  add(record)
end

#use_index?(normalized_where_clause) ⇒ Boolean

Returns:



154
155
156
# 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