Sha256: 21ff6c507d59cb65ad958f9fda0fca0d65a66c1bd5ce91d76d7bf9b0ac9f272b
Contents?: true
Size: 1.47 KB
Versions: 6
Compression:
Stored size: 1.47 KB
Contents
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) return [] if result.ntuples == 0 key = result.fields # trivial fast LRU implementation materializer = @cache.delete(key) if materializer @cache[key] = materializer else materializer = @cache[key] = new_row_matrializer(result) @cache.shift if @cache.length > @max_size end i = 0 r = [] # quicker loop while i < result.ntuples r << materializer.materialize(result, i) i += 1 end r end private def new_row_matrializer(result) fields = result.fields Class.new do attr_accessor(*fields) # AM serializer support alias :read_attribute_for_serialization :send def to_h r = {} instance_variables.each do |f| r[f.to_s.sub('@','').to_sym] = instance_variable_get(f) end r end 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
6 entries across 6 versions & 1 rubygems