Refactor mark_unavailable

This commit is contained in:
Leeingnyo 2021-09-15 16:54:55 +09:00
parent d13cfc045f
commit de193906a2

View File

@ -429,54 +429,24 @@ class Storage
end end
def mark_unavailable def mark_unavailable
MainFiber.run do mark_unavailable nil, nil
get_db do |db|
# Detect dangling entry IDs
trash_ids = [] of String
db.query "select path, id from ids where unavailable = 0" 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 unavailable = 0" 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 end
# Limit mark targets with given arguments # Limit mark targets with given arguments
# They should be checked again if they are really gone, # They should be checked again if they are really gone,
# since they would be available which are renamed or moved # since they would be available which are renamed or moved
def mark_unavailable(ids_candidates : Array(String), def mark_unavailable(ids_candidates : Array(String) | Nil,
titles_candidates : Array(String)) titles_candidates : Array(String) | Nil)
MainFiber.run do MainFiber.run do
get_db do |db| get_db do |db|
# Detect dangling entry IDs # Detect dangling entry IDs
trash_ids = [] of String trash_ids = [] of String
db.query "select path, id from ids where id in " \ # Use query builder instead?
"(#{ids_candidates.join "," { |i| "'#{i}'" }})" do |rs| query = "select path, id from ids where unavailable = 0"
unless ids_candidates.nil?
query += " and id in (#{ids_candidates.join "," { |i| "'#{i}'" }})"
end
db.query query do |rs|
rs.each do rs.each do
path = rs.read String path = rs.read String
fullpath = Path.new(path).expand(Config.current.library_path).to_s fullpath = Path.new(path).expand(Config.current.library_path).to_s
@ -492,8 +462,11 @@ class Storage
# Detect dangling title IDs # Detect dangling title IDs
trash_titles = [] of String trash_titles = [] of String
db.query "select path, id from titles where id in " \ query = "select path, id from titles where unavailable = 0"
"(#{titles_candidates.join "," { |i| "'#{i}'" }})" do |rs| unless titles_candidates.nil?
query += " and id in (#{titles_candidates.join "," { |i| "'#{i}'" }})"
end
db.query query do |rs|
rs.each do rs.each do
path = rs.read String path = rs.read String
fullpath = Path.new(path).expand(Config.current.library_path).to_s fullpath = Path.new(path).expand(Config.current.library_path).to_s