1
0
mirror of https://github.com/bspeice/Melodia synced 2024-12-26 00:28:13 -05:00

Optimize scanning further, make song URL's PK

The Primary Key is added because these URL's must be unique anyway - you
can't have two filenames point to different files. Additionally, this
ensures that the database doesn't have multiple copies of a song.
This commit is contained in:
Bradlee Speice 2013-01-10 10:43:18 -05:00
parent d4d3ad450a
commit 43bd55d1f5
2 changed files with 9 additions and 20 deletions

View File

@ -51,25 +51,14 @@ class Archive (models.Model):
_regex = '|'.join(( '.*' + ext + '$' for ext in SUPPORTED_AUDIO_EXTENSIONS)) _regex = '|'.join(( '.*' + ext + '$' for ext in SUPPORTED_AUDIO_EXTENSIONS))
regex = re.compile(_regex, re.IGNORECASE) regex = re.compile(_regex, re.IGNORECASE)
#Remove songs in the database if they exist no longer #It's hackish, but far fewer transactions to delete everything first, and add it all back.
# -Do this first since we don't need to re-check songs that were just added #If we get interrupted, just re-run it.
for song in self.songs.all(): self.songs.all().delete()
if not os.path.isfile(song.url):
song.delete()
continue
#Add new songs #Add new songs
for dirname, dirnames, filenames in os.walk(self.root_folder): for dirname, dirnames, filenames in os.walk(self.root_folder):
#For each filename that is supported #For each filename that is supported
for filename in itertools.ifilter(lambda filename: re.match(regex, filename), filenames): for filename in itertools.ifilter(lambda filename: re.match(regex, filename), filenames):
#Make sure that `filename` is in the database
try:
rel_url = os.path.join(dirname, filename)
full_url = os.path.abspath(rel_url)
self.songs.get(url = full_url)
except ObjectDoesNotExist, e:
#Song needs to be added to database
rel_url = os.path.join(dirname, filename) rel_url = os.path.join(dirname, filename)
full_url = os.path.abspath(rel_url) full_url = os.path.abspath(rel_url)
new_song = Song(url = full_url) new_song = Song(url = full_url)

View File

@ -47,7 +47,7 @@ class Song (models.Model):
bit_rate = models.IntegerField(default = _default_bit_rate) bit_rate = models.IntegerField(default = _default_bit_rate)
duration = models.IntegerField(default = _default_bit_rate) duration = models.IntegerField(default = _default_bit_rate)
echonest_song_id = models.CharField(max_length = 64, default = _default_echonest_song_id) echonest_song_id = models.CharField(max_length = 64, default = _default_echonest_song_id)
url = models.CharField(max_length = 64) url = models.CharField(max_length = 255, primary_key = True)
file_hash = melodia_settings.HASH_RESULT_DB_TYPE file_hash = melodia_settings.HASH_RESULT_DB_TYPE
def populate_metadata(self, use_echonest = False): def populate_metadata(self, use_echonest = False):