2013-12-17 00:39:03 +00:00
|
|
|
from __future__ import absolute_import
|
2013-12-20 17:16:35 +00:00
|
|
|
import abc
|
2013-12-17 01:00:05 +00:00
|
|
|
|
2013-12-17 20:55:05 +00:00
|
|
|
|
2013-12-17 00:39:03 +00:00
|
|
|
class Database(object):
|
2013-12-20 17:16:35 +00:00
|
|
|
__metaclass__ = abc.ABCMeta
|
|
|
|
|
|
|
|
# Name of your Database subclass, this is used in configuration
|
|
|
|
# to refer to your class
|
|
|
|
type = None
|
|
|
|
|
2013-12-17 00:39:03 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(Database, self).__init__()
|
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
def before_fork(self):
|
|
|
|
"""
|
|
|
|
Called before the database instance is given to the new process
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after_fork(self):
|
|
|
|
"""
|
|
|
|
Called after the database instance has been given to the new process
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
This will be called in the new process.
|
|
|
|
"""
|
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
|
|
|
def setup(self):
|
2013-12-17 20:55:05 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Called on creation or shortly afterwards.
|
2013-12-17 20:55:05 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def empty(self):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Called when the database should be cleared of all data.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def delete_unfingerprinted_songs(self):
|
2013-12-17 20:55:05 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Called to remove any song entries that do not have any fingerprints
|
|
|
|
associated with them.
|
2013-12-17 20:55:05 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def get_num_songs(self):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns the amount of songs in the database.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def get_num_fingerprints(self):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns the number of fingerprints in the database.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-12-17 00:39:03 +00:00
|
|
|
def set_song_fingerprinted(self, sid):
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Sets a specific song as having all fingerprints in the database.
|
|
|
|
|
|
|
|
sid: Song identifier
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def get_songs(self):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns all fully fingerprinted songs in the database.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def get_song_by_id(self, sid):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Return a song by its identifier
|
|
|
|
|
|
|
|
sid: Song identifier
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-12-17 00:39:03 +00:00
|
|
|
def insert(self, hash, sid, offset):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Inserts a single fingerprint into the database.
|
|
|
|
|
|
|
|
hash: Part of a sha1 hash, in hexadecimal format
|
|
|
|
sid: Song identifier this fingerprint is off
|
|
|
|
offset: The offset this hash is from
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
|
|
|
def insert_song(self, song_name):
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Inserts a song name into the database, returns the new
|
|
|
|
identifier of the song.
|
|
|
|
|
|
|
|
song_name: The name of the song.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-12-17 00:39:03 +00:00
|
|
|
def query(self, hash):
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns all matching fingerprint entries associated with
|
|
|
|
the given hash as parameter.
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
hash: Part of a sha1 hash, in hexadecimal format
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def get_iterable_kv_pairs(self):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns all fingerprints in the database.
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-12-17 00:48:49 +00:00
|
|
|
def insert_hashes(self, sid, hashes):
|
2013-11-19 02:51:27 +00:00
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Insert a multitude of fingerprints.
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
sid: Song identifier the fingerprints belong to
|
|
|
|
hashes: A sequence of tuples in the format (hash, offset)
|
|
|
|
- hash: Part of a sha1 hash, in hexadecimal format
|
|
|
|
- offset: Offset this hash was created from/at.
|
|
|
|
"""
|
|
|
|
pass
|
2013-11-19 02:51:27 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
@abc.abstractmethod
|
2013-11-19 02:51:27 +00:00
|
|
|
def return_matches(self, hashes):
|
|
|
|
"""
|
2013-12-20 17:16:35 +00:00
|
|
|
Searches the database for pairs of (hash, offset) values.
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
hashes: A sequence of tuples in the format (hash, offset)
|
|
|
|
- hash: Part of a sha1 hash, in hexadecimal format
|
|
|
|
- offset: Offset this hash was created from/at.
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
Returns a sequence of (sid, offset_difference) tuples.
|
|
|
|
|
|
|
|
sid: Song identifier
|
|
|
|
offset_difference: (offset - database_offset)
|
|
|
|
"""
|
|
|
|
pass
|
2013-12-17 00:39:03 +00:00
|
|
|
|
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
def get_database(database_type=None):
|
|
|
|
# Default to using the mysql database
|
|
|
|
database_type = database_type or "mysql"
|
|
|
|
# Lower all the input.
|
|
|
|
database_type = database_type.lower()
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
for db_cls in Database.__subclasses__():
|
|
|
|
if db_cls.type == database_type:
|
|
|
|
return db_cls
|
2013-12-17 00:39:03 +00:00
|
|
|
|
2013-12-20 17:16:35 +00:00
|
|
|
raise TypeError("Unsupported database type supplied.")
|
2013-12-21 11:01:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Import our default database handler
|
|
|
|
import dejavu.database_sql
|