Class: RGen::Fragment::DumpFileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/fragment/dump_file_cache.rb

Overview

Caches model fragments in Ruby dump files.

Dump files are created per each fragment file.

The main goal is to support fast loading and joining of fragments. Therefore the cache stores additional information which makes the joining process faster (adding to environment, resolving references)

Instance Method Summary collapse

Constructor Details

#initialize(cache_map) ⇒ DumpFileCache

cache_map must be an object responding to load_data and store_data for loading or storing data associated with a file; this can be an instance of Util::FileCacheMap



18
19
20
# File 'lib/rgen/fragment/dump_file_cache.rb', line 18

def initialize(cache_map)
  @cache_map = cache_map 
end

Instance Method Details

#load(fragment) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rgen/fragment/dump_file_cache.rb', line 40

def load(fragment)
  dump = @cache_map.load_data(fragment.location)
  return :invalid if dump == :invalid
  header = Marshal.load(dump)
  fragment.set_root_elements(header[:root_elements],
    :elements => header[:elements],
    :index => header[:index],
    :unresolved_refs => header[:unresolved_refs])
  fragment.data = header[:data]
  if header[:fragment_ref]
    fragment.fragment_ref = header[:fragment_ref]
    fragment.fragment_ref.fragment = fragment
  else
    raise "no fragment_ref in fragment loaded from cache"
  end
end

#store(fragment) ⇒ Object

Note that the fragment must not be connected to other fragments by resolved references unresolve the fragment if necessary



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rgen/fragment/dump_file_cache.rb', line 24

def store(fragment)
  fref = fragment.fragment_ref
  # temporarily remove the reference to the fragment to avoid dumping the fragment
  fref.fragment = nil
  @cache_map.store_data(fragment.location,
    Marshal.dump({
      :root_elements => fragment.root_elements,
      :elements => fragment.elements,
      :index => fragment.index,
      :unresolved_refs => fragment.unresolved_refs,
      :fragment_ref => fref,
      :data => fragment.data
    }))
  fref.fragment = fragment
end