Melon Scanner

The Melon Scanner is designed to:


Class Structure

The MelonScanner class is implemented as a static class within the Melon.LocalClasses namespace. It encapsulates all functionalities related to scanning and updating the MongoDb database.

Properties

The class maintains several static properties to keep track of the scanning process and store temporary data.

Methods

The MelonScanner class provides several methods to perform scanning and database operations:


Scanning Process

Initialization

  1. Check Scanning Status: The scanner first checks if a scanning operation is already in progress by inspecting the Scanning flag. If scanning is active, it returns immediately to prevent concurrent scans.

  2. Set Up Database Connections: Establishes connections to the MongoDB database and retrieves the collections for artists, albums, and tracks.

  3. Load Existing Data: Loads existing tracks, albums, and artists from the database into in-memory dictionaries (tracks, albums, artists) for quick access and comparison.

  4. Initialize Variables: Resets counters and status messages. Sets up the LyricFiles list and other temporary data structures.

  5. Check Library Paths: Validates that library paths are specified in the settings. If none are found, it displays an error message and aborts the scanning process.

Folder Scanning

  1. Counting Files: The ScanFolderCounter method recursively traverses all library paths to count the total number of files that will be processed. This is essential for displaying accurate progress information.

  2. Scanning Files: The ScanFolder method recursively scans each folder in the library paths. For each file found:

    • Thread Management: Ensures that no more than 25 scanning threads are active simultaneously to prevent resource exhaustion.
    • File Processing: For each file, a new thread is spawned to process the file using the ScanInTrack method.

File Processing

  1. Filter Non-Audio Files: The ScanInTrack method first checks if the file is an audio file using the IsAudioFile method. If not, it skips processing.

  2. Process Lyric Files: If the file is a lyric file (e.g., ends with .lrc), it adds the path to the LyricFiles list and skips further processing.

  3. Extract Metadata: Uses the ATL library to extract metadata from the audio file, including:

    • Title
    • Artists
    • Album
    • Track number
    • Disc number
    • Genres
    • Release date
    • Additional fields (e.g., MusicBrainz ID, ISRC)
  4. Create ProtoTrack Object: Constructs a ProtoTrack object to temporarily hold the track's metadata.

  5. Handle Artists and Genres: Splits artist and genre strings into lists using the SplitArtists and SplitGenres methods.

  6. Update Existing Tracks: Checks if the track already exists in the tracks dictionary. If it does, updates the existing entry; otherwise, adds a new one.

  7. Error Handling: If an exception occurs during processing, logs the error to the FailedFiles collection in the database.

Database Update

After all files have been processed:

  1. Construct Albums and Artists: Uses the collected track data to construct albums and artists, ensuring that relationships between tracks, albums, and artists are accurately represented.

  2. Deduplicate Entries: Removes duplicate albums and artists by using distinct keys based on names and associated artists.

  3. Update Track References: Updates tracks to reference the correct album and artist IDs.

  4. Process Lyric Files: Associates lyric files with their corresponding tracks by matching file paths.

  5. Bulk Upload: Uses the Upload method to perform bulk write operations to the database for tracks, albums, and artists.

Cleanup Operations

  1. Delete Missing Files: The DeletePass method removes database entries for files that no longer exist in the file system.

  2. Update Collections: The UpdateCollections method updates dynamic collections based on the latest track information.

  3. Index Database Collections: Calls IndexCollections to ensure that database collections are properly indexed for efficient querying.

  4. Finalize Scanning: Resets scanning flags, updates status messages, and cleans up temporary data structures.


Database Interactions

Indexing Collections

The IndexCollections method creates indexes on the Artists, Albums, and Tracks collections to optimize query performance. It sets up indexes on the Name field with collation settings to ensure case-insensitive and culture-aware comparisons.

Resetting the Database

The ResetDb method performs a complete reset of the database by:

Warning: Resetting the database is a destructive operation that cannot be undone. All media metadata and user stats will be lost.


User Interface Integration

The Melon Scanner provides user interface methods to interact with users and display scanning progress:

Progress Display Diagram:


        +--------------------------------------------------------+
        | Melon > Scanner Progress                               |
        +--------------------------------------------------------+
        | Scan Status: 1500 / 3000 Found                         |
        | [####################################--------] 70%     |
        | System Status: Scanning files                          |
        |                                                        |
        |                                                        |
        |                                                        |
        +--------------------------------------------------------+
        

Diagrams

Overall Scanning Workflow

graph TD; A[Start Scan] --> B[Check Scanning Status] B --> |Not Scanning| C[Initialize Variables] C --> D[Load Existing Data from DB] D --> E[Validate Library Paths] E --> |Paths Exist| F[ScanFolderCounter] F --> G[ScanFolder] G --> H[ScanInTrack] H --> I[Process File Metadata] I --> J[Update tracks, albums, artists] J --> K[All Files Processed?] K --> |No| G K --> |Yes| L[FillOutDB] L --> M[Upload to DB] M --> N[DeletePass] N --> O[UpdateCollections] O --> P[IndexCollections] P --> Q[End Scan] B --> |Already Scanning| R[Return] E --> |No Paths| S[Display Error & Abort]

File Processing Workflow

graph TD; A[ScanInTrack] --> B[IsAudioFile?] B --> |No| C[Is Lyric File?] B --> |Yes| D[Extract Metadata] C --> |Yes| E[Add to LyricFiles] C --> |No| F[Skip File] D --> G[Create ProtoTrack] G --> H[Update Existing Track?] H --> |Yes| I[Update Track in Dictionary] H --> |No| J[Add Track to Dictionary] I --> K[End Thread] J --> K