Module: Persist
- Defined in:
- lib/rbbt/persist.rb,
lib/rbbt/persist/tsv.rb,
lib/rbbt/persist/tsv/cdb.rb,
lib/rbbt/persist/tsv/lmdb.rb,
lib/rbbt/persist/tsv/adapter.rb,
lib/rbbt/persist/tsv/leveldb.rb,
lib/rbbt/persist/tsv/sharder.rb,
lib/rbbt/persist/tsv/kyotocabinet.rb,
lib/rbbt/persist/tsv/packed_index.rb,
lib/rbbt/persist/tsv/tokyocabinet.rb,
lib/rbbt/persist/tsv/fix_width_table.rb
Defined Under Namespace
Modules: CDBAdapter, FWTAdapter, KCAdapter, LMDBAdapter, LevelDBAdapter, PKIAdapter, SharderAdapter, TCAdapter, TSVAdapter Classes: Sharder
Constant Summary collapse
- MEMORY =
{}
- MAX_FILE_LENGTH =
150
- TRUE_STRINGS =
Set.new ["true", "True", "TRUE", "t", "T", "1", "yes", "Yes", "YES", "y", "Y", "ON", "on"]
- CONNECTIONS =
{}
Class Attribute Summary collapse
-
.cachedir ⇒ Object
Returns the value of attribute cachedir.
-
.lock_dir ⇒ Object
Returns the value of attribute lock_dir.
Class Method Summary collapse
- .get_filename(source) ⇒ Object
- .get_result(path, type, persist_options, lockfile, &block) ⇒ Object
- .is_persisted?(path, persist_options = {}) ⇒ Boolean
- .load_file(path, type) ⇒ Object
- .memory(name, options = {}, &block) ⇒ Object
- .newer?(path, file) ⇒ Boolean
- .open_cdb(path, write, serializer = nil) ⇒ Object
- .open_database(path, write, serializer = nil, type = "HDB", options = {}) ⇒ Object
- .open_fwt(path, value_size, range = false, serializer = nil, update = false, in_memory = false, &pos_function) ⇒ Object
- .open_kyotocabinet(path, write, serializer = nil, kyotocabinet_class = 'kch') ⇒ Object
- .open_leveldb(path, write, serializer = nil) ⇒ Object
- .open_lmdb(path, write, serializer = nil) ⇒ Object
- .open_pki(path, write, pattern, &pos_function) ⇒ Object
- .open_sharder(path, write, serializer = nil, type = TokyoCabinet::HDB, options, &shard_function) ⇒ Object
- .open_tokyocabinet(path, write, serializer = nil, tokyocabinet_class = TokyoCabinet::HDB) ⇒ Object
- .persist(name, type = nil, persist_options = {}, &block) ⇒ Object
- .persist_file(path, type, persist_options, &block) ⇒ Object
- .persist_tsv(source, filename, options = {}, persist_options = {}, &block) ⇒ Object
- .persistence_path(file, persist_options = {}, options = {}) ⇒ Object
- .save_file(path, type, content, lockfile = nil) ⇒ Object
- .tee_stream_thread(stream, path, type, callback = nil, abort_callback = nil, lockfile = nil) ⇒ Object (also: tee_stream)
Class Attribute Details
.cachedir ⇒ Object
Returns the value of attribute cachedir.
10 11 12 |
# File 'lib/rbbt/persist.rb', line 10 def cachedir @cachedir end |
.lock_dir ⇒ Object
Returns the value of attribute lock_dir.
18 19 20 |
# File 'lib/rbbt/persist.rb', line 18 def lock_dir @lock_dir end |
Class Method Details
.get_filename(source) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rbbt/persist/tsv.rb', line 39 def self.get_filename(source) case when Path === source source when (source.respond_to?(:filename) and source.filename) source.filename when source.respond_to?(:cmd) "CMD-#{Misc.digest(source.cmd)}" when TSV === source "TSV[#{Misc.digest Misc.fingerprint(source)}]" end || source.object_id.to_s end |
.get_result(path, type, persist_options, lockfile, &block) ⇒ 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 |
# File 'lib/rbbt/persist.rb', line 235 def self.get_result(path, type, , lockfile, &block) res = yield path stream = res if IO === res stream = res.stream if res.respond_to? :stream if stream if [:no_load] == :stream callback = stream.respond_to?(:callback)? stream.callback : nil abort_callback = stream.respond_to?(:abort_callback)? stream.abort_callback : nil # This is to avoid calling the callbacks twice, since they have been # moved to the new 'res' stream #stream.callback = nil #stream.abort_callback = nil res = tee_stream(stream, path, type, callback, abort_callback, lockfile) #res.lockfile = lockfile raise KeepLocked.new res else stream = res.get_stream if res.respond_to? :get_stream begin Open.write(path, stream) Open.open(path) do |stream| case type when :array stream.read.split "\n" when :tsv TSV.open(stream) else stream.read end end rescue stream.abort if stream.respond_to? :abort raise $! end end else res end end |
.is_persisted?(path, persist_options = {}) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rbbt/persist.rb', line 39 def self.is_persisted?(path, = {}) return false if not Open.exists? path return false if TrueClass === [:update] check = [:check] if not check.nil? if Array === check newer = check.select{|file| newer? path, file} if newer.any? Log.medium "Persistence check for #{path} failed in: #{ Misc.fingerprint(newer)}" return false end else return false if newer? path, check end end return true end |
.load_file(path, type) ⇒ Object
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rbbt/persist.rb', line 94 def self.load_file(path, type) begin case (type || :marshal).to_sym when :nil nil when :boolean TRUE_STRINGS.include? Open.read(path).chomp.strip when :annotations Annotated.load_tsv TSV.open(path) when :tsv TSV.open(path) when :marshal_tsv TSV.setup(Marshal.load(Open.open(path))) when :fwt FixWidthTable.get(path) when :string, :text Open.read(path) when :binary f = File.open(path, 'rb') res = f.read f.close res.force_encoding("ASCII-8BIT") if res.respond_to? :force_encoding res when :array res = Open.read(path).split("\n", -1) res.pop if res.last and res.last.empty? res when :marshal Open.open(path) do |stream| content = stream.read.unpack("m").first Marshal.load(content) end when :yaml Open.open(path) do |stream| YAML.load(stream) end when :float Open.read(path).to_f when :integer Open.read(path).to_i else raise "Unknown persistence: #{ type }" end rescue Log.medium "Exception loading #{ type } #{ path }: #{$!.}" raise $! end end |
.memory(name, options = {}, &block) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/rbbt/persist.rb', line 457 def self.memory(name, = {}, &block) case when nil persist name, :memory, :file => name, &block when String persist name, :memory, :file => name + "_" << , &block else file = name repo = .delete :repo if and .any? file << "_" << ([:key] ? [:key] : Misc.hash2md5()) if and .any? persist name, :memory, {:repo => repo, :persist => true, :file => file}.merge(), &block end end |
.newer?(path, file) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rbbt/persist.rb', line 28 def self.newer?(path, file) return true if not Open.exists? file path = path.find if Path === path file = file.find if Path === file patht = File.mtime(path) filet = File.mtime(file) diff = patht - filet return diff if diff < 0 return false end |
.open_cdb(path, write, serializer = nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rbbt/persist/tsv/cdb.rb', line 128 def self.open_cdb(path, write, serializer = nil) write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::CDBAdapter.open(path, write) #unless serializer == :clean # TSV.setup database # database.serializer = serializer if serializer #end database end |
.open_database(path, write, serializer = nil, type = "HDB", options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rbbt/persist/tsv.rb', line 52 def self.open_database(path, write, serializer = nil, type = "HDB", = {}) case type when "LevelDB" Persist.open_leveldb(path, write, serializer) when "CDB" Persist.open_cdb(path, write, serializer) when "LMDB" Persist.open_lmdb(path, write, serializer) when 'kch', 'kct' Persist.open_kyotocabinet(path, write, serializer, type) when 'fwt' value_size, range, update, in_memory, pos_function = Misc. .dup, :value_size, :range, :update, :in_memory, :pos_function if pos_function Persist.open_fwt(path, value_size, range, serializer, update, in_memory, &pos_function) else Persist.open_fwt(path, value_size, range, serializer, update, in_memory) end when 'pki' pattern, pos_function = Misc. .dup, :pattern, :pos_function if pos_function Persist.open_pki(path, write, pattern, &pos_function) else Persist.open_pki(path, write, pattern) end else Persist.open_tokyocabinet(path, write, serializer, type) end end |
.open_fwt(path, value_size, range = false, serializer = nil, update = false, in_memory = false, &pos_function) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbbt/persist/tsv/fix_width_table.rb', line 98 def self.open_fwt(path, value_size, range = false, serializer = nil, update = false, in_memory = false, &pos_function) FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::FWTAdapter.open(path, value_size, range, update, in_memory, &pos_function) unless serializer == :clean TSV.setup database database.serializer = serializer || database.serializer end database end |
.open_kyotocabinet(path, write, serializer = nil, kyotocabinet_class = 'kch') ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 80 def self.open_kyotocabinet(path, write, serializer = nil, kyotocabinet_class= 'kch') write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::KCAdapter.open(path, write, kyotocabinet_class) unless serializer == :clean TSV.setup database database.serializer = serializer || database.serializer end database end |
.open_leveldb(path, write, serializer = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rbbt/persist/tsv/leveldb.rb', line 106 def self.open_leveldb(path, write, serializer = nil) write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::LevelDBAdapter.open(path, write) unless serializer == :clean TSV.setup database database.serializer = serializer || database.serializer end database end |
.open_lmdb(path, write, serializer = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rbbt/persist/tsv/lmdb.rb', line 47 def self.open_lmdb(path, write, serializer = nil) write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::LMDBAdapter.open(path, write) unless serializer == :clean TSV.setup database database.serializer = serializer || database.serializer end database end |
.open_pki(path, write, pattern, &pos_function) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rbbt/persist/tsv/packed_index.rb', line 98 def self.open_pki(path, write, pattern, &pos_function) FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::PKIAdapter.open(path, write, pattern, &pos_function) #TSV.setup database #database.serializer = :clean database end |
.open_sharder(path, write, serializer = nil, type = TokyoCabinet::HDB, options, &shard_function) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/rbbt/persist/tsv/sharder.rb', line 245 def self.open_sharder(path, write, serializer = nil, type = TokyoCabinet::HDB, , &shard_function) write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::SharderAdapter.open(path, write, type, , &shard_function) if type.to_s == 'pki' TSV.setup database database.type = :list database.serializer = :clean else if serializer != :clean TSV.setup database database.serializer = serializer if serializer end end database end |
.open_tokyocabinet(path, write, serializer = nil, tokyocabinet_class = TokyoCabinet::HDB) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 76 def self.open_tokyocabinet(path, write, serializer = nil, tokyocabinet_class = TokyoCabinet::HDB) write = true unless File.exist? path FileUtils.mkdir_p File.dirname(path) unless File.exist?(File.dirname(path)) database = Persist::TCAdapter.open(path, write, serializer, tokyocabinet_class) unless serializer == :clean TSV.setup database database.serializer = serializer || database.serializer end database end |
.persist(name, type = nil, persist_options = {}, &block) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/rbbt/persist.rb', line 341 def self.persist(name, type = nil, = {}, &block) type ||= :marshal ||= {} if type ==:memory and [:file] and [:persist] and [:persist] != :update and not [:update] return ([:repo] || Persist::MEMORY)[[:file]] ||= yield end if FalseClass === [:persist] yield else [:update] ||= true if [:persist].to_s == "update" = Misc. , :other path = persistence_path(name, , || {}) if ENV["RBBT_UPDATE_TSV_PERSIST"] == 'true' and name and Open.exists?(name) [:check] ||= [] [:check] << name else = {} end case when type.to_sym == :memory repo = [:repo] || Persist::MEMORY path = path.find if Path === path repo[path] = yield when (type.to_sym == :annotations and .include? :annotation_repo) repo = [:annotation_repo] keys = nil subkey = name + ":" if String === repo repo = Persist.open_tokyocabinet(repo, false, :list, "BDB") repo.read_and_close do keys = repo.range subkey + 0.chr, true, subkey + 254.chr, true end repo.close else repo.read_and_close do keys = repo.range subkey + 0.chr, true, subkey + 254.chr, true end end case when (keys.length == 1 and keys.first == subkey + 'NIL') nil when (keys.length == 1 and keys.first == subkey + 'EMPTY') [] when (keys.length == 1 and keys.first =~ /:SINGLE$/) key = keys.first values = repo.read_and_close do repo[key] end Annotated.load_tsv_values(key, values, "literal", "annotation_types", "JSON") when (keys.any? and not keys.first =~ /ANNOTATED_DOUBLE_ARRAY/) repo.read_and_close do keys.sort_by{|k| k.split(":").last.to_i}.collect{|key| v = repo[key] Annotated.load_tsv_values(key, v, "literal", "annotation_types", "JSON") } end when (keys.any? and keys.first =~ /ANNOTATED_DOUBLE_ARRAY/) repo.read_and_close do res = keys.sort_by{|k| k.split(":").last.to_i}.collect{|key| v = repo[key] Annotated.load_tsv_values(key, v, "literal", "annotation_types", "JSON") } res.first.annotate res res.extend AnnotatedArray res end else entities = yield repo.write_and_close do case when entities.nil? repo[subkey + "NIL"] = nil when entities.empty? repo[subkey + "EMPTY"] = nil when (not Array === entities or (AnnotatedArray === entities and not Array === entities.first)) tsv_values = entities.tsv_values("literal", "annotation_types", "JSON") repo[subkey + entities.id << ":" << "SINGLE"] = tsv_values when (not Array === entities or (AnnotatedArray === entities and AnnotatedArray === entities.first)) entities.each_with_index do |e,i| next if e.nil? tsv_values = e.tsv_values("literal", "annotation_types", "JSON") repo[subkey + e.id << ":ANNOTATED_DOUBLE_ARRAY:" << i.to_s] = tsv_values end else entities.each_with_index do |e,i| next if e.nil? tsv_values = e.tsv_values("literal", "annotation_types", "JSON") repo[subkey + e.id << ":" << i.to_s] = tsv_values end end end entities end else path = path.find if Path === path persist_file(path, type, , &block) end end end |
.persist_file(path, type, persist_options, &block) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/rbbt/persist.rb', line 279 def self.persist_file(path, type, , &block) Misc.insist do begin if is_persisted?(path, ) Log.low "Persist up-to-date: #{ path } - #{Misc.fingerprint }" return path if [:no_load] return load_file(path, type) else Open.rm path if Open.exists? path end rescue Aborted, Interrupt Log.warn "Aborted loading persistence (#{ type }) #{ path }: #{$!.}. Not erasing." raise $! rescue Exception Log.warn "Exception loading persistence (#{ type }) #{ path }: #{$!.}. Erase and retry." Open.rm path if Open.exists? path raise $! end end lock_filename = Persist.persistence_path(path + '.persist', {:dir => Persist.lock_dir}) begin = Misc.pull_keys , :lock = [:lock] if Hash === [:lock] Misc.lock lock_filename, do |lockfile| Misc.insist do if is_persisted?(path, ) Log.low "Persist up-to-date (suddenly): #{ path } - #{Misc.fingerprint }" lockfile.unlock if lockfile.locked? return path if [:no_load] return load_file(path, type) end end Log.medium "Persist create: #{ path } - #{type} #{Misc.fingerprint }" res = get_result(path, type, , lockfile, &block) save_file(path, type, res, lockfile) return path if [:no_load] res end rescue Lockfile::StolenLockError Log.medium "Lockfile stolen: #{path} - #{lock_filename}" Log.exception $! sleep 1 + rand(2) retry rescue Exception Log.medium "Error in persist: #{path}#{Open.exists?(path) ? Log.color(:red, " Erasing") : ""}" begin Open.rm path rescue end if Open.exists? path raise $! end end |
.persist_tsv(source, filename, options = {}, persist_options = {}, &block) ⇒ Object
81 82 83 84 85 86 87 88 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 124 125 126 127 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 159 160 161 162 163 164 165 166 167 |
# File 'lib/rbbt/persist/tsv.rb', line 81 def self.persist_tsv(source, filename, = {}, = {}, &block) [:prefix] ||= "TSV" if data = [:data] Log.debug "TSV persistence creating with data: #{ Misc.fingerprint(data) }" yield data return data end filename ||= get_filename(source) if not [:persist] data = {} yield(data) return data end path = persistence_path(filename, , ) if ENV["RBBT_UPDATE_TSV_PERSIST"] == 'true' and filename = {:check => [filename]} else = {} end if is_persisted?(path, ) and not [:update] path = path.find if Path === path Log.debug "TSV persistence up-to-date: #{ path }" if [:shard_function] return open_sharder(path, false, nil, [:engine], , &[:shard_function]) else return open_database(path, false, nil, [:engine] || TokyoCabinet::HDB, ) end end lock_filename = Persist.persistence_path(path, {:dir => TSV.lock_dir}) Misc.lock lock_filename do begin if is_persisted?(path, ) and not [:update] path = path.find if Path === path Log.debug "TSV persistence (suddenly) up-to-date: #{ path }" if [:shard_function] return open_sharder(path, false, nil, [:engine], , &[:shard_function]) else return open_database(path, false, nil, [:engine] || TokyoCabinet::HDB, ) end end path = path.find if Path === path FileUtils.rm_rf path if File.exist? path Log.medium "TSV persistence creating: #{ path }" tmp_path = path + '.persist' data = if [:shard_function] open_sharder(tmp_path, true, [:serializer], [:engine], , &[:shard_function]) else open_database(tmp_path, true, [:serializer], [:engine] || TokyoCabinet::HDB, ) end if TSV === data and data.serializer.nil? data.serializer = :type end data.write_and_read do yield data end FileUtils.mv data.persistence_path, path if File.exist? data.persistence_path and not File.exist? path tsv = CONNECTIONS[path] = CONNECTIONS.delete tmp_path tsv.persistence_path = path tsv.fix_io if tsv.respond_to? :fix_io data rescue Exception Log.error "Captured error during persist_tsv. Erasing: #{path}" FileUtils.rm_rf tmp_path if tmp_path and File.exist? tmp_path FileUtils.rm_rf path if path and File.exist? path raise $! end end end |
.persistence_path(file, persist_options = {}, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rbbt/persist.rb', line 59 def self.persistence_path(file, = {}, = {}) persistence_file = Misc. , :file return persistence_file unless persistence_file.nil? prefix = Misc. , :prefix if prefix.nil? perfile = file.to_s.gsub(/\//, '>') else perfile = prefix.to_s + ":" + file.to_s.gsub(/\//, '>') end if .include? :filters [:filters].each do |match,value| perfile = perfile + "&F[#{match}=#{Misc.digest(value.inspect)}]" end end persistence_dir = Misc.(, :dir) || Persist.cachedir Path.setup(persistence_dir) unless Path === persistence_dir filename = perfile.gsub(/\s/,'_').gsub(/\//,'>') = .dup .delete :unnamed .delete "unnamed" filename = filename[0..MAX_FILE_LENGTH] << Misc.digest(filename[MAX_FILE_LENGTH+1..-1]) if filename.length > MAX_FILE_LENGTH + 10 = Misc.hash2md5 filename << ":" << unless .empty? persistence_dir[filename] end |
.save_file(path, type, content, lockfile = nil) ⇒ Object
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rbbt/persist.rb', line 143 def self.save_file(path, type, content, lockfile = nil) if content.nil? lockfile.unlock if lockfile and lockfile.locked? return end case (type || :marshal).to_sym when :nil nil when :boolean Misc.sensiblewrite(path, content ? "true" : "false", :lock => lockfile) when :fwt content.file.seek 0 Misc.sensiblewrite(path, content.file.read, :lock => lockfile) when :tsv content = content.to_s if TSV === content Misc.sensiblewrite(path, content, :lock => lockfile) when :annotations Misc.sensiblewrite(path, Annotated.tsv(content, :all).to_s, :lock => lockfile) when :string, :text Misc.sensiblewrite(path, content, :lock => lockfile) when :binary content.force_encoding("ASCII-8BIT") if content.respond_to? :force_encoding f = File.open(path, 'wb') f.puts content f.close content when :array case content when Array if content.empty? Misc.sensiblewrite(path, "", :lock => lockfile) else Misc.sensiblewrite(path, content * "\n" + "\n", :lock => lockfile) end when IO Misc.sensiblewrite(path, content, :lock => lockfile) else Misc.sensiblewrite(path, content.to_s, :lock => lockfile) end when :marshal_tsv Misc.sensiblewrite(path, Marshal.dump(content.dup), :lock => lockfile) when :marshal dump = Marshal.dump(content) Misc.sensiblewrite(path, [dump].pack("m"), :lock => lockfile) when :yaml Misc.sensiblewrite(path, YAML.dump(content), :lock => lockfile) when :float, :integer, :tsv Misc.sensiblewrite(path, content.to_s, :lock => lockfile) else raise "Unknown persistence: #{ type }" end end |
.tee_stream_thread(stream, path, type, callback = nil, abort_callback = nil, lockfile = nil) ⇒ Object Also known as: tee_stream
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/rbbt/persist.rb', line 197 def self.tee_stream_thread(stream, path, type, callback = nil, abort_callback = nil, lockfile = nil) file, out = Misc.tee_stream(stream) saver_thread = Thread.new(Thread.current) do |parent| begin file.threads = [] Thread.current["name"] = "file saver: " + path save_file(path, type, file, lockfile) rescue Aborted Log.medium "Persist stream thread aborted: #{ Log.color :blue, path }" file.abort if file.respond_to? :abort raise $! rescue Exception Log.medium "Persist stream thread exception: #{ Log.color :blue, path }" file.abort if file.respond_to? :abort parent.raise $! raise $! rescue Exception Log.exception $! raise $! end end ConcurrentStream.setup(out, :threads => saver_thread, :filename => path) out.callback = callback out.abort_callback = abort_callback out.lockfile = stream.lockfile if stream.respond_to? :lockfile and stream.lockfile stream.callback = callback stream.abort_callback = abort_callback out end |