diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-12 08:19:09 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-12 08:19:09 -0500 |
| commit | 780e9b5fd515421a12cea3b35308889f70370e6e (patch) | |
| tree | cde330230bac8c2ec187271fd8db01af48b2d94c /src | |
| parent | 6a16de6dab8759051ad36bf788da634277bdede2 (diff) | |
| download | slskd-780e9b5fd515421a12cea3b35308889f70370e6e.tar.gz | |
split interfaces from repository implementation, rename repository to sqlite specific
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/IReadOnlyShareRepository.cs | 76 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareRepository.cs | 95 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Shares/SqliteShareRepository.cs (renamed from src/slskd/Shares/ShareRepository.cs) | 148 |
4 files changed, 187 insertions, 144 deletions
diff --git a/src/slskd/Shares/IReadOnlyShareRepository.cs b/src/slskd/Shares/IReadOnlyShareRepository.cs new file mode 100644 index 00000000..ae2e4c6a --- /dev/null +++ b/src/slskd/Shares/IReadOnlyShareRepository.cs @@ -0,0 +1,76 @@ +// <copyright file="IReadOnlyShareRepository.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Shares +{ + using System.Collections.Generic; + using Soulseek; + + /// <summary> + /// Read-only persistent storage of shared files and metadata. + /// </summary> + public interface IReadOnlyShareRepository + { + /// <summary> + /// Gets the connection string for this repository. + /// </summary> + string ConnectionString { get; } + + /// <summary> + /// Counts the number of directories in the database. + /// </summary> + /// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param> + /// <returns>The number of directories.</returns> + int CountDirectories(string parentDirectory = null); + + /// <summary> + /// Counts the number of files in the database. + /// </summary> + /// <param name="parentDirectory">The optional directory prefix used for counting files in a subdirectory.</param> + /// <returns>The number of files.</returns> + int CountFiles(string parentDirectory = null); + + /// <summary> + /// Finds the filename of the file matching the specified <paramref name="maskedFilename"/>. + /// </summary> + /// <param name="maskedFilename">The fully qualified remote path of the file.</param> + /// <returns>The filename, if found.</returns> + string FindFilename(string maskedFilename); + + /// <summary> + /// Lists all directories. + /// </summary> + /// <param name="parentDirectory">The optional directory prefix used for listing subdirectories.</param> + /// <returns>The list of directories.</returns> + IEnumerable<string> ListDirectories(string parentDirectory = null); + + /// <summary> + /// Lists all files. + /// </summary> + /// <param name="parentDirectory">The optional parent directory.</param> + /// <param name="includeFullPath">A value indicating whether the fully qualified path should be returned.</param> + /// <returns>The list of files.</returns> + IEnumerable<File> ListFiles(string parentDirectory = null, bool includeFullPath = false); + + /// <summary> + /// Searches the database for files matching the specified <paramref name="query"/>. + /// </summary> + /// <param name="query">The search query.</param> + /// <returns>The list of matching files.</returns> + IEnumerable<File> Search(SearchQuery query); + } +} diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs new file mode 100644 index 00000000..59c9537e --- /dev/null +++ b/src/slskd/Shares/IShareRepository.cs @@ -0,0 +1,95 @@ +// <copyright file="IShareRepository.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Shares +{ + using System; + using Soulseek; + + /// <summary> + /// Writeable persistent storage of shared files and metadata. + /// </summary> + public interface IShareRepository : IReadOnlyShareRepository + { + /// <summary> + /// Creates a new database. + /// </summary> + /// <remarks> + /// Creates tables using 'IF NOT EXISTS', so this is idempotent unless 'discardExisting` is specified, in which case + /// tables are explicitly dropped prior to creation. + /// </remarks> + /// <param name="discardExisting">An optional value that determines whether the existing database should be discarded.</param> + void Create(bool discardExisting = false); + + /// <summary> + /// Inserts a directory. + /// </summary> + /// <param name="name">The fully qualified local name of the directory.</param> + /// <param name="timestamp">The timestamp to assign to the record.</param> + void InsertDirectory(string name, long timestamp); + + /// <summary> + /// Inserts a file. + /// </summary> + /// <param name="maskedFilename">The fully qualified remote path of the file.</param> + /// <param name="originalFilename">The fully qualified local path of the file.</param> + /// <param name="touchedAt">The timestamp at which the file was last modified, according to the host OS.</param> + /// <param name="file">The Soulseek.File instance representing the file.</param> + /// <param name="timestamp">The timestamp to assign to the record.</param> + void InsertFile(string maskedFilename, string originalFilename, DateTime touchedAt, File file, long timestamp); + + /// <summary> + /// Inserts a scan record at the specified <paramref name="timestamp"/>. + /// </summary> + /// <param name="timestamp">The timestamp associated with the scan.</param> + /// <param name="options">The options snapshot at the start of the scan.</param> + void InsertScan(long timestamp, Options.SharesOptions options); + + /// <summary> + /// Backs the current database up to the database at the specified <paramref name="connectionString"/>. + /// </summary> + /// <param name="connectionString">The connection string of the destination database.</param> + void BackupTo(string connectionString); + + /// <summary> + /// Restores the current database from the database at the specified <paramref name="connectionString"/>. + /// </summary> + /// <param name="connectionString">The connection string of the source database.</param> + void RestoreFrom(string connectionString); + + /// <summary> + /// Deletes directory records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. + /// </summary> + /// <param name="olderThanTimestamp">The timestamp before which to delete directories.</param> + /// <returns>The number of records deleted.</returns> + long PruneDirectories(long olderThanTimestamp); + + /// <summary> + /// Deletes file records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. + /// </summary> + /// <param name="olderThanTimestamp">The timestamp before which to delete files.</param> + /// <returns>The number of records deleted.</returns> + long PruneFiles(long olderThanTimestamp); + + /// <summary> + /// Updates the scan started at the specified <paramref name="timestamp"/> to set the <paramref name="end"/>. + /// </summary> + /// <param name="timestamp">The timestamp associated with the scan.</param> + /// <param name="end">The timestamp at the conclusion of the scan.</param> + void UpdateScan(long timestamp, long end); + } +}
\ No newline at end of file diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 9ab96da9..c88a2345 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -66,7 +66,7 @@ namespace slskd.Shares }); }); - Repository = new ShareRepository(connectionString: Program.ConnectionStrings.Shares); + Repository = new SqliteShareRepository(connectionString: Program.ConnectionStrings.Shares); OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -110,8 +110,8 @@ namespace slskd.Shares AgentRepositories.AddOrUpdate( key: agent, - addValue: new ShareRepository(BuildConnectionString(agent)), - updateValueFactory: (agent, repository) => new ShareRepository(BuildConnectionString(agent))); + addValue: new SqliteShareRepository(BuildConnectionString(agent)), + updateValueFactory: (_, repository) => new SqliteShareRepository(BuildConnectionString(agent))); } /// <summary> @@ -289,7 +289,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Memory'. Attempting to load from backup..."); - if (ShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) + if (SqliteShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) { Log.Information("Share cache backup validated. Attempting to restore..."); @@ -307,7 +307,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Disk'. Attempting to validate..."); - if (ShareRepository.TryValidateDatabase(Program.ConnectionStrings.Shares)) + if (SqliteShareRepository.TryValidateDatabase(Program.ConnectionStrings.Shares)) { // no-op } @@ -315,7 +315,7 @@ namespace slskd.Shares { Log.Warning("Share cache is missing, corrupt, or is out of date. Attempting to load from backup..."); - if (ShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) + if (SqliteShareRepository.TryValidateDatabase(Program.ConnectionStrings.SharesBackup)) { Log.Information("Share cache backup validated. Attempting to restore..."); diff --git a/src/slskd/Shares/ShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index fd69c2a3..6a91ef82 100644 --- a/src/slskd/Shares/ShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -1,4 +1,4 @@ -// <copyright file="ShareRepository.cs" company="slskd Team"> +// <copyright file="SqliteShareRepository.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -26,142 +26,15 @@ namespace slskd.Shares using Soulseek; /// <summary> - /// Read-only persistent storage of shared files and metadata. - /// </summary> - public interface IReadOnlyShareRepository - { - /// <summary> - /// Gets the connection string for this repository. - /// </summary> - string ConnectionString { get; } - - /// <summary> - /// Counts the number of directories in the database. - /// </summary> - /// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param> - /// <returns>The number of directories.</returns> - int CountDirectories(string parentDirectory = null); - - /// <summary> - /// Counts the number of files in the database. - /// </summary> - /// <param name="parentDirectory">The optional directory prefix used for counting files in a subdirectory.</param> - /// <returns>The number of files.</returns> - int CountFiles(string parentDirectory = null); - - /// <summary> - /// Finds the filename of the file matching the specified <paramref name="maskedFilename"/>. - /// </summary> - /// <param name="maskedFilename">The fully qualified remote path of the file.</param> - /// <returns>The filename, if found.</returns> - string FindFilename(string maskedFilename); - - /// <summary> - /// Lists all directories. - /// </summary> - /// <param name="parentDirectory">The optional directory prefix used for listing subdirectories.</param> - /// <returns>The list of directories.</returns> - IEnumerable<string> ListDirectories(string parentDirectory = null); - - /// <summary> - /// Lists all files. - /// </summary> - /// <param name="parentDirectory">The optional parent directory.</param> - /// <param name="includeFullPath">A value indicating whether the fully qualified path should be returned.</param> - /// <returns>The list of files.</returns> - IEnumerable<Soulseek.File> ListFiles(string parentDirectory = null, bool includeFullPath = false); - - /// <summary> - /// Searches the database for files matching the specified <paramref name="query"/>. - /// </summary> - /// <param name="query">The search query.</param> - /// <returns>The list of matching files.</returns> - IEnumerable<Soulseek.File> Search(SearchQuery query); - } - - /// <summary> - /// Writeable persistent storage of shared files and metadata. - /// </summary> - public interface IShareRepository : IReadOnlyShareRepository - { - /// <summary> - /// Creates a new database. - /// </summary> - /// <remarks> - /// Creates tables using 'IF NOT EXISTS', so this is idempotent unless 'discardExisting` is specified, in which case - /// tables are explicitly dropped prior to creation. - /// </remarks> - /// <param name="discardExisting">An optional value that determines whether the existing database should be discarded.</param> - void Create(bool discardExisting = false); - - /// <summary> - /// Inserts a directory. - /// </summary> - /// <param name="name">The fully qualified local name of the directory.</param> - /// <param name="timestamp">The timestamp to assign to the record.</param> - void InsertDirectory(string name, long timestamp); - - /// <summary> - /// Inserts a file. - /// </summary> - /// <param name="maskedFilename">The fully qualified remote path of the file.</param> - /// <param name="originalFilename">The fully qualified local path of the file.</param> - /// <param name="touchedAt">The timestamp at which the file was last modified, according to the host OS.</param> - /// <param name="file">The Soulseek.File instance representing the file.</param> - /// <param name="timestamp">The timestamp to assign to the record.</param> - void InsertFile(string maskedFilename, string originalFilename, DateTime touchedAt, Soulseek.File file, long timestamp); - - /// <summary> - /// Inserts a scan record at the specified <paramref name="timestamp"/>. - /// </summary> - /// <param name="timestamp">The timestamp associated with the scan.</param> - /// <param name="options">The options snapshot at the start of the scan.</param> - void InsertScan(long timestamp, Options.SharesOptions options); - - /// <summary> - /// Backs the current database up to the database at the specified <paramref name="connectionString"/>. - /// </summary> - /// <param name="connectionString">The connection string of the destination database.</param> - void BackupTo(string connectionString); - - /// <summary> - /// Restores the current database from the database at the specified <paramref name="connectionString"/>. - /// </summary> - /// <param name="connectionString">The connection string of the source database.</param> - void RestoreFrom(string connectionString); - - /// <summary> - /// Deletes directory records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. - /// </summary> - /// <param name="olderThanTimestamp">The timestamp before which to delete directories.</param> - /// <returns>The number of records deleted.</returns> - long PruneDirectories(long olderThanTimestamp); - - /// <summary> - /// Deletes file records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. - /// </summary> - /// <param name="olderThanTimestamp">The timestamp before which to delete files.</param> - /// <returns>The number of records deleted.</returns> - long PruneFiles(long olderThanTimestamp); - - /// <summary> - /// Updates the scan started at the specified <paramref name="timestamp"/> to set the <paramref name="end"/>. - /// </summary> - /// <param name="timestamp">The timestamp associated with the scan.</param> - /// <param name="end">The timestamp at the conclusion of the scan.</param> - void UpdateScan(long timestamp, long end); - } - - /// <summary> /// Persistent storage of shared files and metadata. /// </summary> - public class ShareRepository : IShareRepository + public class SqliteShareRepository : IShareRepository { /// <summary> - /// Initializes a new instance of the <see cref="ShareRepository"/> class. + /// Initializes a new instance of the <see cref="SqliteShareRepository"/> class. /// </summary> /// <param name="connectionString"></param> - public ShareRepository(string connectionString) + public SqliteShareRepository(string connectionString) { ConnectionString = connectionString; } @@ -171,7 +44,7 @@ namespace slskd.Shares /// </summary> public string ConnectionString { get; } - private ILogger Log { get; } = Serilog.Log.ForContext<ShareRepository>(); + private ILogger Log { get; } = Serilog.Log.ForContext<SqliteShareRepository>(); /// <summary> /// Attempts to validate the database at the specified <paramref name="connectionString"/>. @@ -186,7 +59,7 @@ namespace slskd.Shares throw new ArgumentNullException(nameof(connectionString), "A connection string is required"); } - var log = Serilog.Log.ForContext<ShareRepository>(); + var log = Serilog.Log.ForContext<SqliteShareRepository>(); // to update this schema map, run the following query against a valid, up-to-date database and paste the output below: // select '{ "' || name || '", "' || sql || '" },' from sqlite_master where type = 'table' @@ -289,7 +162,7 @@ namespace slskd.Shares } finally { - cmd.Dispose(); + cmd?.Dispose(); } } @@ -322,7 +195,7 @@ namespace slskd.Shares } finally { - cmd.Dispose(); + cmd?.Dispose(); } } @@ -481,7 +354,7 @@ namespace slskd.Shares } finally { - cmd.Dispose(); + cmd?.Dispose(); } } @@ -606,13 +479,12 @@ namespace slskd.Shares "ORDER BY filenames.maskedFilename ASC;"; var results = new List<Soulseek.File>(); - SqliteDataReader reader = default; try { using var conn = GetConnection(); using var cmd = new SqliteCommand(sql, conn); - reader = cmd.ExecuteReader(); + using var reader = cmd.ExecuteReader(); while (reader.Read()) { |
