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

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

Instance Method Summary collapse

Constructor Details

#initialize(indexed_columns) ⇒ Index

Returns a new instance of Index.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 66

def initialize(indexed_columns)
  @indexed_columns = Hash.new do |hash, record_class|
    if record_class.column_names.include? 'aggregate_id'
      hash[record_class] = [:aggregate_id]
    else
      hash[record_class] = []
    end
  end

  @indexed_columns.merge!(indexed_columns)

  @index = {}
  @reverse_index = {}
end

Instance Method Details

#add(record_class, record) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 81

def add(record_class, record)
  return unless indexed?(record_class)

  get_keys(record_class, record).each do |key|
    @index[key.hash] = [] unless @index.has_key? key.hash
    @index[key.hash] << record

    @reverse_index[record.object_id.hash] = [] unless @reverse_index.has_key? record.object_id.hash
    @reverse_index[record.object_id.hash] << key.hash
  end
end

#clearObject



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

def clear
  @index = {}
  @reverse_index = {}
end

#find(record_class, where_clause) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 111

def find(record_class, where_clause)
  key = [record_class.name]
  get_index(record_class, where_clause).each do |field|
    key << field
    key << where_clause[field]
  end
  @index[key.hash] || []
end

#remove(record_class, record) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 93

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



106
107
108
109
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 106

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

#use_index?(record_class, where_clause) ⇒ Boolean

Returns:



125
126
127
# File 'lib/sequent/core/persistors/replay_optimized_postgres_persistor.rb', line 125

def use_index?(record_class, where_clause)
  @indexed_columns.has_key?(record_class) && @indexed_columns[record_class].any? { |indexed_where| where_clause.keys.size == indexed_where.size && (where_clause.keys - indexed_where).empty? }
end