Define ExamineContext, apply it when scanning

This commit is contained in:
Leeingnyo 2021-09-14 22:37:30 +09:00
parent be47f309b0
commit 523195d649
4 changed files with 30 additions and 10 deletions

View File

@ -142,20 +142,28 @@ class Library
storage = Storage.new auto_close: false
examine_context : ExamineContext = {
file_count: 0,
cached_contents_signature: {} of String => String,
deleted_title_ids: [] of String,
deleted_entry_ids: [] of String
}
@title_ids.select! do |title_id|
title = @title_hash[title_id]
existence = title.examine
existence = title.examine examine_context
@title_hash.delete title_id unless existence
existence
end
remained_title_dirs = @title_ids.map { |id| title_hash[id].dir }
cache = examine_context["cached_contents_signature"]
(Dir.entries @dir)
.select { |fn| !fn.starts_with? "." }
.map { |fn| File.join @dir, fn }
.select { |path| !(remained_title_dirs.includes? path) }
.select { |path| File.directory? path }
.map { |path| Title.new path, "" }
.map { |path| Title.new path, "", cache }
.select { |title| !(title.entries.empty? && title.titles.empty?) }
.sort! { |a, b| a.title <=> b.title }
.each do |title|

View File

@ -19,7 +19,7 @@ class Title
@[YAML::Field(ignore: true)]
@cached_cover_url : String?
def initialize(@dir : String, @parent_id)
def initialize(@dir : String, @parent_id, cache : Hash(String, String)?)
storage = Storage.default
@signature = Dir.signature dir
id = storage.get_title_id dir, signature
@ -32,7 +32,7 @@ class Title
})
end
@id = id
@contents_signature = Dir.contents_signature dir
@contents_signature = Dir.contents_signature dir, cache
@title = File.basename dir
@encoded_title = URI.encode @title
@title_ids = [] of String
@ -70,9 +70,14 @@ class Title
end
end
def examine(cache = {} of String => String) : Bool
def self.new(dir : String, parent_id)
new dir, parent_id, nil
end
def examine(context : ExamineContext) : Bool
return false unless Dir.exists? @dir # No title, Remove this
contents_signature = Dir.contents_signature @dir, cache
contents_signature = Dir.contents_signature @dir,
context["cached_contents_signature"]
# Not changed. Reuse this
return true if @contents_signature == contents_signature
@ -95,7 +100,7 @@ class Title
previous_titles_size = @title_ids.size
@title_ids.select! do |title_id|
title = Library.default.get_title! title_id
existence = title.examine cache
existence = title.examine context
Library.default.title_hash.delete title_id unless existence
existence
end
@ -115,7 +120,7 @@ class Title
path = File.join dir, fn
if File.directory? path
next if remained_title_dirs.includes? path
title = Title.new path, @id
title = Title.new path, @id, context["cached_contents_signature"]
next if title.entries.size == 0 && title.titles.size == 0
Library.default.title_hash[title.id] = title
@title_ids << title.id

View File

@ -133,3 +133,10 @@ class TitleInfo
LRUCache.set generate_cache_entry key, self.to_json
end
end
alias ExamineContext = NamedTuple(
file_count: Int32,
cached_contents_signature: Hash(String, String),
deleted_title_ids: Array(String),
deleted_entry_ids: Array(String)
)

View File

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