diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-13 09:43:17 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-13 09:43:17 -0500 |
| commit | 43c19eac0b258c852fc10d017daf1ee010f8cdcc (patch) | |
| tree | e74762e49f030ab95408c9a4ac56e71e89e6e17a /src/slskd | |
| parent | 1e87c47a84ab5b2188fb34aa642b8bae123dc2d3 (diff) | |
| download | slskd-43c19eac0b258c852fc10d017daf1ee010f8cdcc.tar.gz | |
differentiate read and read/write repositories. add host collection
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Program.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Shares/API/Controllers/SharesController.cs | 6 | ||||
| -rw-r--r-- | src/slskd/Shares/IReadOnlyShareRepository.cs | 60 | ||||
| -rw-r--r-- | src/slskd/Shares/IReadWriteShareRepository.cs | 96 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareRepository.cs | 83 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareService.cs | 20 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareScanner.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 91 | ||||
| -rw-r--r-- | src/slskd/Shares/SqliteShareRepository.cs | 2 |
9 files changed, 183 insertions, 186 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 44558e85..e744ac54 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -39,7 +39,6 @@ namespace slskd using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -519,9 +518,9 @@ namespace slskd services.AddSingleton<IShareService, ShareService>(); services.AddSingleton(new ShareConnectionStringFactory( - createFromFile: (name) => $"Data Source={Path.Combine(DataDirectory, name)}.db;Cache=shared", - createFromMemory: (name) => $"file:{name}?mode=memory", - createBackupFromFile: (name) => $"Data Source={Path.Combine(DataDirectory, name)}.db.bak;")); + createFromFile: (name) => $"Data Source={Path.Combine(DataDirectory, $"shares.{name}")}.db;Cache=shared", + createFromMemory: (name) => $"file:shares.{name}?mode=memory", + createBackupFromFile: (name) => $"Data Source={Path.Combine(DataDirectory, $"shares.{name}")}.db.bak;")); services.AddSingleton<ISearchService, SearchService>(); services.AddSingleton<IUserService, UserService>(); diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index d07e8eed..671e8992 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -58,7 +58,7 @@ namespace slskd.Shares.API { var shares = new List<SummarizedShare>(); - foreach (var share in Shares.Shares) + foreach (var share in Shares.Hosts.SelectMany(host => host.Shares)) { var (directories, files) = await Shares.SummarizeShareAsync(share); @@ -91,7 +91,7 @@ namespace slskd.Shares.API [ProducesResponseType(404)] public async Task<IActionResult> Get(string id) { - var share = Shares.Shares.FirstOrDefault(share => share.Id == id); + var share = Shares.Hosts.SelectMany(host => host.Shares).FirstOrDefault(share => share.Id == id); if (share == default) { @@ -141,7 +141,7 @@ namespace slskd.Shares.API [ProducesResponseType(404)] public async Task<IActionResult> BrowseShare(string id) { - var share = Shares.Shares.FirstOrDefault(share => share.Id == id); + var share = Shares.Hosts.SelectMany(host => host.Shares).FirstOrDefault(share => share.Id == id); if (share == default) { diff --git a/src/slskd/Shares/IReadOnlyShareRepository.cs b/src/slskd/Shares/IReadOnlyShareRepository.cs index 9822f536..fff587ad 100644 --- a/src/slskd/Shares/IReadOnlyShareRepository.cs +++ b/src/slskd/Shares/IReadOnlyShareRepository.cs @@ -17,68 +17,10 @@ namespace slskd.Shares { - using System.Collections.Generic; - using Soulseek; - /// <summary> /// Read-only persistent storage of shared files and metadata. /// </summary> - public interface IReadOnlyShareRepository + public interface IReadOnlyShareRepository : IShareRepository { - /// <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); - - /// <summary> - /// Attempts to validate the database at the specified <paramref name="connectionString"/>, or the - /// default <see cref="ConnectionString"/>. - /// </summary> - /// <param name="connectionString">The connection string of the database to validate.</param> - /// <returns>A value indicating whether the database is valid.</returns> - bool TryValidate(string connectionString = null); } } diff --git a/src/slskd/Shares/IReadWriteShareRepository.cs b/src/slskd/Shares/IReadWriteShareRepository.cs new file mode 100644 index 00000000..c6701753 --- /dev/null +++ b/src/slskd/Shares/IReadWriteShareRepository.cs @@ -0,0 +1,96 @@ +// <copyright file="IReadWriteShareRepository.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 IReadWriteShareRepository : IShareRepository + { + + /// <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/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs index 59c9537e..87c2c386 100644 --- a/src/slskd/Shares/IShareRepository.cs +++ b/src/slskd/Shares/IShareRepository.cs @@ -17,79 +17,68 @@ namespace slskd.Shares { - using System; + using System.Collections.Generic; using Soulseek; /// <summary> - /// Writeable persistent storage of shared files and metadata. + /// Persistent storage of shared files and metadata. /// </summary> - public interface IShareRepository : IReadOnlyShareRepository + public interface IShareRepository { /// <summary> - /// Creates a new database. + /// Gets the connection string for this repository. /// </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); + string ConnectionString { get; } /// <summary> - /// Inserts a directory. + /// Counts the number of directories in the database. /// </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); + /// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param> + /// <returns>The number of directories.</returns> + int CountDirectories(string parentDirectory = null); /// <summary> - /// Inserts a file. + /// Counts the number of files in the database. /// </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); + /// <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> - /// Inserts a scan record at the specified <paramref name="timestamp"/>. + /// Finds the filename of the file matching the specified <paramref name="maskedFilename"/>. /// </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); + /// <param name="maskedFilename">The fully qualified remote path of the file.</param> + /// <returns>The filename, if found.</returns> + string FindFilename(string maskedFilename); /// <summary> - /// Restores the current database from the database at the specified <paramref name="connectionString"/>. + /// Lists all directories. /// </summary> - /// <param name="connectionString">The connection string of the source database.</param> - void RestoreFrom(string connectionString); + /// <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> - /// Deletes directory records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. + /// Lists all files. /// </summary> - /// <param name="olderThanTimestamp">The timestamp before which to delete directories.</param> - /// <returns>The number of records deleted.</returns> - long PruneDirectories(long olderThanTimestamp); + /// <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> - /// Deletes file records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>. + /// Searches the database for files matching the specified <paramref name="query"/>. /// </summary> - /// <param name="olderThanTimestamp">The timestamp before which to delete files.</param> - /// <returns>The number of records deleted.</returns> - long PruneFiles(long olderThanTimestamp); + /// <param name="query">The search query.</param> + /// <returns>The list of matching files.</returns> + IEnumerable<File> Search(SearchQuery query); /// <summary> - /// Updates the scan started at the specified <paramref name="timestamp"/> to set the <paramref name="end"/>. + /// Attempts to validate the database at the specified <paramref name="connectionString"/>, or the + /// default <see cref="ConnectionString"/>. /// </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); + /// <param name="connectionString">The connection string of the database to validate.</param> + /// <returns>A value indicating whether the database is valid.</returns> + bool TryValidate(string connectionString = null); } -}
\ No newline at end of file +} diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 3ec5fd56..821ac20e 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -29,9 +29,9 @@ namespace slskd.Shares public interface IShareService { /// <summary> - /// Gets the list of configured shares. + /// Gets the list of share hosts. /// </summary> - IReadOnlyList<Share> Shares { get; } + IReadOnlyList<Host> Hosts { get; } /// <summary> /// Gets the state monitor for the service. @@ -39,18 +39,6 @@ namespace slskd.Shares public IStateMonitor<ShareState> StateMonitor { get; } /// <summary> - /// Adds the shares associated with the specified <paramref name="agent"/>. - /// </summary> - /// <param name="agent">The name of the agent.</param> - void AddAgentShares(string agent); - - /// <summary> - /// Remotes the shares associated with the specified <paramref name="agent"/>. - /// </summary> - /// <param name="agent">The name of the agent.</param> - void RemoveAgentShares(string agent); - - /// <summary> /// Returns the entire contents of the share. /// </summary> /// <returns>The entire contents of the share.</returns> @@ -82,7 +70,7 @@ namespace slskd.Shares Task<IEnumerable<File>> SearchAsync(SearchQuery query); /// <summary> - /// Scans the configured shares. + /// Scans the configured shares on the local host. /// </summary> /// <returns>The operation context.</returns> /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> @@ -96,7 +84,7 @@ namespace slskd.Shares Task<(int Directories, int Files)> SummarizeShareAsync(Share share); /// <summary> - /// Cancels the currently running scan, if one is running. + /// Cancels the currently running scan on the local host, if one is running. /// </summary> /// <returns>A value indicating whether a scan was cancelled.</returns> bool TryCancelScan(); diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs index 7a174396..f5e7e52d 100644 --- a/src/slskd/Shares/ShareScanner.cs +++ b/src/slskd/Shares/ShareScanner.cs @@ -67,7 +67,7 @@ namespace slskd.Shares /// <param name="soulseekFileFactory"></param> public ShareScanner( int workerCount, - IShareRepository shareRepository, + IReadWriteShareRepository shareRepository, ISoulseekFileFactory soulseekFileFactory = null) { WorkerCount = workerCount; @@ -87,7 +87,7 @@ namespace slskd.Shares private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); private CancellationTokenSource CancellationTokenSource { get; set; } - private IShareRepository Repository { get; } + private IReadWriteShareRepository Repository { get; } /// <summary> /// Scans the configured shares and fills the cache. diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 94940a71..d36a7d20 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -45,25 +45,30 @@ namespace slskd.Shares IOptionsMonitor<Options> optionsMonitor, IShareScanner scanner = null) { + var options = optionsMonitor.CurrentValue; + ConnectionStringFactory = shareConnectionStringFactory; - CacheStorageMode = optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum<StorageMode>(); + CacheStorageMode = options.Shares.Cache.StorageMode.ToEnum<StorageMode>(); if (CacheStorageMode == StorageMode.Memory) { - ConnectionString = ConnectionStringFactory.CreateFromMemory("shares"); + ConnectionString = ConnectionStringFactory.CreateFromMemory(options.InstanceName); } else { - ConnectionString = ConnectionStringFactory.CreateFromFile("shares"); + ConnectionString = ConnectionStringFactory.CreateFromFile(options.InstanceName); } - BackupConnectionString = ConnectionStringFactory.CreateBackupFromFile("shares"); + BackupConnectionString = ConnectionStringFactory.CreateBackupFromFile(options.InstanceName); + + var host = new Host(options.InstanceName, state: HostState.Online); + var repository = new SqliteShareRepository(ConnectionString); - Repository = new SqliteShareRepository(ConnectionString); + HostDictionary.TryAdd(options.InstanceName, (host, repository)); Scanner = scanner ?? new ShareScanner( - shareRepository: Repository, - workerCount: optionsMonitor.CurrentValue.Shares.Cache.Workers); + shareRepository: repository, + workerCount: options.Shares.Cache.Workers); Scanner.StateMonitor.OnChange(cacheState => { @@ -92,9 +97,9 @@ namespace slskd.Shares } /// <summary> - /// Gets the list of configured shares. + /// Gets the list of share hosts. /// </summary> - public IReadOnlyList<Share> Shares => SharesList.AsReadOnly(); + public IReadOnlyList<Host> Hosts => HostDictionary.Values.Select(v => v.Host).ToList().AsReadOnly(); /// <summary> /// Gets the state monitor for the service. @@ -105,40 +110,20 @@ namespace slskd.Shares private string BackupConnectionString { get; } private ShareConnectionStringFactory ConnectionStringFactory { get; } private IShareScanner Scanner { get; } - private IShareRepository Repository { get; } private string LastOptionsHash { get; set; } private IOptionsMonitor<Options> OptionsMonitor { get; } - private List<Share> SharesList { get; set; } = new List<Share>(); + private ConcurrentDictionary<string, (Host Host, IShareRepository Repository)> HostDictionary { get; set; } = new(); private IManagedState<ShareState> State { get; } = new ManagedState<ShareState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); private StorageMode CacheStorageMode { get; } private ILogger Log { get; } = Serilog.Log.ForContext<ShareService>(); - private ConcurrentDictionary<string, IReadOnlyShareRepository> AgentRepositories { get; } = new(); - - /// <summary> - /// Adds the shares associated with the specified <paramref name="agent"/>. - /// </summary> - /// <param name="agent">The name of the agent.</param> - public void AddAgentShares(string agent) + private (Host Host, IReadWriteShareRepository Repository) LocalHost { - if (string.IsNullOrEmpty(agent)) + get { - throw new ArgumentNullException(nameof(agent)); + HostDictionary.TryGetValue(OptionsMonitor.CurrentValue.InstanceName, out var record); + return (record.Host, (IReadWriteShareRepository)record.Repository); } - - AgentRepositories.AddOrUpdate( - key: agent, - addValue: new SqliteShareRepository(BuildConnectionString(agent)), - updateValueFactory: (_, repository) => new SqliteShareRepository(BuildConnectionString(agent))); - } - - /// <summary> - /// Remotes the shares associated with the specified <paramref name="agent"/>. - /// </summary> - /// <param name="agent">The name of the agent.</param> - public void RemoveAgentShares(string agent) - { - AgentRepositories.TryRemove(agent, out _); } /// <summary> @@ -161,13 +146,13 @@ namespace slskd.Shares // in the root. to get around this, prime a dictionary with all known directories, and an empty Soulseek.Directory. if // there are any files in the directory, this entry will be overwritten with a new Soulseek.Directory containing the // files. if not they'll be left as is. - foreach (var directory in Repository.ListDirectories(prefix)) + foreach (var directory in LocalHost.Repository.ListDirectories(prefix)) { var name = directory.NormalizePathForSoulseek(); directories.TryAdd(name, new Directory(name)); } - var files = Repository.ListFiles(prefix, includeFullPath: true); + var files = LocalHost.Repository.ListFiles(prefix, includeFullPath: true); var groups = files .GroupBy(file => Path.GetDirectoryName(file.Filename)) @@ -202,7 +187,7 @@ namespace slskd.Shares { var localPath = directory.LocalizePath(); - var files = Repository.ListFiles(localPath); + var files = LocalHost.Repository.ListFiles(localPath); return Task.FromResult(new Directory(directory, files)); } @@ -218,7 +203,7 @@ namespace slskd.Shares /// </exception> public Task<FileInfo> ResolveFileAsync(string remoteFilename) { - var resolvedFilename = Repository.FindFilename(remoteFilename.LocalizePath()); + var resolvedFilename = LocalHost.Repository.FindFilename(remoteFilename.LocalizePath()); if (string.IsNullOrEmpty(resolvedFilename)) { @@ -244,7 +229,7 @@ namespace slskd.Shares /// <returns>The matching files.</returns> public Task<IEnumerable<File>> SearchAsync(SearchQuery query) { - var results = Repository.Search(query); + var results = LocalHost.Repository.Search(query); return Task.FromResult(results.Select(r => new File( r.Code, @@ -261,10 +246,10 @@ namespace slskd.Shares /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> public async Task ScanAsync() { - await Scanner.ScanAsync(Shares, OptionsMonitor.CurrentValue.Shares); + await Scanner.ScanAsync(LocalHost.Host.Shares, OptionsMonitor.CurrentValue.Shares); Log.Debug("Backing up shared file cache database..."); - Repository.BackupTo(BackupConnectionString); + LocalHost.Repository.BackupTo(BackupConnectionString); Log.Debug("Shared file cache database backup complete"); } @@ -277,8 +262,8 @@ namespace slskd.Shares { var prefix = share.RemotePath + Path.DirectorySeparatorChar; - var dirs = Repository.CountDirectories(prefix); - var files = Repository.CountFiles(prefix); + var dirs = LocalHost.Repository.CountDirectories(prefix); + var files = LocalHost.Repository.CountFiles(prefix); return Task.FromResult((dirs, files)); } @@ -311,11 +296,11 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Memory'. Attempting to load from backup..."); - if (Repository.TryValidate(BackupConnectionString)) + if (LocalHost.Repository.TryValidate(BackupConnectionString)) { Log.Information("Share cache backup validated. Attempting to restore..."); - Repository.RestoreFrom(connectionString: BackupConnectionString); + LocalHost.Repository.RestoreFrom(connectionString: BackupConnectionString); Log.Information("Share cache successfully restored from backup"); } @@ -329,7 +314,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Disk'. Attempting to validate..."); - if (Repository.TryValidate(ConnectionString)) + if (LocalHost.Repository.TryValidate(ConnectionString)) { // no-op } @@ -337,11 +322,11 @@ namespace slskd.Shares { Log.Warning("Share cache is missing, corrupt, or is out of date. Attempting to load from backup..."); - if (Repository.TryValidate(BackupConnectionString)) + if (LocalHost.Repository.TryValidate(BackupConnectionString)) { Log.Information("Share cache backup validated. Attempting to restore..."); - Repository.RestoreFrom(connectionString: BackupConnectionString); + LocalHost.Repository.RestoreFrom(connectionString: BackupConnectionString); Log.Information("Share cache successfully restored from backup"); } @@ -366,8 +351,8 @@ namespace slskd.Shares Faulted = false, Ready = true, ScanProgress = 1, - Directories = Repository.CountDirectories(), - Files = Repository.CountFiles(), + Directories = LocalHost.Repository.CountDirectories(), + Files = LocalHost.Repository.CountFiles(), }); } catch (Exception ex) @@ -387,8 +372,6 @@ namespace slskd.Shares } } - private string BuildConnectionString(string agent = null) => $"Data Source={agent}{(!string.IsNullOrEmpty(agent) ? "." : string.Empty)}shares.db"; - private void Configure(Options options) { SyncRoot.Wait(); @@ -405,11 +388,11 @@ namespace slskd.Shares var shares = options.Shares.Directories .Select(share => share.TrimEnd('/', '\\')) .ToHashSet() // remove duplicates - .Select(share => new Share(share)) // convert to Shares + .Select(share => new Share(host: options.InstanceName, share)) // convert to Shares .OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent .ToList(); - SharesList = shares; + LocalHost.Host.SetShares(shares); State.SetValue(state => state with { ScanPending = true }); diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index 737c6954..bb00f420 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -28,7 +28,7 @@ namespace slskd.Shares /// <summary> /// Persistent storage of shared files and metadata. /// </summary> - public class SqliteShareRepository : IShareRepository + public class SqliteShareRepository : IReadWriteShareRepository { /// <summary> /// Initializes a new instance of the <see cref="SqliteShareRepository"/> class. |
