Library Editing
Melon's library comes from your files' tags. So when a tag is wrong and you fix it in Melon, the hard part isn't changing the database, it's making the change stick. If only the database changed, the next scan would read the old tags and put everything back. This page explains how edits and deletes work so they don't get undone. The endpoints themselves are in Metadata Updates and Creating and Deleting.
How an Edit Runs
flowchart TD
A[Edit request] --> B{Scan running?}
B -- yes --> X[409]
B -- no --> C[Change the database]
C --> D[Update copied names]
D --> E[Queue a tag job per file]
E --> F[Return EditResult]
E -.background.-> G[Tag writer writes files]
-
One at a time. Edits run one after another, never at
the same time, so two edits can't trip over each other. While a scan
is running, edits are refused with
409instead of waiting, since the scan is reading the same files. - The database changes first. Clients see the edit as soon as the request returns.
- Copied names follow. Melon copies names into other documents so it doesn't have to look them up every time (a track stores its album's name and its artists' names). A rename updates every copy: artist names on tracks, albums, connected artists and chapters; album names on tracks, variant links and chapters; track names in playlists, collections, queues and chapters.
-
Each affected file gets a tag job. Renaming an
album touches every track on it, so every one of those files gets
a job. The jobs live in the
TagJobscollection, so they survive a restart.
Tag Jobs
The tag writer runs in the background and works through jobs, up to 200 files at a time. A job doesn't remember what the edit was. It writes whatever the track looks like in the database right now. So five quick edits to one track only cost one file write, and the file always ends up matching the database.
What gets written:
- Title, album, track and disc number, genres, and the date (or the year, if that's all there is).
-
Artists, twice: in the normal tag (
ARTIST,ALBUMARTIST) and in the multi-value tag (ARTISTS,ALBUMARTISTS). The scanner reads the multi-value tag first and never splits it, so names like "Tyler, The Creator" or "Chase & Status" come back whole. See Artists. - ISRC, MusicBrainz id, publisher, release status and release type, under whatever name the file already uses for them (MP3s tagged by Picard spell them differently).
- Chapters, when the track has some.
A track with no date in the database never gets a date of 0 written over the one in the file. After a file is written, Melon updates the track's last modified time, so a short scan doesn't bother reading it again.
If a file can't be written (read-only, open in another program, a format that can't be tagged), it's tried again, up to 3 times. After that it's marked as failed and shows up in api/update/jobs, where it can be retried. The database keeps the edit either way.
Edits and Scans
Before a scan reads anything, it waits for a running edit to finish and writes every job that's still waiting. So a scan never reads a file that's behind the database, and an edit is never undone by a scan that started a second later.
A scan can still change what an edit did when the edit goes against the scanner's rules. For example, merging two copies of a release whose track numbers clash gets them split back into variants on the next scan, since the files are in different folders and clash.
Merging
A merge folds duplicates into one target and removes the duplicates. It happens when you ask for one, and also when an edit would make two things the same: renaming an artist to a name another artist has, or renaming an album to the name and artists of another album.
- Credits (for artists) or tracks (for albums) move to the target, and their files get new tags.
- Plays and skips add up per user. The target's ratings stay, and a duplicate's rating only fills in for users who hadn't rated the target.
- Artwork and bios carry over where the target has none. An artist's genres are added to the target's.
- Play history in the
Statscollection moves to the target.
Deleting
Deleting removes files from disk, so it's built to be undone for a while. It uses three collections:
-
Trashholds deleted tracks. The whole track document moves here fromTracks. Every library endpoint, search and mix reads fromTracks, so a deleted track is gone from all of them at once without any of them needing to check for it. -
TrashDocsholds albums and artists that a delete left empty, so recovering a track can bring them back the way they were. -
RemovedTracksremembers the name of every deleted track, even after its file is gone. That's what lets play history show "Removed song" instead of an id that leads nowhere.
Each trashed track has a time it'll be deleted at: when it was deleted plus the deletion wait (30 minutes by default). Every 30 seconds Melon looks for tracks whose time is up and deletes their file, their lyrics file if nothing else uses it, and their folder if it's empty now. It never deletes a library folder itself. Then the track is taken out of playlists, collections and queues. If the file can't be deleted, the track stays in the trash with the error and is tried again next time.
The scanner skips every file that's in the trash, so a track waiting to be deleted doesn't come back in a scan. Changing the wait only applies to tracks deleted afterwards.
Recovering puts the track document back in Tracks, brings
back its album and artists from TrashDocs if they went
too, and recounts the album. Nothing was taken out of playlists yet,
so they're whole again.