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.



9
10
11
12
# File 'lib/mini_sql/postgres/deserializer_cache.rb', line 9

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

Instance Method Details

#materialize(result, decorator_module = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mini_sql/postgres/deserializer_cache.rb', line 28

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

  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

#materializer(result) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mini_sql/postgres/deserializer_cache.rb', line 14

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

  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
end