Sha256: 00d8fb4518a1c1c8836e7287033b74cc6bcf63c42357fb0b84ca363e8da505db

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Postgres
    class DeserializerCache

      DEFAULT_MAX_SIZE = 500

      def initialize(max_size = nil)
        @cache = {}
        @max_size = max_size || DEFAULT_MAX_SIZE
      end

      def materialize(result, decorator_module = nil)

        return [] if result.ntuples == 0

        key = result.fields.join(',')

        # trivial fast LRU implementation
        materializer = @cache.delete(key)
        if materializer
          @cache[key] = materializer
        else
          materializer = @cache[key] = new_row_materializer(result)
          @cache.shift if @cache.length > @max_size
        end

        materializer.include(decorator_module) if decorator_module

        if decorator_module
          materializer = materializer.decorated(decorator_module)
        end

        i = 0
        r = []
        # quicker loop
        while i < result.ntuples
          r << materializer.materialize(result, i)
          i += 1
        end
        r
      end

      private

      def new_row_materializer(result)
        fields = result.fields

        Class.new do
          extend MiniSql::Decoratable
          include MiniSql::Result

          attr_accessor(*fields)

          instance_eval <<~RUBY
            def materialize(pg_result, index)
              r = self.new
              #{col = -1; fields.map { |f| "r.#{f} = pg_result.getvalue(index, #{col += 1})" }.join("; ")}
              r
            end
          RUBY
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mini_sql-1.6.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.5.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.4.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.3.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.2.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.1.3 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.1.2 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.1.1 lib/mini_sql/postgres_jdbc/deserializer_cache.rb
mini_sql-1.1.0 lib/mini_sql/postgres_jdbc/deserializer_cache.rb