Class: Zold::Copies
- Inherits:
-
Object
- Object
- Zold::Copies
- Defined in:
- lib/zold/copies.rb
Overview
All copies
Constant Summary collapse
- EXT =
Extension for copy files
'.zc'
Instance Method Summary collapse
-
#add(content, host, port, score, time: Time.now, master: false) ⇒ Object
Returns the name of the copy.
- #all(masters_first: true) ⇒ Object
-
#clean(max: 24 * 60 * 60) ⇒ Object
Delete all copies that are older than the “max” age provided, in seconds.
-
#initialize(dir, log: Loog::NULL) ⇒ Copies
constructor
A new instance of Copies.
- #load ⇒ Object
- #remove(host, port) ⇒ Object
- #root ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(dir, log: Loog::NULL) ⇒ Copies
Returns a new instance of Copies.
26 27 28 29 |
# File 'lib/zold/copies.rb', line 26 def initialize(dir, log: Loog::NULL) @dir = dir @log = log end |
Instance Method Details
#add(content, host, port, score, time: Time.now, master: false) ⇒ Object
Returns the name of the copy
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 |
# File 'lib/zold/copies.rb', line 87 def add(content, host, port, score, time: Time.now, master: false) raise "Content can't be empty" if content.empty? raise 'TCP port must be of type Integer' unless port.is_a?(Integer) raise "TCP port can't be negative: #{port}" if port.negative? raise 'Time must be of type Time' unless time.is_a?(Time) raise "Time must be in the past: #{time}" if time > Time.now raise 'Score must be Integer' unless score.is_a?(Integer) raise "Score can't be negative: #{score}" if score.negative? FileUtils.mkdir_p(@dir) Futex.new(file, log: @log).open do list = load target = list.find do |s| f = File.join(@dir, "#{s[:name]}#{Copies::EXT}") digest = OpenSSL::Digest::SHA256.new(content).hexdigest File.exist?(f) && OpenSSL::Digest::SHA256.file(f).hexdigest == digest end if target.nil? max = DirItems.new(@dir).fetch .select { |f| File.basename(f, Copies::EXT) =~ /^[0-9]+$/ } .map(&:to_i) .max max = 0 if max.nil? name = (max + 1).to_s File.write(File.join(@dir, "#{name}#{Copies::EXT}"), content) else name = target[:name] end list.reject! { |s| s[:host] == host && s[:port] == port } list << { name: name, host: host, port: port, score: score, time: time, master: master } save(list) name end end |
#all(masters_first: true) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/zold/copies.rb', line 128 def all(masters_first: true) Futex.new(file, log: @log).open(false) do list = load.group_by { |s| s[:name] }.map do |name, scores| { name: name, path: File.join(@dir, "#{name}#{Copies::EXT}"), total: scores.count, master: scores.any? { |s| s[:master] }, score: scores.select { |s| s[:time] > Time.now - (24 * 60 * 60) }.sum { |s| s[:score] } || 0 } end.select { |c| File.exist?(c[:path]) } if masters_first list.sort_by! { |c| [c[:master] ? 1 : 0, c[:score].to_s.rjust(5, '0')] } else list.sort_by! { |c| c[:score] } end list.reverse end end |
#clean(max: 24 * 60 * 60) ⇒ Object
Delete all copies that are older than the “max” age provided, in seconds.
40 41 42 43 44 45 46 47 48 49 50 51 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 |
# File 'lib/zold/copies.rb', line 40 def clean(max: 24 * 60 * 60) Futex.new(file, log: @log).open do list = load list.reject! do |s| if s[:time] >= Time.now - max false else @log.debug("Copy ##{s[:name]}/#{s[:host]}:#{s[:port]} is too old, over #{Age.new(s[:time])}") true end end save(list) deleted = 0 files.each do |f| next unless list.find { |s| s[:name] == File.basename(f, Copies::EXT) }.nil? file = File.join(@dir, f) size = File.size(file) File.delete(file) @log.debug("Copy at #{f} deleted: #{Size.new(size)}") deleted += 1 end list.select! do |s| cp = File.join(@dir, "#{s[:name]}#{Copies::EXT}") wallet = Wallet.new(cp) begin wallet.refurbish raise "Invalid protocol #{wallet.protocol} in #{cp}" unless wallet.protocol == Zold::PROTOCOL true rescue StandardError => e FileUtils.rm_rf(cp) @log.debug("Copy at #{cp} deleted: #{Backtrace.new(e)}") deleted += 1 false end end save(list) deleted end end |
#load ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/zold/copies.rb', line 148 def load FileUtils.mkdir_p(File.dirname(file)) FileUtils.touch(file) CSV.read(file).select { |s| s.count == 6 }.map do |s| { name: s[0], host: s[1], port: s[2].to_i, score: s[3].to_i, time: Txn.parse_time(s[4]), master: s[5] == 'M' } end end |
#remove(host, port) ⇒ Object
80 81 82 83 84 |
# File 'lib/zold/copies.rb', line 80 def remove(host, port) Futex.new(file, log: @log).open do save(load.reject { |s| s[:host] == host && s[:port] == port }) end end |
#root ⇒ Object
31 32 33 |
# File 'lib/zold/copies.rb', line 31 def root File.(File.join(@dir, '..')) end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/zold/copies.rb', line 35 def to_s File.basename(@dir) end |