Skip to content

Add several indexes to share database, avoid using HasFlag() in other queries#1589

Merged
jpdillingham merged 16 commits into
masterfrom
indexes
Dec 27, 2025
Merged

Add several indexes to share database, avoid using HasFlag() in other queries#1589
jpdillingham merged 16 commits into
masterfrom
indexes

Conversation

@jpdillingham

Copy link
Copy Markdown
Member

SQLite can't leverage indexes with bitwise operations, which is what HasFlag does under the hood when Entity Framework translates it to SQL. This PR introduces TransferStateCategories that group combinations of TransferStates flags into logical categories. These categories can then be used in place of HasFlag, which will result in an IN clause instead of a bitwise operation.

This is mostly in preparation for some additional indexes on the transfers database.

I've also updated the transfer pruning logic so that only one UPDATE statement is issued, instead of one per transfer. Not sure why it was like that but it was very slow!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes database query performance in SQLite by eliminating bitwise operations (HasFlag) that prevent index utilization. It introduces TransferStateCategories to group transfer state combinations and updates pruning/removal logic to use bulk operations instead of individual updates.

  • Introduces TransferStateCategories static class with predefined HashSets of state combinations for optimized querying
  • Refactors Prune() and Remove() methods to use ExecuteUpdate() for bulk operations and accept multiple states
  • Adds indexes to the shares database for timestamp-based queries

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/slskd/Transfers/Types/TransferStateCategories.cs New file defining HashSets of transfer state combinations to enable IN clause queries instead of bitwise operations
src/slskd/Transfers/Uploads/UploadService.cs Updates Prune and Remove methods to accept state arrays, use ExecuteUpdate for bulk operations, and changes Remove to return bool/int
src/slskd/Transfers/Downloads/DownloadService.cs Similar changes as UploadService, plus updates AddOrSupersede to use TransferStateCategories
src/slskd/Transfers/API/Controllers/TransfersController.cs Updates clear completed endpoints to use new Remove overload with TransferStateCategories
src/slskd/Shares/SqliteShareRepository.cs Adds three timestamp-based indexes for better query performance
src/slskd/Application.cs Updates pruning calls to cast state combinations to int for new method signature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/slskd/Transfers/Types/TransferStateCategories.cs Outdated
Comment thread src/slskd/Transfers/Types/TransferStateCategories.cs Outdated
Comment thread src/slskd/Transfers/Types/TransferStateCategories.cs
Comment thread src/slskd/Transfers/Uploads/UploadService.cs
Comment thread src/slskd/Transfers/Uploads/UploadService.cs
Comment thread src/slskd/Transfers/Downloads/DownloadService.cs
Comment thread src/slskd/Transfers/Downloads/DownloadService.cs
Comment thread src/slskd/Transfers/Types/TransferStateCategories.cs Outdated
@jpdillingham jpdillingham merged commit 6a9c127 into master Dec 27, 2025
6 checks passed
@jpdillingham jpdillingham deleted the indexes branch December 27, 2025 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants