I am writing a Windows service in ruby using Win32-utils gems. The service is currently running, but most of its function requires it to know when the file was modified. I am currently doing this with a large hash containing data about each file, which is great for relatively small directories, but when it is used in a folder containing files ~ 50,000, it is a lot of use and takes a lot of time to check for updates.
The code is as follows:
First run (hash setting):
Find.find(@local_base) do |path|
if not FileTest.directory?(path)
f = open(path)
f.rewind
@files[path.gsub(@local_base, "")] = DataFile.new(@local_base,
path.gsub(@local_base, ""),
Digest::MD5.hexdigest(f.read.gsub("\n", "\r\n")),
f.mtime.to_i,
@last_checked)
end
end
Subsequent runs (check for updates):
def check_for_updates
tempHash = Hash.new
Find.find(@local_base) do |path|
if not FileTest.directory?(path)
File.open(path) do |f|
if not @files[path.gsub(@local_base, "")].nil?
if f.mtime.to_i > @last_checked
if @files[path.gsub(@local_base, "")].modified?
@files[path.gsub(@local_base, "")].update
end
end
else
f.rewind
tempHash[f.path] = DataFile.new(@local_base,
path.gsub(@local_base, ""),
Digest::MD5.hexdigest(f.read.gsub("\n", "\r\n")),
f.mtime.to_i,
@last_scan)
end
end
end
end
if not tempHash.empty?
tempHash.each do |k, v|
@files[k] = v
end
end
tempHash = nil
update_last_checked
end
Is there a faster / more efficient way to notify my program of changed files, even better if I can do this without recursively searching the entire directory.