From 6a8c774a0572c1abbf48886fff6d30b89fc4db90 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jul 2025 18:43:19 -0500 Subject: add indexes on state and direction --- src/slskd/Transfers/TransfersDbContext.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/slskd/Transfers/TransfersDbContext.cs b/src/slskd/Transfers/TransfersDbContext.cs index cd721539..ea138bcd 100644 --- a/src/slskd/Transfers/TransfersDbContext.cs +++ b/src/slskd/Transfers/TransfersDbContext.cs @@ -61,6 +61,16 @@ namespace slskd.Transfers .Entity() .Property(d => d.Direction) .HasConversion(new EnumToStringConverter()); + + modelBuilder + .Entity() + .HasIndex(t => t.Direction) + .HasDatabaseName("IDX_Transfers_Direction"); + + modelBuilder + .Entity() + .HasIndex(t => t.State) + .HasDatabaseName("IDX_Transfers_State"); } } } -- cgit v1.2.3 From a848d8397a1c12e37f59b699f049ce37afcc34a5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jul 2025 18:43:48 -0500 Subject: add GetDatabaseIndexes --- src/slskd/Core/Data/SchemaInspector.cs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/slskd/Core/Data/SchemaInspector.cs b/src/slskd/Core/Data/SchemaInspector.cs index 234b9100..d4fadfba 100644 --- a/src/slskd/Core/Data/SchemaInspector.cs +++ b/src/slskd/Core/Data/SchemaInspector.cs @@ -67,6 +67,8 @@ public static class SchemaInspector dict[table] = columns; } + connection.Close(); + return dict; } catch (Exception ex) @@ -75,8 +77,63 @@ public static class SchemaInspector } } + /// + /// Retrieves the index information for the specified SQLite database. + /// + /// The connection string for the database. + /// The index information. + /// Thrown if the database can't be accessed, or something else goes wrong. + public static Dictionary> GetDatabaseIndexes(string connectionString) + { + try + { + using var connection = new SqliteConnection(connectionString); + connection.Open(); + + var dict = new Dictionary>(); + + using var tableCommand = new SqliteCommand("SELECT name FROM sqlite_master WHERE type='table';", connection); + using var tableReader = tableCommand.ExecuteReader(); + + while (tableReader.Read()) + { + var table = tableReader.GetString(0); + + var indexes = new List(); + + using var command = new SqliteCommand($"PRAGMA index_list({table});", connection); + using var cr = command.ExecuteReader(); + + while (cr.Read()) + { + indexes.Add(new IndexInfo( + Seq: cr.GetInt64(cr.GetOrdinal("seq")), + Name: cr.GetString(cr.GetOrdinal("name")), + Unique: cr.GetInt64(cr.GetOrdinal("unique")) > 0, + Origin: cr.GetString(cr.GetOrdinal("origin")), + Partial: cr.GetInt64(cr.GetOrdinal("partial")) > 0)); + } + + dict[table] = indexes; + } + + connection.Close(); + + return dict; + } + catch (Exception ex) + { + throw new SlskdException($"Failed to retrieve index information for database '{connectionString}'. The database might be corrupt or in use by another application; if the problem persists the backing file may need to be deleted", ex); + } + } + /// /// Represents a SQLite database column. /// public record ColumnInfo(long Cid, string Name, string Type, bool NotNull, object DefaultValue, bool PrimaryKey); + + /// + /// Represents a SQLite database index. + /// + public record IndexInfo(long Seq, string Name, bool Unique, string Origin, bool Partial); } \ No newline at end of file -- cgit v1.2.3 From ab13639cc3ba0b056937fc2ea3502291e26fb791 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jul 2025 18:47:37 -0500 Subject: add migration to apply indexes to transfers db --- .../Z07062025_TransferIndexesMigration.cs | 89 ++++++++++++++++++++++ src/slskd/Core/Data/Migrator.cs | 1 + 2 files changed, 90 insertions(+) create mode 100644 src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs diff --git a/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs b/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs new file mode 100644 index 00000000..62ff9e7b --- /dev/null +++ b/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs @@ -0,0 +1,89 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Migrations; + +using System; +using System.IO; +using System.Linq; +using Microsoft.Data.Sqlite; +using Serilog; + +/// +/// Updates the Transfers table to add indexes on the Direction and State columns. +/// +public class Z07062025_TransferIndexesMigration : IMigration +{ + private ILogger Log { get; } = Serilog.Log.ForContext(); + private string DatabaseName { get; } = "transfers"; + private string ConnectionString => $"Data Source={Path.Combine(Program.DataDirectory, $"{DatabaseName}.db")}"; + + public bool NeedsToBeApplied() + { + // check to see if *BOTH* of the indexes are in place. if one or both are missing, we must apply + var schema = SchemaInspector.GetDatabaseIndexes(ConnectionString); + var txfers = schema["Transfers"]; + + var directionExists = txfers.Any(c => c.Name == "IDX_Transfers_Direction"); + var stateExists = txfers.Any(c => c.Name == "IDX_Transfers_State"); + + if (directionExists && stateExists) + { + return false; + } + + return true; + } + + public void Apply() + { + if (!NeedsToBeApplied()) + { + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z07062025_TransferIndexesMigration)); + return; + } + + using var connection = new SqliteConnection(ConnectionString); + connection.Open(); + + using var transaction = connection.BeginTransaction(); + + try + { + Log.Information("> Adding missing index(es) on the Transfers table..."); + + var directionCommand = new SqliteCommand(@" + CREATE INDEX IF NOT EXISTS IDX_Transfers_Direction ON Transfers (Direction) + ", connection, transaction); + directionCommand.ExecuteNonQuery(); + + var stateCommand = new SqliteCommand(@" + CREATE INDEX IF NOT EXISTS IDX_Transfers_State ON Transfers (State) + ", connection, transaction); + stateCommand.ExecuteNonQuery(); + + Log.Information("> Index(es) created"); + transaction.Commit(); + Log.Information("> Done!"); + } + catch (Exception) + { + transaction.Rollback(); + throw; + } + } +} diff --git a/src/slskd/Core/Data/Migrator.cs b/src/slskd/Core/Data/Migrator.cs index e359d334..645f4575 100644 --- a/src/slskd/Core/Data/Migrator.cs +++ b/src/slskd/Core/Data/Migrator.cs @@ -48,6 +48,7 @@ public class Migrator private Dictionary Migrations { get; } = new() { { nameof(Z04012025_TransferStateMigration), new Z04012025_TransferStateMigration() }, + { nameof(Z07062025_TransferIndexesMigration), new Z07062025_TransferIndexesMigration() }, }; /// -- cgit v1.2.3 From 652e1b329200f52b4f8b46599d271162d97cdef0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Jul 2025 13:53:46 -0500 Subject: naming --- src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs b/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs index 62ff9e7b..53e6e044 100644 --- a/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs +++ b/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs @@ -35,8 +35,8 @@ public class Z07062025_TransferIndexesMigration : IMigration public bool NeedsToBeApplied() { // check to see if *BOTH* of the indexes are in place. if one or both are missing, we must apply - var schema = SchemaInspector.GetDatabaseIndexes(ConnectionString); - var txfers = schema["Transfers"]; + var idxes = SchemaInspector.GetDatabaseIndexes(ConnectionString); + var txfers = idxes["Transfers"]; var directionExists = txfers.Any(c => c.Name == "IDX_Transfers_Direction"); var stateExists = txfers.Any(c => c.Name == "IDX_Transfers_State"); -- cgit v1.2.3