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) ⇒ Object
Returns the name of the copy.
- #all ⇒ Object
- #clean ⇒ Object
-
#initialize(dir, log: Log::Quiet.new) ⇒ 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) ⇒ Object
Returns the name of the copy
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 |
# File 'lib/zold/copies.rb', line 92 def add(content, host, port, score, time = Time.now) 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? Futex.new(file, log: @log).open do FileUtils.mkdir_p(@dir) list = load target = list.find do |s| f = File.join(@dir, "#{s[:name]}#{Copies::EXT}") File.exist?(f) && IO.read(f) == content 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 } save(list) name end end |
#all ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zold/copies.rb', line 131 def all Futex.new(file, log: @log).open do load.group_by { |s| s[:name] }.map do |name, scores| { name: name, path: File.join(@dir, "#{name}#{Copies::EXT}"), 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[:score] }.reverse end end |
#clean ⇒ Object
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 80 81 82 83 |
# File 'lib/zold/copies.rb', line 55 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| file = File.join(@dir, f) wallet = Wallet.new(file) begin wallet.refurbish raise "Invalid protocol #{wallet.protocol} in #{file}" unless wallet.protocol == Zold::PROTOCOL rescue StandardError => e FileUtils.rm_rf(file) @log.debug("Copy at #{f} deleted: #{Backtrace.new(e)}") deleted += 1 end end deleted end end |
#load ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/zold/copies.rb', line 145 def load FileUtils.mkdir_p(File.dirname(file)) FileUtils.touch(file) CSV.read(file).map do |s| { name: s[0], host: s[1], port: s[2].to_i, score: s[3].to_i, time: Time.parse(s[4]) } end end |
#remove(host, port) ⇒ Object
85 86 87 88 89 |
# File 'lib/zold/copies.rb', line 85 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
47 48 49 |
# File 'lib/zold/copies.rb', line 47 def root File.(File.join(@dir, '..')) end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/zold/copies.rb', line 51 def to_s File.basename(@dir) end |