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.



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

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



74
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
# File 'lib/zold/copies.rb', line 74

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])
      File.exist?(f) && AtomicFile.new(f).read == content
    end
    if target.nil?
      max = Dir.new(@dir)
        .select { |f| f =~ /^[0-9]+$/ }
        .map(&:to_i)
        .max
      max = 0 if max.nil?
      name = (max + 1).to_s
      AtomicFile.new(File.join(@dir, name)).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



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

def all
  @mutex.synchronize do
    load.group_by { |s| s[:name] }.map do |name, scores|
      {
        name: name,
        path: File.join(@dir, name),
        score: scores.select { |s| s[:time] > Time.now - 24 * 60 * 60 }
          .map { |s| s[:score] }
          .inject(&:+) || 0
      }
    end.select { |c| File.exist?(c[:path]) }
  end
end

#cleanObject



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

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| f =~ /^[0-9]+$/ }.each do |f|
      next unless list.find { |s| s[:name] == f }.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



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

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

#rootObject



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

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

#to_sObject



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

def to_s
  File.basename(@dir)
end