From bdd9a8f7274e190e9fc0df3e7c1fa4aabb6383c8 Mon Sep 17 00:00:00 2001 From: Chuddah Date: Tue, 18 Feb 2020 00:45:16 +0000 Subject: [PATCH] Resort to the expensive database lookup only if the person exists in the database. The database is configured such that each person name within the table has to be unique. Attempting to add a person which already exists causes an IntegrityError exception to be thrown. This is a less costly operation than the cursor.execute and cursor.fetch operations. Increase performance by reverting to the expensive calls of fetching if adding a person fails. This pattern could also be applied to other tables within the database. --- jellyfin_kodi/objects/kodi/kodi.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/jellyfin_kodi/objects/kodi/kodi.py b/jellyfin_kodi/objects/kodi/kodi.py index e34d57d2..0ed7ebe2 100644 --- a/jellyfin_kodi/objects/kodi/kodi.py +++ b/jellyfin_kodi/objects/kodi/kodi.py @@ -8,6 +8,7 @@ import logging from . import artwork from . import queries as QU from helper import values +from sqlite3 import IntegrityError ################################################################################################## @@ -156,13 +157,13 @@ class Kodi(object): return person_id def get_person(self, *args): - try: - self.cursor.execute(QU.get_person, args) - - return self.cursor.fetchone()[0] - except TypeError: return self.add_person(*args) + except IntegrityError: + # The person already exists in the database + # Now we can do the expensive operation of fetching the id + self.cursor.execute(QU.get_person, args) + return self.cursor.fetchone()[0] def add_genres(self, genres, *args):