Class: ActiveRecord::ConnectionAdapters::StatementPool

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
activerecord/lib/active_record/connection_adapters/statement_pool.rb

Overview

:nodoc:

Direct Known Subclasses

AbstractMysqlAdapter::StatementPool

Constant Summary collapse

DEFAULT_STATEMENT_LIMIT =
1000

Instance Method Summary collapse

Methods included from Enumerable

#as_json

Constructor Details

#initialize(statement_limit = nil) ⇒ StatementPool

Returns a new instance of StatementPool.



10
11
12
13
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 10

def initialize(statement_limit = nil)
  @cache = Hash.new { |h, pid| h[pid] = {} }
  @statement_limit = statement_limit || DEFAULT_STATEMENT_LIMIT
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 23

def [](key)
  cache[key]
end

#[]=(sql, stmt) ⇒ Object



31
32
33
34
35
36
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 31

def []=(sql, stmt)
  while @statement_limit <= cache.size
    dealloc(cache.shift.last)
  end
  cache[sql] = stmt
end

#clearObject



38
39
40
41
42
43
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 38

def clear
  cache.each_value do |stmt|
    dealloc stmt
  end
  cache.clear
end

#delete(key) ⇒ Object



52
53
54
55
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 52

def delete(key)
  dealloc cache[key]
  cache.delete(key)
end

#each(&block) ⇒ Object



15
16
17
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 15

def each(&block)
  cache.each(&block)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 19

def key?(key)
  cache.key?(key)
end

#lengthObject



27
28
29
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 27

def length
  cache.length
end

#resetObject

Clear the pool without deallocating; this is only safe when we know the server has independently deallocated all statements (e.g. due to a reconnect, or a DISCARD ALL)



48
49
50
# File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 48

def reset
  cache.clear
end