Sha256: 271a2e7dc2f0fbd1188c234927829afe1acca23239b06c4701896c706252ce5d

Contents?: true

Size: 1.07 KB

Versions: 51

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    class StatementPool # :nodoc:
      include Enumerable

      DEFAULT_STATEMENT_LIMIT = 1000

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

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

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

      def [](key)
        cache[key]
      end

      def length
        cache.length
      end

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

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

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

      private

        def cache
          @cache[Process.pid]
        end

        def dealloc(stmt)
          raise NotImplementedError
        end
    end
  end
end

Version data entries

51 entries across 51 versions & 7 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.8 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.7.1 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.7 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.6.3 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.6.2 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.6.1 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.6 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.6 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.5 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.5 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.4 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.3 lib/active_record/connection_adapters/statement_pool.rb
activerecord-6.0.2.2 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.2 lib/active_record/connection_adapters/statement_pool.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/statement_pool.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/statement_pool.rb
activerecord-6.0.2.1 lib/active_record/connection_adapters/statement_pool.rb
activerecord-5.2.4.1 lib/active_record/connection_adapters/statement_pool.rb
activerecord-6.0.2 lib/active_record/connection_adapters/statement_pool.rb