Class: Xampl::InMemoryPersister
Instance Attribute Summary
Attributes inherited from Persister
#automatic, #block_changes, #cache_hits, #expunged, #format, #last_write_count, #name, #read_count, #rolled_back, #syncing, #total_cache_hits, #total_read_count, #total_rollback_count, #total_sync_count, #total_write_count, #write_count
Class Method Summary
collapse
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
Constructor Details
#initialize(name = nil, format = nil, capacity = 20) ⇒ InMemoryPersister
Returns a new instance of InMemoryPersister.
7
8
9
10
11
12
13
14
|
# File 'lib/xamplr/persisters/in-memory.rb', line 7
def initialize(name=nil, format=nil, capacity=20)
super(name, format)
@module_map = {}
@capacity = capacity
@cache = {}
@new_cache = {}
end
|
Class Method Details
.kind ⇒ Object
16
17
18
|
# File 'lib/xamplr/persisters/in-memory.rb', line 16
def InMemoryPersister.kind
:in_memory
end
|
Instance Method Details
#cache(xampl) ⇒ Object
70
71
72
|
# File 'lib/xamplr/persisters/in-memory.rb', line 70
def cache(xampl)
return Xampl.store_in_map(@new_cache, xampl) { xampl }
end
|
#clear_cache ⇒ Object
80
81
82
83
|
# File 'lib/xamplr/persisters/in-memory.rb', line 80
def clear_cache
@new_cache = {}
@cache = {}
end
|
#fresh_cache ⇒ Object
24
25
26
|
# File 'lib/xamplr/persisters/in-memory.rb', line 24
def fresh_cache
return XamplCache.new(@capacity)
end
|
#read(klass, pid, target = nil) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/xamplr/persisters/in-memory.rb', line 160
def read(klass, pid, target=nil)
xampl, target = read_from_cache(klass, pid, target)
return xampl if xampl and !target
representation = Xampl.lookup_in_map(@module_map, klass, pid)
return nil unless representation
xampl = realise(representation, target)
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
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
124
125
126
|
# File 'lib/xamplr/persisters/in-memory.rb', line 98
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
68
|
# File 'lib/xamplr/persisters/in-memory.rb', line 50
def rollback_cleanup
@new_cache.each{ | name, map |
if map then
map.each{ | name2, map2 |
if map2 then
map2.each{ | pid, xampl |
@changed.delete(xampl)
xampl.invalidate
}
end
}
end
}
@changed.each{ | xampl, ignore|
xampl.force_load
}
@new_cache = {}
super
end
|
#sync_done ⇒ Object
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/in-memory.rb', line 28
def sync_done
if @new_cache then
@new_cache.each{ | name1, map1 |
if map1 then
cache_map1 = @cache[name1]
@cache[name1] = cache_map1 = {} unless cache_map1
map1.each{ | name2, map2 |
if map2 then
cache_map2 = cache_map1[name2]
cache_map1[name2] = cache_map2 = self.fresh_cache unless cache_map2
map2.each{ | pid, xampl |
cache_map2[pid] = xampl
}
end
}
end
}
end
@new_cache = {}
end
|
#write(xampl) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/xamplr/persisters/in-memory.rb', line 85
def write(xampl)
raise XamplException.new(:no_index_so_no_persist) unless xampl.get_the_index
if Xampl.store_in_map(@module_map, xampl) { represent(xampl) } then
@write_count = @write_count + 1
xampl.changes_accepted
return true
else
return false
end
end
|
#xxread_from_cache(klass, pid, target = nil) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/xamplr/persisters/in-memory.rb', line 128
def xxread_from_cache(klass, pid, target=nil)
xampl = Xampl.lookup_in_map(@cache, klass, pid)
if (nil != xampl) then
puts "READ CACHE pid #{pid} -- #{target} #{xampl}" if target and target != xampl
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
end
target = xampl
end
xampl = Xampl.lookup_in_map(@new_cache, klass, pid)
if (nil != xampl) then
puts "READ CACHE pid #{pid} -- #{target} #{xampl}" if target and target != xampl
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
end
target = xampl
end
return xampl
end
|