Memoization on dir contents_signature

This commit is contained in:
Leeingnyo 2021-09-12 02:03:54 +09:00
parent a8f729f5c1
commit 9309f51df6
2 changed files with 9 additions and 5 deletions

View File

@ -70,11 +70,12 @@ class Title
end
end
def examine : Bool
def examine(cache = {} of String => String) : Bool
return false unless Dir.exists? @dir # no title, should be removed
contents_signature = Dir.contents_signature @dir
contents_signature = Dir.contents_signature @dir, cache
# not changed, preserve
return true if @contents_signature == contents_signature
puts "Contents changed in #{@dir}"
# fix title
@contents_signature = contents_signature
@ -95,7 +96,7 @@ class Title
previous_titles_size = @title_ids.size
@title_ids.select! do |title_id|
title = Library.default.get_title! title_id
title.examine
title.examine cache
end
remained_title_dirs = @title_ids.map do |id|
title = Library.default.get_title! id

View File

@ -54,7 +54,8 @@ class Dir
# Rescan conditions:
# - When a file added, moved, removed, renamed (including which in nested
# directories)
def self.contents_signature(dirname) : String
def self.contents_signature(dirname, cache = {} of String => String) : String
return cache[dirname] if cache[dirname]?
signatures = [] of String
self.open dirname do |dir|
dir.entries.sort.each do |fn|
@ -69,6 +70,8 @@ class Dir
end
end
end
Digest::SHA1.hexdigest(signatures.sort.join)
hash = Digest::SHA1.hexdigest(signatures.sort.join)
cache[dirname] = hash
hash
end
end