Class: Xampl::AbstractCachingPersister
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.
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_cache ⇒ Object
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_cache ⇒ Object
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)
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
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
end
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
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_cleanup ⇒ Object
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_done ⇒ Object
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 = fresh_cache unless cache_map2
map2.each do |pid, xampl|
cache_map2[pid] = xampl
end
end
end
end
end
end
@new_cache = {}
end
|
#write_to_cache(xampl) ⇒ Object