Sha256: 9f1f3dea5399be713bff68ab95e5ce0da1a16c07be41b297dda3bce4e6963735

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Sqlite
    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)

        key = result.columns.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

        if decorator_module
          materializer = materializer.decorated(decorator_module)
        end

        r = []
        # quicker loop
        while !result.eof?
          data = result.next
          if data
            r << materializer.materialize(data)
          end
        end
        r
      end

      private

      def new_row_materializer(result)
        fields = result.columns

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

          attr_accessor(*fields)

          instance_eval <<~RUBY
            def materialize(data)
              r = self.new
              #{col = -1; fields.map { |f| "r.#{f} = data[#{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/sqlite/deserializer_cache.rb
mini_sql-1.5.0 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.4.0 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.3.0 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.2.0 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.1.3 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.1.2 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.1.1 lib/mini_sql/sqlite/deserializer_cache.rb
mini_sql-1.1.0 lib/mini_sql/sqlite/deserializer_cache.rb