Reduce database calls when loading covers
This commit is contained in:
42
__main__.py
42
__main__.py
@@ -296,7 +296,6 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
|
|
||||||
def cull_covers(self, event=None):
|
def cull_covers(self, event=None):
|
||||||
blank_pixmap = QtGui.QPixmap()
|
blank_pixmap = QtGui.QPixmap()
|
||||||
blank_pixmap.load(':/images/blank.png')
|
|
||||||
|
|
||||||
all_indexes = set()
|
all_indexes = set()
|
||||||
for i in range(self.lib_ref.item_proxy_model.rowCount()):
|
for i in range(self.lib_ref.item_proxy_model.rowCount()):
|
||||||
@@ -321,25 +320,30 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
this_item.setIcon(QtGui.QIcon(blank_pixmap))
|
this_item.setIcon(QtGui.QIcon(blank_pixmap))
|
||||||
this_item.setData(False, QtCore.Qt.UserRole + 8)
|
this_item.setData(False, QtCore.Qt.UserRole + 8)
|
||||||
|
|
||||||
|
hash_index_dict = {}
|
||||||
|
hash_list = []
|
||||||
for i in visible_indexes:
|
for i in visible_indexes:
|
||||||
model_index = self.lib_ref.item_proxy_model.mapToSource(i)
|
model_index = self.lib_ref.item_proxy_model.mapToSource(i)
|
||||||
this_item = self.lib_ref.view_model.item(model_index.row())
|
|
||||||
|
|
||||||
if this_item:
|
book_hash = self.lib_ref.view_model.data(
|
||||||
is_cover_already_displayed = this_item.data(QtCore.Qt.UserRole + 8)
|
model_index, QtCore.Qt.UserRole + 6)
|
||||||
if is_cover_already_displayed:
|
cover_displayed = self.lib_ref.view_model.data(
|
||||||
continue
|
model_index, QtCore.Qt.UserRole + 8)
|
||||||
|
|
||||||
book_hash = this_item.data(QtCore.Qt.UserRole + 6)
|
if book_hash and not cover_displayed:
|
||||||
cover = database.DatabaseFunctions(
|
hash_list.append(book_hash)
|
||||||
self.database_path).fetch_data(
|
hash_index_dict[book_hash] = model_index
|
||||||
('CoverImage',),
|
|
||||||
'books',
|
|
||||||
{'Hash': book_hash},
|
|
||||||
'EQUALS',
|
|
||||||
True)
|
|
||||||
|
|
||||||
self.cover_loader(this_item, cover)
|
all_covers = database.DatabaseFunctions(
|
||||||
|
self.database_path).fetch_covers_only(hash_list)
|
||||||
|
|
||||||
|
for i in all_covers:
|
||||||
|
book_hash = i[0]
|
||||||
|
cover = i[1]
|
||||||
|
model_index = hash_index_dict[book_hash]
|
||||||
|
|
||||||
|
book_item = self.lib_ref.view_model.item(model_index.row())
|
||||||
|
self.cover_loader(book_item, cover)
|
||||||
|
|
||||||
def start_culling_timer(self):
|
def start_culling_timer(self):
|
||||||
if self.settings['perform_culling']:
|
if self.settings['perform_culling']:
|
||||||
@@ -1104,12 +1108,16 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
|
|
||||||
Settings(self).save_settings()
|
Settings(self).save_settings()
|
||||||
self.thread = BackGroundTabUpdate(self.database_path, all_metadata)
|
self.thread = BackGroundTabUpdate(self.database_path, all_metadata)
|
||||||
self.thread.finished.connect(QtWidgets.qApp.exit)
|
self.thread.finished.connect(self.database_care)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
Settings(self).save_settings()
|
Settings(self).save_settings()
|
||||||
QtWidgets.qApp.exit()
|
self.database_care()
|
||||||
|
|
||||||
|
def database_care(self):
|
||||||
|
database.DatabaseFunctions(self.database_path).vacuum_database()
|
||||||
|
QtWidgets.qApp.exit()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
24
database.py
24
database.py
@@ -19,7 +19,7 @@
|
|||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore, QtWidgets
|
||||||
|
|
||||||
|
|
||||||
class DatabaseInit:
|
class DatabaseInit:
|
||||||
@@ -73,7 +73,7 @@ class DatabaseFunctions:
|
|||||||
self.database.execute(sql_command, [path, path, name, tags, is_checked])
|
self.database.execute(sql_command, [path, path, name, tags, is_checked])
|
||||||
|
|
||||||
self.database.commit()
|
self.database.commit()
|
||||||
self.close_database()
|
self.database.close()
|
||||||
|
|
||||||
def add_to_database(self, data):
|
def add_to_database(self, data):
|
||||||
# data is expected to be a dictionary
|
# data is expected to be a dictionary
|
||||||
@@ -115,7 +115,7 @@ class DatabaseFunctions:
|
|||||||
path, isbn, tags, book_hash, cover_insert])
|
path, isbn, tags, book_hash, cover_insert])
|
||||||
|
|
||||||
self.database.commit()
|
self.database.commit()
|
||||||
self.close_database()
|
self.database.close()
|
||||||
|
|
||||||
def fetch_data(self, columns, table, selection_criteria, equivalence, fetch_one=False):
|
def fetch_data(self, columns, table, selection_criteria, equivalence, fetch_one=False):
|
||||||
# columns is a tuple that will be passed as a comma separated list
|
# columns is a tuple that will be passed as a comma separated list
|
||||||
@@ -150,6 +150,7 @@ class DatabaseFunctions:
|
|||||||
|
|
||||||
# book data is returned as a list of tuples
|
# book data is returned as a list of tuples
|
||||||
data = self.database.execute(sql_command_fetch).fetchall()
|
data = self.database.execute(sql_command_fetch).fetchall()
|
||||||
|
self.database.close()
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
# Because this is the result of a fetchall(), we need an
|
# Because this is the result of a fetchall(), we need an
|
||||||
@@ -164,7 +165,12 @@ class DatabaseFunctions:
|
|||||||
except (KeyError, sqlite3.OperationalError):
|
except (KeyError, sqlite3.OperationalError):
|
||||||
print('Commander, SQLite is in rebellion @ data fetching handling')
|
print('Commander, SQLite is in rebellion @ data fetching handling')
|
||||||
|
|
||||||
self.close_database()
|
def fetch_covers_only(self, hash_list):
|
||||||
|
parameter_marks = ','.join(['?' for i in hash_list])
|
||||||
|
sql_command = f"SELECT Hash, CoverImage from books WHERE Hash IN ({parameter_marks})"
|
||||||
|
data = self.database.execute(sql_command, hash_list).fetchall()
|
||||||
|
self.database.close()
|
||||||
|
return data
|
||||||
|
|
||||||
def modify_positional_data(self, positional_data):
|
def modify_positional_data(self, positional_data):
|
||||||
for i in positional_data:
|
for i in positional_data:
|
||||||
@@ -189,7 +195,7 @@ class DatabaseFunctions:
|
|||||||
return
|
return
|
||||||
|
|
||||||
self.database.commit()
|
self.database.commit()
|
||||||
self.close_database()
|
self.database.close()
|
||||||
|
|
||||||
def delete_from_database(self, column_name, target_data):
|
def delete_from_database(self, column_name, target_data):
|
||||||
# target_data is an iterable
|
# target_data is an iterable
|
||||||
@@ -202,8 +208,8 @@ class DatabaseFunctions:
|
|||||||
self.database.execute(sql_command, (i,))
|
self.database.execute(sql_command, (i,))
|
||||||
|
|
||||||
self.database.commit()
|
self.database.commit()
|
||||||
self.close_database()
|
|
||||||
|
|
||||||
def close_database(self):
|
|
||||||
self.database.execute("VACUUM")
|
|
||||||
self.database.close()
|
self.database.close()
|
||||||
|
|
||||||
|
def vacuum_database(self):
|
||||||
|
self.database.execute("VACUUM")
|
||||||
|
return True
|
||||||
|
Reference in New Issue
Block a user