Class: Sequent::Core::Persistors::ReplayOptimizedPostgresPersistor::Index
- Inherits:
-
Object
- Object
- Sequent::Core::Persistors::ReplayOptimizedPostgresPersistor::Index
- Defined in:
- lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb
Instance Method Summary collapse
- #add(record_class, record) ⇒ Object
- #clear ⇒ Object
- #find(record_class, where_clause) ⇒ Object
-
#initialize(indexed_columns) ⇒ Index
constructor
A new instance of Index.
- #remove(record_class, record) ⇒ Object
- #update(record_class, record) ⇒ Object
- #use_index?(record_class, where_clause) ⇒ Boolean
Constructor Details
#initialize(indexed_columns) ⇒ Index
Returns a new instance of Index.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 73 def initialize(indexed_columns) @indexed_columns = Hash.new do |hash, record_class| hash[record_class] = if record_class.column_names.include? 'aggregate_id' ['aggregate_id'] else [] end end @indexed_columns = @indexed_columns.merge( indexed_columns.reduce({}) do |memo, (key, ics)| memo.merge({key => ics.map { |c| c.map(&:to_s) }}) end, ) @index = {} @reverse_index = {} end |
Instance Method Details
#add(record_class, record) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 92 def add(record_class, record) return unless indexed?(record_class) get_keys(record_class, record).each do |key| @index[key.hash] = [] unless @index.key? key.hash @index[key.hash] << record @reverse_index[record.object_id.hash] = [] unless @reverse_index.key? record.object_id.hash @reverse_index[record.object_id.hash] << key.hash end end |
#clear ⇒ Object
131 132 133 134 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 131 def clear @index = {} @reverse_index = {} end |
#find(record_class, where_clause) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 122 def find(record_class, where_clause) key = [record_class.name] get_index(record_class, where_clause).each do |field| key << field key << where_clause.stringify_keys[field] end @index[key.hash] || [] end |
#remove(record_class, record) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 104 def remove(record_class, record) return unless indexed?(record_class) keys = @reverse_index.delete(record.object_id.hash) { [] } return unless keys.any? keys.each do |key| @index[key].delete(record) @index.delete(key) if @index[key].count == 0 end end |
#update(record_class, record) ⇒ Object
117 118 119 120 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 117 def update(record_class, record) remove(record_class, record) add(record_class, record) end |
#use_index?(record_class, where_clause) ⇒ Boolean
136 137 138 |
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 136 def use_index?(record_class, where_clause) @indexed_columns.key?(record_class) && get_index(record_class, where_clause).present? end |