Class: Xampl::AbstractCachingPersister

Inherits:
Persister
  • Object
show all
Defined in:
lib/xamplr/persisters/caching.rb

Direct Known Subclasses

FilesystemPersister, TokyoCabinetPersister

Instance Attribute Summary

Attributes inherited from Persister

#automatic, #block_changes, #cache_hits, #expunged, #format, #last_write_count, #name, #read_count, #rolled_back, #slow_sync, #syncing, #total_cache_hits, #total_read_count, #total_rollback_count, #total_sync_count, #total_write_count, #write_count

Instance Method Summary collapse

Methods inherited from Persister

#busy, #close, #count_changed, #do_sync_write, #done_sync_write, #expunge, #find_known, #find_xampl, #has_changed, #has_not_changed, #introduce, #is_busy, #lazy_load, #lookup, #optimise, #print_stats, #put_changed, #query_implemented, #realise, replace, #represent, #rollback, #shutdown, #start_sync_write, #sync, #write

Constructor Details

#initialize(root, name = nil, format = nil, capacity = DEFAULT_CAPACITY) ⇒ AbstractCachingPersister

Returns a new instance of AbstractCachingPersister.

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/xamplr/persisters/caching.rb', line 8

def initialize(root, name=nil, format=nil, capacity=DEFAULT_CAPACITY)
  super(name, format)

  raise XamplException.new(:name_required) unless name

  @root_dir = File.join(root, name)
  @repo_root = root
  @repo_name = name

  @capacity = capacity
  @cache = {}
  @new_cache = {}
end

Instance Method Details

#cache(xampl) ⇒ Object



69
70
71
72
# File 'lib/xamplr/persisters/caching.rb', line 69

def cache(xampl)
  Xampl.store_in_map(@new_cache, xampl) { xampl }
  xampl.introduce_persister(self)
end

#clear_cacheObject



80
81
82
83
# File 'lib/xamplr/persisters/caching.rb', line 80

def clear_cache
  @cache = {}
  @new_cache = {}
end

#dump(cache) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/xamplr/persisters/caching.rb', line 125

def dump(cache)
  puts "DUMP CACHE"
  cache.each do | k, v |
    puts "--- [[#{k}]]"
    v.each do | kk, vv |
      puts "--- --- [[#{kk}]]"
      puts vv.print
    end
  end
end

#fresh_cacheObject



22
23
24
# File 'lib/xamplr/persisters/caching.rb', line 22

def fresh_cache
  return XamplCache.new(@capacity)
end

#read(klass, pid, target = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/xamplr/persisters/caching.rb', line 136

def read(klass, pid, target=nil)
  #return nil unless pid

#      puts "#{File.basename(__FILE__)} #{__LINE__} READ:: klass: #{klass} pid: #{pid} target: [[#{target}]], PM: #{ self }"

  xampl, target = read_from_cache(klass, pid, target)
  return xampl if xampl and !target

  representation = read_representation(klass, pid)
  return nil unless representation

  xampl = nil
  begin
    #puts "ABSTRACT_READ[#{__LINE__}]:: klass: #{klass} pid: #{pid} target: #{target}"
    xampl = realise(representation, target)
    return nil unless xampl
  rescue => e
    raise RuntimeError, "FAILED TO READ -- persister: #{name} klass: #{klass} pid: #{pid} target: #{target}\n#{ e }", e.backtrace
    #puts "FAILED TO READ -- persister: #{name} klass: #{klass} pid: #{pid} target: #{target}"
    #puts "Exception: #{e}"
    #print e.backtrace.join("\n")
    #raise e
  end

  #      puts "#{File.basename(__FILE__)} #{__LINE__} STORE IN CACHE:: xampl: #{xampl }, persister: #{ self }"
  #      dump(@cache)

  Xampl.store_in_cache(@cache, xampl, self) { xampl }
  xampl.introduce_persister(self)

  @read_count = @read_count + 1
  xampl.changes_accepted
  @changed.delete(xampl)

  return xampl
end

#read_from_cache(klass, pid, target = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xamplr/persisters/caching.rb', line 89

def read_from_cache(klass, pid, target=nil)
  xampl = Xampl.lookup_in_map(@cache, klass, pid)
  if xampl then
    if target and target != xampl then

      #TODO -- report this better
      #puts "#{File.basename(__FILE__)} #{__LINE__} CACHE CONFLICT:: klass: #{ klass }, pid: #{ pid }, target: #{ target }, cached: #{ xampl }"
      #dump(@cache)
      #caller(0).each { | trace | puts "  #{trace}"}

      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl, target
    end
    return xampl, xampl
  end

  xampl = Xampl.lookup_in_map(@new_cache, klass, pid)
  if xampl then
    if target and target != xampl then
      target.invalidate
      raise XamplException.new(:cache_conflict)
    end
    unless xampl.load_needed then
      @cache_hits = @cache_hits + 1
      return xampl, target
    end
    return xampl, xampl
  end

  return nil, target
end

#rollback_cleanupObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xamplr/persisters/caching.rb', line 50

def rollback_cleanup
  @new_cache.each do |name, map|
    if map then
      map.each do |name2, map2|
        if map2 then
          map2.each do |pid, xampl|
            @changed.delete(xampl)
          end
        end
      end
    end
    @changed.each do |xampl, ignore|
      xampl.force_load
    end
    @new_cache = {}
    super
  end
end

#sync_doneObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xamplr/persisters/caching.rb', line 26

def sync_done
  if @new_cache then
    @new_cache.each do |name1, map1|
      if map1 then
        cache_map1 = @cache[name1]
        @cache[name1] = cache_map1 = {} unless cache_map1
        map1.each do |name2, map2|
          if map2 then
            cache_map2 = cache_map1[name2]
            #cache_map1[name2] = cache_map2 = {} unless cache_map2
            cache_map1[name2] = cache_map2 = fresh_cache unless cache_map2

            #cache_map2.merge!(map2)
            map2.each do |pid, xampl|
              cache_map2[pid] = xampl
            end
          end
        end
      end
    end
  end
  @new_cache = {}
end

#uncache(xampl) ⇒ Object



74
75
76
77
78
# File 'lib/xamplr/persisters/caching.rb', line 74

def uncache(xampl)
  @changed.delete(xampl)
  Xampl.remove_from_map(@cache, xampl)
  return Xampl.remove_from_map(@new_cache, xampl)
end

#write_to_cache(xampl) ⇒ Object



85
86
87
# File 'lib/xamplr/persisters/caching.rb', line 85

def write_to_cache(xampl)
  return Xampl.store_in_cache(@cache, xampl, self) { xampl }
end