Class: MiniSql::Postgres::DeserializerCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_sql/postgres/deserializer_cache.rb,
lib/mini_sql/postgres_jdbc/deserializer_cache.rb

Constant Summary collapse

DEFAULT_MAX_SIZE =
500

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil) ⇒ DeserializerCache

Returns a new instance of DeserializerCache.



7
8
9
10
# File 'lib/mini_sql/postgres/deserializer_cache.rb', line 7

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

Instance Method Details

#materialize(result) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mini_sql/postgres/deserializer_cache.rb', line 12

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