forked from logstash-plugins/logstash-input-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatched_files_collection.rb
89 lines (75 loc) · 1.77 KB
/
watched_files_collection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
79
80
81
82
83
84
85
86
87
88
89
# encoding: utf-8
module FileWatch
class WatchedFilesCollection
def initialize(settings)
@sort_by = settings.file_sort_by # "last_modified" | "path"
@sort_direction = settings.file_sort_direction # "asc" | "desc"
@sort_method = method("#{@sort_by}_#{@sort_direction}".to_sym)
@files = Concurrent::Array.new
@pointers = Concurrent::Hash.new
end
def add(watched_file)
@files << watched_file
@sort_method.call
end
def remove_paths(paths)
removed_files = Array(paths).map do |path|
index = @pointers.delete(path)
if index
watched_file = @files.delete_at(index)
refresh_pointers
watched_file
end
end
@sort_method.call
removed_files
end
def close_all
@files.each(&:file_close)
end
def empty?
@files.empty?
end
def keys
@pointers.keys
end
def values
@files
end
def watched_file_by_path(path)
index = @pointers[path]
return nil unless index
@files[index]
end
private
def last_modified_asc
@files.sort! do |left, right|
left.modified_at <=> right.modified_at
end
refresh_pointers
end
def last_modified_desc
@files.sort! do |left, right|
right.modified_at <=> left.modified_at
end
refresh_pointers
end
def path_asc
@files.sort! do |left, right|
left.path <=> right.path
end
refresh_pointers
end
def path_desc
@files.sort! do |left, right|
right.path <=> left.path
end
refresh_pointers
end
def refresh_pointers
@files.each_with_index do |watched_file, index|
@pointers[watched_file.path] = index
end
end
end
end