database.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from __future__ import absolute_import
  2. import abc
  3. class Database(object):
  4. __metaclass__ = abc.ABCMeta
  5. FIELD_FILE_SHA1 = 'file_sha1'
  6. FIELD_SONG_ID = 'id'
  7. FIELD_SONGNAME = 'name'
  8. FIELD_LENGTH = 'length'
  9. FIELD_OFFSET = 'offset'
  10. FIELD_HASH = 'hash'
  11. # Name of your Database subclass, this is used in configuration
  12. # to refer to your class
  13. type = None
  14. def __init__(self):
  15. super(Database, self).__init__()
  16. def before_fork(self):
  17. """
  18. Called before the database instance is given to the new process
  19. """
  20. pass
  21. def after_fork(self):
  22. """
  23. Called after the database instance has been given to the new process
  24. This will be called in the new process.
  25. """
  26. pass
  27. def setup(self):
  28. """
  29. Called on creation or shortly afterwards.
  30. """
  31. pass
  32. @abc.abstractmethod
  33. def empty(self):
  34. """
  35. Called when the database should be cleared of all data.
  36. """
  37. pass
  38. @abc.abstractmethod
  39. def delete_unfingerprinted_songs(self):
  40. """
  41. Called to remove any song entries that do not have any fingerprints
  42. associated with them.
  43. """
  44. pass
  45. @abc.abstractmethod
  46. def get_num_songs(self):
  47. """
  48. Returns the amount of songs in the database.
  49. """
  50. pass
  51. @abc.abstractmethod
  52. def get_num_fingerprints(self):
  53. """
  54. Returns the number of fingerprints in the database.
  55. """
  56. pass
  57. @abc.abstractmethod
  58. def set_song_fingerprinted(self, sid):
  59. """
  60. Sets a specific song as having all fingerprints in the database.
  61. sid: Song identifier
  62. """
  63. pass
  64. @abc.abstractmethod
  65. def get_songs(self):
  66. """
  67. Returns all fully fingerprinted songs in the database.
  68. """
  69. pass
  70. @abc.abstractmethod
  71. def get_song_by_id(self, sid):
  72. """
  73. Return a song by its identifier
  74. sid: Song identifier
  75. """
  76. pass
  77. @abc.abstractmethod
  78. def insert(self, hash, sid, offset):
  79. """
  80. Inserts a single fingerprint into the database.
  81. hash: Part of a sha1 hash, in hexadecimal format
  82. sid: Song identifier this fingerprint is off
  83. offset: The offset this hash is from
  84. """
  85. pass
  86. @abc.abstractmethod
  87. def insert_song(self, song_name):
  88. """
  89. Inserts a song name into the database, returns the new
  90. identifier of the song.
  91. song_name: The name of the song.
  92. """
  93. pass
  94. @abc.abstractmethod
  95. def query(self, hash):
  96. """
  97. Returns all matching fingerprint entries associated with
  98. the given hash as parameter.
  99. hash: Part of a sha1 hash, in hexadecimal format
  100. """
  101. pass
  102. @abc.abstractmethod
  103. def get_iterable_kv_pairs(self):
  104. """
  105. Returns all fingerprints in the database.
  106. """
  107. pass
  108. @abc.abstractmethod
  109. def insert_hashes(self, sid, hashes):
  110. """
  111. Insert a multitude of fingerprints.
  112. sid: Song identifier the fingerprints belong to
  113. hashes: A sequence of tuples in the format (hash, offset)
  114. - hash: Part of a sha1 hash, in hexadecimal format
  115. - offset: Offset this hash was created from/at.
  116. """
  117. pass
  118. @abc.abstractmethod
  119. def return_matches(self, hashes, ads_filter):
  120. """
  121. Searches the database for pairs of (hash, offset) values.
  122. hashes: A sequence of tuples in the format (hash, offset)
  123. - hash: Part of a sha1 hash, in hexadecimal format
  124. - offset: Offset this hash was created from/at.
  125. Returns a sequence of (sid, offset_difference) tuples.
  126. sid: Song identifier
  127. offset_difference: (offset - database_offset)
  128. """
  129. pass
  130. def get_database(database_type='None'):
  131. # Default to using the mysql database
  132. database_type = database_type or "mysql"
  133. # Lower all the input.
  134. database_type = database_type.lower()
  135. for db_cls in Database.__subclasses__():
  136. if db_cls.type == database_type:
  137. return db_cls
  138. raise TypeError("Unsupported database type supplied.")
  139. import dejavu.database_mem