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 ⇒ Object
- #clean ⇒ Object
-
#initialize(dir, log: Log::NULL) ⇒ Copies
constructor
A new instance of Copies.
- #load ⇒ Object
- #remove(host, port) ⇒ Object
- #root ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Method Details
#add(content, host, port, score, time: Time.now, master: false) ⇒ Object
Returns the name of the copy
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 |
# File 'lib/zold/copies.rb', line 93 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 IO.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 ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/zold/copies.rb', line 134 def all Futex.new(file, log: @log).open(false) do 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 } .map { |s| s[:score] } .inject(&:+) || 0 } end.select { |c| File.exist?(c[:path]) }.sort_by { |c| [c[:master] ? 1 : 0, c[:score].to_s] }.reverse end end |
#clean ⇒ Object
56 57 58 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 |
# File 'lib/zold/copies.rb', line 56 def clean Futex.new(file, log: @log).open do list = load list.reject! { |s| s[:time] < Time.now - 24 * 60 * 60 } 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 files.each do |f| cp = File.join(@dir, f) wallet = Wallet.new(cp) begin wallet.refurbish raise "Invalid protocol #{wallet.protocol} in #{cp}" unless wallet.protocol == Zold::PROTOCOL rescue StandardError => e FileUtils.rm_rf(cp) @log.debug("Copy at #{f} deleted: #{Backtrace.new(e)}") deleted += 1 end end deleted end end |
#load ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/zold/copies.rb', line 150 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
86 87 88 89 90 |
# File 'lib/zold/copies.rb', line 86 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
48 49 50 |
# File 'lib/zold/copies.rb', line 48 def root File.(File.join(@dir, '..')) end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/zold/copies.rb', line 52 def to_s File.basename(@dir) end |