Method: Spider::Model::Storage::BaseStorage#sequence_next
- Defined in:
- lib/spiderfw/model/storage/base_storage.rb
#sequence_next(name, newval = nil, increment = 1) ⇒ Fixnum
Increments a named sequence and returns the new value
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/spiderfw/model/storage/base_storage.rb', line 445 def sequence_next(name, newval=nil, increment=1) path = sequence_file_path(name) FileUtils.mkpath(File.dirname(path)) self.class.sequence_sync.lock(::Sync::EX) if newval seq = newval else seq = 0 File.open(path, 'a+') do |f| f.rewind f.flock File::LOCK_EX cur = f.gets if (cur) seq, increment_str = cur.split('|') else seq, increment_str = 0, 1 end seq = seq.to_i increment = increment_str.to_i if increment_str f.close end seq += increment end File.open(path, 'w+') do |f| f.print(seq) f.print("|#{increment}") if (increment != 1) f.flock File::LOCK_UN f.close end self.class.sequence_sync.lock(::Sync::UN) return seq end |