Skip to content

Commit

Permalink
Optimize the cache updater to minimize disk accesses
Browse files Browse the repository at this point in the history
The bottleneck is disk accesses, so we rewrote the updater to to
minimize them. We trade higher memory for reduced disk accesses. We:

1. Execute one big SQL query at the start to fetch the relevant previous caches.
2. Skip reading a file's data if the mtime has not changed since the previous cache update.
3. Only execute a SQLite upsert if the read data differ from the previous caches.

With these optimizations, we make a lot of readdir and stat calls, but
minimize file and database accesses to solely the files that have
updated since the last cache run.
  • Loading branch information
azuline committed Oct 13, 2023
1 parent 587dbbb commit 95aa414
Show file tree
Hide file tree
Showing 8 changed files with 809 additions and 311 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,15 @@ must be top-level directories inside `music_source_dir`. Each album should also
be a single directory in `music_source_dir`.

Every directory should follow the format: `$music_source_dir/$album_name/$track.mp3`.
Additional nested directories are not currently supported.

So for example: `$music_source_dir/BLACKPINK - 2016. SQUARE ONE/*.mp3`.

Currently,

## Filetypes

Rosé supports MP3, M4A, OGG, OPUS, and FLAC audio files.
Rosé supports `.mp3`, `.m4a`, `.ogg` (vorbis), `.opus`, and `.flac` audio files.

Rosé also supports JPEG and PNG cover art. The supported cover art file stems
are `cover`, `folder`, and `art`. The supported cover art file extensions are
Expand Down
68 changes: 37 additions & 31 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,43 @@ def seeded_cache(config: Config) -> None:
with sqlite3.connect(config.cache_database_path) as conn:
conn.executescript(
f"""\
INSERT INTO releases (id, source_path, cover_image_path, virtual_dirname, title, release_type, release_year, new)
VALUES ('r1', '{dirpaths[0]}', null, 'r1', 'Release 1', 'album', 2023, true)
, ('r2', '{dirpaths[1]}', '{imagepaths[0]}', 'r2', 'Release 2', 'album', 2021, false);
INSERT INTO releases_genres (release_id, genre, genre_sanitized)
VALUES ('r1', 'Techno', 'Techno')
, ('r1', 'Deep House', 'Deep House')
, ('r2', 'Classical', 'Classical');
INSERT INTO releases_labels (release_id, label, label_sanitized)
VALUES ('r1', 'Silk Music', 'Silk Music')
, ('r2', 'Native State', 'Native State');
INSERT INTO tracks (id, source_path, virtual_filename, title, release_id, track_number, disc_number, duration_seconds)
VALUES ('t1', '{musicpaths[0]}', '01.m4a', 'Track 1', 'r1', '01', '01', 120)
, ('t2', '{musicpaths[1]}', '02.m4a', 'Track 2', 'r1', '02', '01', 240)
, ('t3', '{musicpaths[2]}', '01.m4a', 'Track 1', 'r2', '01', '01', 120);
INSERT INTO releases_artists (release_id, artist, artist_sanitized, role)
VALUES ('r1', 'Techno Man', 'Techno Man', 'main')
, ('r1', 'Bass Man', 'Bass Man', 'main')
, ('r2', 'Violin Woman', 'Violin Woman', 'main')
, ('r2', 'Conductor Woman', 'Conductor Woman', 'guest');
INSERT INTO tracks_artists (track_id, artist, artist_sanitized, role)
VALUES ('t1', 'Techno Man', 'Techno Man', 'main')
, ('t1', 'Bass Man', 'Bass Man', 'main')
, ('t2', 'Techno Man', 'Techno Man', 'main')
, ('t2', 'Bass Man', 'Bass Man', 'main')
, ('t3', 'Violin Woman', 'Violin Woman', 'main')
, ('t3', 'Conductor Woman', 'Conductor Woman', 'guest');
INSERT INTO releases
(id , source_path , cover_image_path , datafile_mtime, virtual_dirname, title , release_type, release_year, multidisc, new , formatted_artists)
VALUES ('r1', '{dirpaths[0]}', null , '999' , 'r1' , 'Release 1', 'album' , 2023 , false , true , 'Techno Man;Bass Man')
, ('r2', '{dirpaths[1]}', '{imagepaths[0]}', '999' , 'r2' , 'Release 2', 'album' , 2021 , false , false, 'Violin Woman feat. Conductor Woman');
INSERT INTO releases_genres
(release_id, genre , genre_sanitized)
VALUES ('r1' , 'Techno' , 'Techno')
, ('r1' , 'Deep House', 'Deep House')
, ('r2' , 'Classical' , 'Classical');
INSERT INTO releases_labels
(release_id, label , label_sanitized)
VALUES ('r1' , 'Silk Music' , 'Silk Music')
, ('r2' , 'Native State', 'Native State');
INSERT INTO tracks
(id , source_path , source_mtime, virtual_filename, title , release_id, track_number, disc_number, duration_seconds, formatted_artists)
VALUES ('t1', '{musicpaths[0]}', '999' , '01.m4a' , 'Track 1', 'r1' , '01' , '01' , 120 , 'Techno Man;Bass Man')
, ('t2', '{musicpaths[1]}', '999' , '02.m4a' , 'Track 2', 'r1' , '02' , '01' , 240 , 'Techno Man;Bass Man')
, ('t3', '{musicpaths[2]}', '999' , '01.m4a' , 'Track 1', 'r2' , '01' , '01' , 120 , 'Violin Woman feat. Conductor Woman');
INSERT INTO releases_artists
(release_id, artist , artist_sanitized , role)
VALUES ('r1' , 'Techno Man' , 'Techno Man' , 'main')
, ('r1' , 'Bass Man' , 'Bass Man' , 'main')
, ('r2' , 'Violin Woman' , 'Violin Woman' , 'main')
, ('r2' , 'Conductor Woman', 'Conductor Woman', 'guest');
INSERT INTO tracks_artists
(track_id, artist , artist_sanitized , role)
VALUES ('t1' , 'Techno Man' , 'Techno Man' , 'main')
, ('t1' , 'Bass Man' , 'Bass Man' , 'main')
, ('t2' , 'Techno Man' , 'Techno Man' , 'main')
, ('t2' , 'Bass Man' , 'Bass Man' , 'main')
, ('t3' , 'Violin Woman' , 'Violin Woman' , 'main')
, ('t3' , 'Conductor Woman', 'Conductor Woman', 'guest');
""" # noqa: E501
)

Expand Down
Loading

0 comments on commit 95aa414

Please sign in to comment.