Resort to the expensive database lookup only if the person exists in the (#200)

* Added profiling info

* Resort to the expensive database lookup only if the person exists in the
database.

* Prevent any access to the people database unless a person must be added.

* Bulk operations where possible.

* Prepare for a new install and the table not existing.
This commit is contained in:
Damien Ruscoe 2020-02-20 00:28:47 +00:00 committed by GitHub
parent 4974d823d4
commit 756991e9ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 14 deletions

View file

@ -8,6 +8,7 @@ import logging
from . import artwork from . import artwork
from . import queries as QU from . import queries as QU
from helper import values from helper import values
from sqlite3 import IntegrityError
################################################################################################## ##################################################################################################
@ -21,6 +22,14 @@ class Kodi(object):
def __init__(self): def __init__(self):
self.artwork = artwork.Artwork(self.cursor) self.artwork = artwork.Artwork(self.cursor)
try:
self.cursor.execute(QU.get_all_people)
except:
# Failed to load the table. Has the table been created?
self._people_cache = {}
else:
self._people_cache = dict(self.cursor.fetchall())
def create_entry_path(self): def create_entry_path(self):
self.cursor.execute(QU.create_path) self.cursor.execute(QU.create_path)
@ -123,31 +132,36 @@ class Kodi(object):
self.artwork.update(person['imageurl'], person_id, art, "thumb") self.artwork.update(person['imageurl'], person_id, art, "thumb")
def add_link(link, person_id):
self.cursor.execute(QU.update_link.replace("{LinkType}", link), (person_id,) + args)
cast_order = 1 cast_order = 1
bulk_updates = {}
for person in people: for person in people:
person_id = self.get_person(person['Name']) person_id = self.get_person(person['Name'])
if person['Type'] == 'Actor': if person['Type'] == 'Actor':
sql = QU.update_actor
role = person.get('Role') role = person.get('Role')
self.cursor.execute(QU.update_actor, (person_id,) + args + (role, cast_order,)) bulk_updates.setdefault(sql, []).append((person_id,) + args + (role, cast_order,))
cast_order += 1 cast_order += 1
elif person['Type'] == 'Director': elif person['Type'] == 'Director':
add_link('director_link', person_id) sql = QU.update_link.replace("{LinkType}", 'director_link')
bulk_updates.setdefault(sql, []).append((person_id,) + args)
elif person['Type'] == 'Writer': elif person['Type'] == 'Writer':
add_link('writer_link', person_id) sql = QU.update_link.replace("{LinkType}", 'writer_link')
bulk_updates.setdefault(sql, []).append((person_id,) + args)
elif person['Type'] == 'Artist': elif person['Type'] == 'Artist':
add_link('actor_link', person_id) sql = QU.update_link.replace("{LinkType}", 'actor_link')
bulk_updates.setdefault(sql, []).append((person_id,) + args)
add_thumbnail(person_id, person, person['Type']) add_thumbnail(person_id, person, person['Type'])
for sql, parameters in bulk_updates.items():
self.cursor.executemany(sql, parameters)
def add_person(self, *args): def add_person(self, *args):
person_id = self.create_entry_person() person_id = self.create_entry_person()
@ -155,14 +169,22 @@ class Kodi(object):
return person_id return person_id
def get_person(self, *args): def _get_person(self, *args):
try: try:
self.cursor.execute(QU.get_person, args)
return self.cursor.fetchone()[0]
except TypeError:
return self.add_person(*args) 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 get_person(self, *args):
try:
return self._people_cache[args]
except KeyError:
person_id = self._get_person(*args)
self._people_cache[args] = person_id
return person_id
def add_genres(self, genres, *args): def add_genres(self, genres, *args):

View file

@ -88,6 +88,10 @@ SELECT strFilename
FROM files FROM files
WHERE idFile = ? WHERE idFile = ?
""" """
get_all_people = """
SELECT name, actor_id
FROM actor
"""
get_person = """ get_person = """
SELECT actor_id SELECT actor_id
FROM actor FROM actor