diff --git a/dejavu/control.py b/dejavu/control.py index 5d5b71f..606e366 100644 --- a/dejavu/control.py +++ b/dejavu/control.py @@ -1,5 +1,5 @@ from dejavu.database import SQLDatabase -from dejavu.converter import Converter +from dejavu.convert import Converter from dejavu.fingerprint import Fingerprinter from scipy.io import wavfile from multiprocessing import Process @@ -62,8 +62,9 @@ class Dejavu(): p.join() # delete orphans - print "Done fingerprinting. Deleting orphaned fingerprints..." - self.fingerprinter.db.delete_orphans() + # print "Done fingerprinting. Deleting orphaned fingerprints..." + # TODO: need a more performant query in database.py for the + #self.fingerprinter.db.delete_orphans() def fingerprint_worker(self, files, sql_connection, output): diff --git a/dejavu/database.py b/dejavu/database.py index 9614a5f..80a40c5 100644 --- a/dejavu/database.py +++ b/dejavu/database.py @@ -102,7 +102,11 @@ class SQLDatabase(): # delete DELETE_UNFINGERPRINTED = "DELETE FROM %s WHERE %s = 0;" % (SONGS_TABLENAME, FIELD_FINGERPRINTED) - DELETE_ORPHANS = "" + DELETE_ORPHANS = """ + delete from fingerprints + where not exists ( + select * from songs where fingerprints.song_id = songs.song_id + )""" def __init__(self, hostname, username, password, database): # connect @@ -154,13 +158,14 @@ class SQLDatabase(): def delete_orphans(self): try: self.cursor = self.connection.cursor() - self.cursor.execute(SQLDatabase.DELETE_ORPHANS) - self.connection.commit() + ### TODO: SQLDatabase.DELETE_ORPHANS is not performant enough, need better query + ### to delete fingerprints for which no song is tied to. + #self.cursor.execute(SQLDatabase.DELETE_ORPHANS) + #self.connection.commit() except mysql.Error, e: print "Error in delete_orphans(), %d: %s" % (e.args[0], e.args[1]) self.connection.rollback() - def delete_unfingerprinted_songs(self): try: self.cursor = self.connection.cursor() diff --git a/dejavu/fingerprint.py b/dejavu/fingerprint.py index 9d7b2ee..89b57f6 100644 --- a/dejavu/fingerprint.py +++ b/dejavu/fingerprint.py @@ -197,13 +197,14 @@ class Fingerprinter(): largest_count = diff_counter[diff][sid] song_id = sid - if verbose: print "Diff is %d with %d offset-aligned matches" % (largest, largest_count) + if verbose: + print "Diff is %d with %d offset-aligned matches" % (largest, largest_count) - #from collections import OrderedDict - #print OrderedDict(diff_counter) - - # extract idenfication - songname = self.db.get_song_by_id(song_id)[SQLDatabase.FIELD_SONGNAME] + # extract idenfication + song = self.db.get_song_by_id(song_id) + songname = song.get(SQLDatabase.FIELD_SONGNAME, None) + if not songname: + return None songname = songname.replace("_", " ") elapsed = time.time() - starttime