mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 02:45:29 -04:00
Define ExamineContext, apply it when scanning
This commit is contained in:
parent
be47f309b0
commit
523195d649
@ -142,20 +142,28 @@ class Library
|
|||||||
|
|
||||||
storage = Storage.new auto_close: false
|
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_ids.select! do |title_id|
|
||||||
title = @title_hash[title_id]
|
title = @title_hash[title_id]
|
||||||
existence = title.examine
|
existence = title.examine examine_context
|
||||||
@title_hash.delete title_id unless existence
|
@title_hash.delete title_id unless existence
|
||||||
existence
|
existence
|
||||||
end
|
end
|
||||||
remained_title_dirs = @title_ids.map { |id| title_hash[id].dir }
|
remained_title_dirs = @title_ids.map { |id| title_hash[id].dir }
|
||||||
|
|
||||||
|
cache = examine_context["cached_contents_signature"]
|
||||||
(Dir.entries @dir)
|
(Dir.entries @dir)
|
||||||
.select { |fn| !fn.starts_with? "." }
|
.select { |fn| !fn.starts_with? "." }
|
||||||
.map { |fn| File.join @dir, fn }
|
.map { |fn| File.join @dir, fn }
|
||||||
.select { |path| !(remained_title_dirs.includes? path) }
|
.select { |path| !(remained_title_dirs.includes? path) }
|
||||||
.select { |path| File.directory? 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?) }
|
.select { |title| !(title.entries.empty? && title.titles.empty?) }
|
||||||
.sort! { |a, b| a.title <=> b.title }
|
.sort! { |a, b| a.title <=> b.title }
|
||||||
.each do |title|
|
.each do |title|
|
||||||
|
@ -19,7 +19,7 @@ class Title
|
|||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
@cached_cover_url : String?
|
@cached_cover_url : String?
|
||||||
|
|
||||||
def initialize(@dir : String, @parent_id)
|
def initialize(@dir : String, @parent_id, cache : Hash(String, String)?)
|
||||||
storage = Storage.default
|
storage = Storage.default
|
||||||
@signature = Dir.signature dir
|
@signature = Dir.signature dir
|
||||||
id = storage.get_title_id dir, signature
|
id = storage.get_title_id dir, signature
|
||||||
@ -32,7 +32,7 @@ class Title
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
@id = id
|
@id = id
|
||||||
@contents_signature = Dir.contents_signature dir
|
@contents_signature = Dir.contents_signature dir, cache
|
||||||
@title = File.basename dir
|
@title = File.basename dir
|
||||||
@encoded_title = URI.encode @title
|
@encoded_title = URI.encode @title
|
||||||
@title_ids = [] of String
|
@title_ids = [] of String
|
||||||
@ -70,9 +70,14 @@ class Title
|
|||||||
end
|
end
|
||||||
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
|
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
|
# Not changed. Reuse this
|
||||||
return true if @contents_signature == contents_signature
|
return true if @contents_signature == contents_signature
|
||||||
|
|
||||||
@ -95,7 +100,7 @@ class Title
|
|||||||
previous_titles_size = @title_ids.size
|
previous_titles_size = @title_ids.size
|
||||||
@title_ids.select! do |title_id|
|
@title_ids.select! do |title_id|
|
||||||
title = Library.default.get_title! 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
|
Library.default.title_hash.delete title_id unless existence
|
||||||
existence
|
existence
|
||||||
end
|
end
|
||||||
@ -115,7 +120,7 @@ class Title
|
|||||||
path = File.join dir, fn
|
path = File.join dir, fn
|
||||||
if File.directory? path
|
if File.directory? path
|
||||||
next if remained_title_dirs.includes? 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
|
next if title.entries.size == 0 && title.titles.size == 0
|
||||||
Library.default.title_hash[title.id] = title
|
Library.default.title_hash[title.id] = title
|
||||||
@title_ids << title.id
|
@title_ids << title.id
|
||||||
|
@ -133,3 +133,10 @@ class TitleInfo
|
|||||||
LRUCache.set generate_cache_entry key, self.to_json
|
LRUCache.set generate_cache_entry key, self.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias ExamineContext = NamedTuple(
|
||||||
|
file_count: Int32,
|
||||||
|
cached_contents_signature: Hash(String, String),
|
||||||
|
deleted_title_ids: Array(String),
|
||||||
|
deleted_entry_ids: Array(String)
|
||||||
|
)
|
||||||
|
@ -55,7 +55,7 @@ class Dir
|
|||||||
# - When a file added, moved, removed, renamed (including which in nested
|
# - When a file added, moved, removed, renamed (including which in nested
|
||||||
# directories)
|
# directories)
|
||||||
def self.contents_signature(dirname, cache = {} of String => String) : String
|
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
|
signatures = [] of String
|
||||||
self.open dirname do |dir|
|
self.open dirname do |dir|
|
||||||
dir.entries.sort.each do |fn|
|
dir.entries.sort.each do |fn|
|
||||||
@ -71,7 +71,7 @@ class Dir
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
hash = Digest::SHA1.hexdigest(signatures.join)
|
hash = Digest::SHA1.hexdigest(signatures.join)
|
||||||
cache[dirname] = hash
|
cache[dirname] = hash unless cache.nil?
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user