Class: Xampl::Persister
- Inherits:
-
Object
- Object
- Xampl::Persister
- Defined in:
- lib/xamplr/persister.rb
Direct Known Subclasses
AbstractCachingPersister, InMemoryPersister, SimplePersister
Instance Attribute Summary collapse
-
#automatic ⇒ Object
Returns the value of attribute automatic.
-
#block_changes ⇒ Object
Returns the value of attribute block_changes.
-
#cache_hits ⇒ Object
Returns the value of attribute cache_hits.
-
#expunged ⇒ Object
Returns the value of attribute expunged.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#last_write_count ⇒ Object
Returns the value of attribute last_write_count.
-
#name ⇒ Object
Returns the value of attribute name.
-
#read_count ⇒ Object
Returns the value of attribute read_count.
-
#rolled_back ⇒ Object
Returns the value of attribute rolled_back.
-
#syncing ⇒ Object
readonly
Returns the value of attribute syncing.
-
#total_cache_hits ⇒ Object
Returns the value of attribute total_cache_hits.
-
#total_read_count ⇒ Object
Returns the value of attribute total_read_count.
-
#total_rollback_count ⇒ Object
Returns the value of attribute total_rollback_count.
-
#total_sync_count ⇒ Object
Returns the value of attribute total_sync_count.
-
#total_write_count ⇒ Object
Returns the value of attribute total_write_count.
-
#write_count ⇒ Object
Returns the value of attribute write_count.
Class Method Summary collapse
Instance Method Summary collapse
- #busy(yes) ⇒ Object
- #cache(xampl) ⇒ Object
- #clear_cache ⇒ Object
- #close ⇒ Object
- #count_changed ⇒ Object
- #do_sync_write ⇒ Object
- #done_sync_write ⇒ Object
- #expunge(xampl) ⇒ Object
- #find_known(klass, pid) ⇒ Object
- #find_xampl(hint = false) ⇒ Object
- #has_changed(xampl) ⇒ Object
- #has_not_changed(xampl) ⇒ Object
-
#initialize(name = nil, format = nil) ⇒ Persister
constructor
A new instance of Persister.
- #introduce(xampl) ⇒ Object
- #is_busy ⇒ Object
- #lazy_load(target, klass, pid) ⇒ Object
- #lookup(klass, pid) ⇒ Object
- #optimise(opts) ⇒ Object
- #print_stats ⇒ Object
- #put_changed(msg = "") ⇒ Object
- #query_implemented ⇒ Object
- #read(klass, pid, target = nil) ⇒ Object
- #realise(representation, target = nil) ⇒ Object
- #represent(xampl, mentions = []) ⇒ Object
- #rollback ⇒ Object
- #rollback_cleanup ⇒ Object
- #shutdown ⇒ Object
- #start_sync_write ⇒ Object
- #sync ⇒ Object
- #sync_done ⇒ Object
- #uncache(xampl) ⇒ Object
- #write(xampl) ⇒ Object
Constructor Details
#initialize(name = nil, format = nil) ⇒ Persister
Returns a new instance of Persister.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/xamplr/persister.rb', line 19 def initialize(name=nil, format=nil) @name = name @format = format @automatic = false @changed = {} @expunged = Set.new @cache_hits = 0 @total_cache_hits = 0 @read_count = 0 @total_read_count = 0 @write_count = 0 @total_write_count = 0 @last_write_count = 0 @last_cache_hits = 0 @total_sync_count = 0 @total_rollback_count = 0 @rolled_back = false @syncing = false @busy_count = 0 end |
Instance Attribute Details
#automatic ⇒ Object
Returns the value of attribute automatic.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def automatic @automatic end |
#block_changes ⇒ Object
Returns the value of attribute block_changes.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def block_changes @block_changes end |
#cache_hits ⇒ Object
Returns the value of attribute cache_hits.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def cache_hits @cache_hits end |
#expunged ⇒ Object
Returns the value of attribute expunged.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def expunged @expunged end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
17 18 19 |
# File 'lib/xamplr/persister.rb', line 17 def format @format end |
#last_write_count ⇒ Object
Returns the value of attribute last_write_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def last_write_count @last_write_count end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def name @name end |
#read_count ⇒ Object
Returns the value of attribute read_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def read_count @read_count end |
#rolled_back ⇒ Object
Returns the value of attribute rolled_back.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def rolled_back @rolled_back end |
#syncing ⇒ Object (readonly)
Returns the value of attribute syncing.
17 18 19 |
# File 'lib/xamplr/persister.rb', line 17 def syncing @syncing end |
#total_cache_hits ⇒ Object
Returns the value of attribute total_cache_hits.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def total_cache_hits @total_cache_hits end |
#total_read_count ⇒ Object
Returns the value of attribute total_read_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def total_read_count @total_read_count end |
#total_rollback_count ⇒ Object
Returns the value of attribute total_rollback_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def total_rollback_count @total_rollback_count end |
#total_sync_count ⇒ Object
Returns the value of attribute total_sync_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def total_sync_count @total_sync_count end |
#total_write_count ⇒ Object
Returns the value of attribute total_write_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def total_write_count @total_write_count end |
#write_count ⇒ Object
Returns the value of attribute write_count.
7 8 9 |
# File 'lib/xamplr/persister.rb', line 7 def write_count @write_count end |
Class Method Details
.replace(old_xampl, new_xampl) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/xamplr/persister.rb', line 111 def Persister.replace(old_xampl, new_xampl) pid = old_xampl.get_the_index if old_xampl.persister != @@persister then raise MixedPersisters.new(@@persister, old_xampl.persister) end if new_xampl.persister != @@persister then raise MixedPersisters.new(@@persister, new_xampl.persister) end new_xampl.note_replacing(old_xampl) unless old_xampl.load_needed then Xampl.log.warn("Replacing live xampl: #{old_xampl} pid: #{pid}") @@persister.uncache(old_xampl) old_xampl.invalidate end new_xampl.pid = nil new_xampl.pid = pid @@persister.introduce(new_xampl) end |
Instance Method Details
#busy(yes) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/xamplr/persister.rb', line 53 def busy(yes) if yes then @busy_count += 1 elsif 0 < @busy_count then @busy_count -= 1 end end |
#cache(xampl) ⇒ Object
99 100 101 |
# File 'lib/xamplr/persister.rb', line 99 def cache(xampl) raise XamplException.new(:unimplemented) end |
#clear_cache ⇒ Object
107 108 109 |
# File 'lib/xamplr/persister.rb', line 107 def clear_cache raise XamplException.new(:unimplemented) end |
#close ⇒ Object
44 45 46 |
# File 'lib/xamplr/persister.rb', line 44 def close self.sync end |
#count_changed ⇒ Object
92 93 94 95 96 97 |
# File 'lib/xamplr/persister.rb', line 92 def count_changed # @changed.each{ | thing, ignore | # puts "changed: #{thing}, index: #{thing.get_the_index}" # } return @changed.size end |
#do_sync_write ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/xamplr/persister.rb', line 225 def do_sync_write unchanged_in_changed_list = 0 @changed.each do |xampl, ignore| unchanged_in_changed_list += 1 unless xampl.is_changed unless xampl.kind_of?(InvalidXampl) then write(xampl) end end end |
#done_sync_write ⇒ Object
221 222 223 |
# File 'lib/xamplr/persister.rb', line 221 def done_sync_write # only if needed end |
#expunge(xampl) ⇒ Object
174 175 176 |
# File 'lib/xamplr/persister.rb', line 174 def expunge(xampl) false end |
#find_known(klass, pid) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/xamplr/persister.rb', line 194 def find_known(klass, pid) #raise XamplException.new(:live_across_rollback) if @rolled_back xampl = read_from_cache(klass, pid, nil) return xampl end |
#find_xampl(hint = false) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/xamplr/persister.rb', line 166 def find_xampl(hint=false) if hint then return [], "no query made" else return [] end end |
#has_changed(xampl) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/xamplr/persister.rb', line 72 def has_changed(xampl) #raise XamplException.new(:live_across_rollback) if @rolled_back # puts "!!!! has_changed #{xampl} #{xampl.get_the_index} -- persist required: #{xampl.persist_required}" if xampl.persist_required && xampl.is_changed then unless self == xampl.persister raise MixedPersisters.new(xampl.persister, self) end @changed[xampl] = xampl # puts "!!!! change recorded ==> #{@changed.size}/#{count_changed} #{@changed.object_id} !!!!" # @changed.each{ | thing, ignore | # puts " changed: #{thing}, index: #{thing.get_the_index}, changed: #{thing.is_changed}" # } end end |
#has_not_changed(xampl) ⇒ Object
87 88 89 90 |
# File 'lib/xamplr/persister.rb', line 87 def has_not_changed(xampl) # puts "!!!! has_not_changed #{xampl} #{xampl.get_the_index} -- in @changed: #{nil != @changed[xampl]}" @changed.delete(xampl) if xampl end |
#introduce(xampl) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/xamplr/persister.rb', line 65 def introduce(xampl) if xampl.introduce_persister(self) then cache(xampl) end has_changed(xampl) if xampl.is_changed end |
#is_busy ⇒ Object
61 62 63 |
# File 'lib/xamplr/persister.rb', line 61 def is_busy return 0 < @busy_count end |
#lazy_load(target, klass, pid) ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/xamplr/persister.rb', line 202 def lazy_load(target, klass, pid) # puts "#{File.basename(__FILE__)} #{__LINE__} LAZY_LOAD:: klass: #{klass} pid: #{pid} target: #{target}" xampl = read(klass, pid, target) # puts " LAZY_LOAD --> #{xampl}" return xampl end |
#lookup(klass, pid) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/xamplr/persister.rb', line 178 def lookup(klass, pid) #raise XamplException.new(:live_across_rollback) if @rolled_back #puts "#{File.basename(__FILE__)} #{__LINE__} LOOKUP:: klass: #{klass} pid: #{pid}" begin busy(true) xampl = read(klass, pid) ensure busy(false) end #puts "#{File.basename(__FILE__)} #{__LINE__} ---> #{ xampl }" return xampl end |
#optimise(opts) ⇒ Object
41 42 |
# File 'lib/xamplr/persister.rb', line 41 def optimise(opts) end |
#print_stats ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/xamplr/persister.rb', line 318 def print_stats printf("SYNC[%s]:: TOTAL cache_hits: %d, reads: %d, writes: %d\n", self.name, @total_cache_hits, @total_read_count, @total_write_count) printf(" cache_hits: %d, reads: %d, writes: %d, time: %fms \n", @last_cache_hits, @read_count, @last_write_count, @last_sync_time) printf(" syncs: %d\n", @total_sync_count) printf(" changed count: %d (%d)\n", count_changed, @changed.size) @changed.each do |thing, ignore| if thing.is_changed then puts " changed: #{thing}, index: #{thing.get_the_index}" else puts " UNCHANGED: #{thing}, index: #{thing.get_the_index} <<<<<<<<<<<<<<<<<<< BAD!" end end end |
#put_changed(msg = "") ⇒ Object
212 213 214 215 |
# File 'lib/xamplr/persister.rb', line 212 def put_changed(msg="") puts "Changed::#{msg}:" @changed.each { | xampl, ignore | puts " #{xampl.tag} #{xampl.get_the_index}" } end |
#query_implemented ⇒ Object
162 163 164 |
# File 'lib/xamplr/persister.rb', line 162 def query_implemented false end |
#read(klass, pid, target = nil) ⇒ Object
158 159 160 |
# File 'lib/xamplr/persister.rb', line 158 def read(klass, pid, target=nil) raise XamplException.new(:unimplemented) end |
#realise(representation, target = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/xamplr/persister.rb', line 143 def realise(representation, target=nil) # Normally we'd expect to see the representation in the @format format, but # that isn't necessarily the case. Try to work out what the format might be... if representation =~ /^</ then return XamplObject.realise_from_xml_string(representation, target) else XamplObject.from_ruby(representation, target) end end |
#represent(xampl, mentions = []) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/xamplr/persister.rb', line 132 def represent(xampl, mentions=[]) #puts "REPRESENT #{xampl} load needed: #{xampl.load_needed}" # return nil if xampl.load_needed case xampl.default_persister_format || @format when nil, :xml_format then return xampl.persist("", mentions) when :ruby_format then return xampl.to_ruby(mentions) end end |
#rollback ⇒ Object
304 305 306 307 308 309 310 311 312 |
# File 'lib/xamplr/persister.rb', line 304 def rollback begin busy(true) return Xampl.rollback(self) ensure busy(false) end end |
#rollback_cleanup ⇒ Object
314 315 316 |
# File 'lib/xamplr/persister.rb', line 314 def rollback_cleanup @changed = {} end |
#shutdown ⇒ Object
48 49 50 51 |
# File 'lib/xamplr/persister.rb', line 48 def shutdown # self.sync # self.close end |
#start_sync_write ⇒ Object
217 218 219 |
# File 'lib/xamplr/persister.rb', line 217 def start_sync_write #only if needed end |
#sync ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/xamplr/persister.rb', line 235 def sync @last_sync_time = Time.now #raise XamplException.new(:live_across_rollback) if @rolled_back begin # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNC changed: #{@changed.size}" if 0 < @changed.size # @changed.each do | key, value | # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] key: #{key.class.name}, pid: #{key.get_the_index}" # end # # if 0 < @changed.size then # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNC changed: #{@changed.size}" # caller(0).each do | trace | # next if /xamplr/ =~ trace # puts " #{trace}" # break if /actionpack/ =~ trace # end # end # # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNC changed: #{@changed.size}" if 0 < @changed.size then begin # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNC changed: #{@changed.size}" busy(true) # if @syncing then # puts "\n\n\n\n#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNCING IS ALREADY TRUE!!!!!!\n\n\n" # end @syncing = true start_sync_write do_sync_write ensure done_sync_write @syncing = false end else # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] SYNC, noting changed" end @changed = {} puts "SOME NOT EXPUNGED: #{ @expunged.inspect }" unless 0 == @expunged.size @expunged = Set.new @total_read_count = @total_read_count + @read_count @total_write_count = @total_write_count + @write_count @total_cache_hits = @total_cache_hits + @cache_hits @total_sync_count = @total_sync_count + 1 @last_cache_hits = @cache_hits @last_write_count = @write_count @cache_hits = 0 @read_count = 0 @write_count = 0 self.sync_done() return @last_write_count ensure busy(false) # puts "#{ __FILE__ }:#{ __LINE__ } [#{__method__}] **** SYNCING IS FALSE" @syncing = false @last_sync_time = Time.now - @last_sync_time end end |
#sync_done ⇒ Object
300 301 302 |
# File 'lib/xamplr/persister.rb', line 300 def sync_done raise XamplException.new(:unimplemented) end |
#uncache(xampl) ⇒ Object
103 104 105 |
# File 'lib/xamplr/persister.rb', line 103 def uncache(xampl) raise XamplException.new(:unimplemented) end |
#write(xampl) ⇒ Object
154 155 156 |
# File 'lib/xamplr/persister.rb', line 154 def write(xampl) raise XamplException.new(:unimplemented) end |