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.
-
#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
- #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
- #sync ⇒ Object
- #sync_done ⇒ Object
- #uncache(xampl) ⇒ Object
- #version(stream) ⇒ Object
- #write(xampl) ⇒ Object
Constructor Details
#initialize(name = nil, format = nil) ⇒ Persister
Returns a new instance of Persister.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/xamplr/persister.rb', line 17 def initialize(name=nil, format=nil) @name = name @format = format @automatic = false @changed = {} @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 @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.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def automatic @automatic end |
#block_changes ⇒ Object
Returns the value of attribute block_changes.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def block_changes @block_changes end |
#cache_hits ⇒ Object
Returns the value of attribute cache_hits.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def cache_hits @cache_hits end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
15 16 17 |
# File 'lib/xamplr/persister.rb', line 15 def format @format end |
#last_write_count ⇒ Object
Returns the value of attribute last_write_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def last_write_count @last_write_count end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def name @name end |
#read_count ⇒ Object
Returns the value of attribute read_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def read_count @read_count end |
#rolled_back ⇒ Object
Returns the value of attribute rolled_back.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def rolled_back @rolled_back end |
#syncing ⇒ Object (readonly)
Returns the value of attribute syncing.
15 16 17 |
# File 'lib/xamplr/persister.rb', line 15 def syncing @syncing end |
#total_cache_hits ⇒ Object
Returns the value of attribute total_cache_hits.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def total_cache_hits @total_cache_hits end |
#total_read_count ⇒ Object
Returns the value of attribute total_read_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def total_read_count @total_read_count end |
#total_rollback_count ⇒ Object
Returns the value of attribute total_rollback_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def total_rollback_count @total_rollback_count end |
#total_sync_count ⇒ Object
Returns the value of attribute total_sync_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def total_sync_count @total_sync_count end |
#total_write_count ⇒ Object
Returns the value of attribute total_write_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def total_write_count @total_write_count end |
#write_count ⇒ Object
Returns the value of attribute write_count.
6 7 8 |
# File 'lib/xamplr/persister.rb', line 6 def write_count @write_count end |
Class Method Details
.replace(old_xampl, new_xampl) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/xamplr/persister.rb', line 102 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
44 45 46 47 48 49 50 |
# File 'lib/xamplr/persister.rb', line 44 def busy(yes) if yes then @busy_count += 1 elsif 0 < @busy_count then @busy_count -= 1 end end |
#cache(xampl) ⇒ Object
90 91 92 |
# File 'lib/xamplr/persister.rb', line 90 def cache(xampl) raise XamplException.new(:unimplemented) end |
#clear_cache ⇒ Object
98 99 100 |
# File 'lib/xamplr/persister.rb', line 98 def clear_cache raise XamplException.new(:unimplemented) end |
#close ⇒ Object
40 41 42 |
# File 'lib/xamplr/persister.rb', line 40 def close self.sync end |
#count_changed ⇒ Object
83 84 85 86 87 88 |
# File 'lib/xamplr/persister.rb', line 83 def count_changed # @changed.each{ | thing, ignore | # puts "changed: #{thing}, index: #{thing.get_the_index}" # } return @changed.size end |
#do_sync_write ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/xamplr/persister.rb', line 210 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 |
#find_known(klass, pid) ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/xamplr/persister.rb', line 187 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
163 164 165 166 167 168 169 |
# File 'lib/xamplr/persister.rb', line 163 def find_xampl(hint=false) if hint then return [], "no query made" else return [] end end |
#has_changed(xampl) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/xamplr/persister.rb', line 63 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
78 79 80 81 |
# File 'lib/xamplr/persister.rb', line 78 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
56 57 58 59 60 61 |
# File 'lib/xamplr/persister.rb', line 56 def introduce(xampl) if xampl.introduce_persister(self) then cache(xampl) end has_changed(xampl) if xampl.is_changed end |
#is_busy ⇒ Object
52 53 54 |
# File 'lib/xamplr/persister.rb', line 52 def is_busy return 0 < @busy_count end |
#lazy_load(target, klass, pid) ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/xamplr/persister.rb', line 195 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/xamplr/persister.rb', line 171 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
37 38 |
# File 'lib/xamplr/persister.rb', line 37 def optimise(opts) end |
#print_stats ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/xamplr/persister.rb', line 285 def print_stats printf("SYNC:: TOTAL cache_hits: %d, reads: %d, writes: %d\n", @total_cache_hits, @total_read_count, @total_write_count) printf(" cache_hits: %d, reads: %d, last writes: %d\n", @cache_hits, @read_count, @last_write_count) 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
205 206 207 208 |
# File 'lib/xamplr/persister.rb', line 205 def put_changed(msg="") puts "Changed::#{msg}:" @changed.each { | xampl, ignore | puts " #{xampl.tag} #{xampl.get_the_index}" } end |
#query_implemented ⇒ Object
159 160 161 |
# File 'lib/xamplr/persister.rb', line 159 def query_implemented false end |
#read(klass, pid, target = nil) ⇒ Object
155 156 157 |
# File 'lib/xamplr/persister.rb', line 155 def read(klass, pid, target=nil) raise XamplException.new(:unimplemented) end |
#realise(representation, target = nil) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/xamplr/persister.rb', line 134 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
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xamplr/persister.rb', line 123 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
271 272 273 274 275 276 277 278 279 |
# File 'lib/xamplr/persister.rb', line 271 def rollback begin busy(true) return Xampl.rollback(self) ensure busy(false) end end |
#rollback_cleanup ⇒ Object
281 282 283 |
# File 'lib/xamplr/persister.rb', line 281 def rollback_cleanup @changed = {} end |
#sync ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 |
# File 'lib/xamplr/persister.rb', line 220 def sync #raise XamplException.new(:live_across_rollback) if @rolled_back begin #puts "SYNC" #puts "SYNC" #puts "SYNC changed: #{@changed.size}" if 0 < @changed.size #@changed.each do | key, value | ##puts " #{key.class.name}" ##puts "key: #{key.class.name}, value: #{value.class.name}" #puts key.to_xml #end #puts "SYNC" #puts "SYNC" #if 0 < @changed.size then #puts "SYNC changed: #{@changed.size}" ##caller(0).each do | trace | ## next if /xamplr/ =~ trace ## puts " #{trace}" ## break if /actionpack/ =~ trace ##end #end busy(true) @syncing = true do_sync_write @changed = {} @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 @read_count = 0 @last_write_count = @write_count @write_count = 0 self.sync_done() return @last_write_count ensure busy(false) @syncing = false end end |
#sync_done ⇒ Object
267 268 269 |
# File 'lib/xamplr/persister.rb', line 267 def sync_done raise XamplException.new(:unimplemented) end |
#uncache(xampl) ⇒ Object
94 95 96 |
# File 'lib/xamplr/persister.rb', line 94 def uncache(xampl) raise XamplException.new(:unimplemented) end |
#version(stream) ⇒ Object
145 146 147 148 149 |
# File 'lib/xamplr/persister.rb', line 145 def version(stream) raise XamplException.new(:unimplemented) # catch(:refuse_to_version) do # end end |
#write(xampl) ⇒ Object
151 152 153 |
# File 'lib/xamplr/persister.rb', line 151 def write(xampl) raise XamplException.new(:unimplemented) end |