mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 02:45:29 -04:00
Memoization on dir contents_signature
This commit is contained in:
parent
a8f729f5c1
commit
9309f51df6
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user