From f4d7128b59fd229031735fac38006ba4a86ce7f8 Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Tue, 14 Sep 2021 23:30:03 +0900 Subject: [PATCH] Mark unavailable only in candidates --- src/library/library.cr | 3 ++- src/storage.cr | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/library/library.cr b/src/library/library.cr index 8029fcb..12d662f 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -183,7 +183,8 @@ class Library ms = (Time.local - start).total_milliseconds Logger.info "Scanned #{@title_ids.size} titles in #{ms}ms" - Storage.default.mark_unavailable + Storage.default.mark_unavailable examine_context["deleted_entry_ids"], + examine_context["deleted_title_ids"] spawn do save_instance diff --git a/src/storage.cr b/src/storage.cr index 39116b9..1f0aab7 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -466,6 +466,47 @@ class Storage end end + # Limit mark targets with given arguments + def mark_unavailable(trash_ids_candidates : Array(String), trash_titles_candidates : Array(String)) + MainFiber.run do + get_db do |db| + # Detect dangling entry IDs + trash_ids = [] of String + db.query "select path, id from ids where id in " \ + "(#{trash_ids_candidates.join "," { |i| "'#{i}'" }})" do |rs| + rs.each do + path = rs.read String + fullpath = Path.new(path).expand(Config.current.library_path).to_s + trash_ids << rs.read String unless File.exists? fullpath + end + end + + unless trash_ids.empty? + Logger.debug "Marking #{trash_ids.size} entries as unavailable" + end + db.exec "update ids set unavailable = 1 where id in " \ + "(#{trash_ids.join "," { |i| "'#{i}'" }})" + + # Detect dangling title IDs + trash_titles = [] of String + db.query "select path, id from titles where id in " \ + "(#{trash_titles_candidates.join "," { |i| "'#{i}'" }})" do |rs| + rs.each do + path = rs.read String + fullpath = Path.new(path).expand(Config.current.library_path).to_s + trash_titles << rs.read String unless Dir.exists? fullpath + end + end + + unless trash_titles.empty? + Logger.debug "Marking #{trash_titles.size} titles as unavailable" + end + db.exec "update titles set unavailable = 1 where id in " \ + "(#{trash_titles.join "," { |i| "'#{i}'" }})" + end + end + end + private def get_missing(tablename) ary = [] of IDTuple MainFiber.run do