Class: Zold::Copies

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/copies.rb

Overview

All copies

Instance Method Summary collapse

Constructor Details

#initialize(dir, log: Log::Quiet.new) ⇒ Copies

Returns a new instance of Copies.



34
35
36
37
38
39
40
# File 'lib/zold/copies.rb', line 34

def initialize(dir, log: Log::Quiet.new)
  raise 'Dir can\'t be nil' if dir.nil?
  @dir = dir
  raise 'Log can\'t be nil' if log.nil?
  @log = log
  @mutex = Mutex.new
end

Instance Method Details

#add(content, host, port, score, time = Time.now) ⇒ Object

Returns the name of the copy



75
76
77
78
79
80
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
# File 'lib/zold/copies.rb', line 75

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 < 0
  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 < 0
  @mutex.synchronize do
    FileUtils.mkdir_p(@dir)
    list = load
    target = list.find do |s|
      f = File.join(@dir, "#{s[:name]}#{Wallet::EXTENSION}")
      File.exist?(f) && AtomicFile.new(f).read == content
    end
    if target.nil?
      max = Dir.new(@dir)
        .select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }
        .map(&:to_i)
        .max
      max = 0 if max.nil?
      name = (max + 1).to_s
      AtomicFile.new(File.join(@dir, "#{name}#{Wallet::EXTENSION}")).write(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

#allObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/zold/copies.rb', line 114

def all
  @mutex.synchronize do
    load.group_by { |s| s[:name] }.map do |name, scores|
      {
        name: name,
        path: File.join(@dir, "#{name}#{Wallet::EXTENSION}"),
        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

#cleanObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zold/copies.rb', line 50

def clean
  @mutex.synchronize do
    list = load
    list.reject! { |s| s[:time] < Time.now - 24 * 60 * 60 }
    save(list)
    deleted = 0
    Dir.new(@dir).select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }.each do |f|
      next unless list.find { |s| s[:name] == File.basename(f, Wallet::EXTENSION) }.nil?
      file = File.join(@dir, f)
      size = File.size(file)
      File.delete(file)
      @log.debug("Copy ##{f} deleted: #{size}b")
      deleted += 1
    end
    deleted
  end
end

#remove(host, port) ⇒ Object



68
69
70
71
72
# File 'lib/zold/copies.rb', line 68

def remove(host, port)
  @mutex.synchronize do
    save(load.reject { |s| s[:host] == host && s[:port] == port })
  end
end

#rootObject



42
43
44
# File 'lib/zold/copies.rb', line 42

def root
  File.join(@dir, '..')
end

#to_sObject



46
47
48
# File 'lib/zold/copies.rb', line 46

def to_s
  File.basename(@dir)
end