aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-09-24 17:19:20 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-09-24 17:19:20 -0500
commit9b8d59a93b4661f7e3650e025b725498b9a5567b (patch)
tree12c2f41ee7a19c1bcd2f2e08be9ab2579272531b /src
parent6484666fc88126f3216846409e3f1b81b2a78677 (diff)
downloadslskd-9b8d59a93b4661f7e3650e025b725498b9a5567b.tar.gz
improve shares UI performance
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Shares/API/Controllers/SharesController.cs38
-rw-r--r--src/slskd/Shares/IShareService.cs9
-rw-r--r--src/slskd/Shares/ISharedFileCache.cs17
-rw-r--r--src/slskd/Shares/ShareService.cs16
-rw-r--r--src/slskd/Shares/SharedFileCache.cs152
5 files changed, 162 insertions, 70 deletions
diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs
index 44d79a18..5f87f900 100644
--- a/src/slskd/Shares/API/Controllers/SharesController.cs
+++ b/src/slskd/Shares/API/Controllers/SharesController.cs
@@ -57,18 +57,13 @@ namespace slskd.Shares.API
[ProducesResponseType(typeof(IEnumerable<SummarizedShare>), 200)]
public async Task<IActionResult> List()
{
- var browse = await Shares.BrowseAsync();
+ var shares = new List<SummarizedShare>();
- var summary = Shares.Shares.Select(share =>
+ foreach (var share in Shares.Shares)
{
- var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath));
- var fileCount = directories.Aggregate(seed: 0, (sum, directory) =>
- {
- sum += directory.FileCount;
- return sum;
- });
+ var (directories, files) = await Shares.SummarizeShareAsync(share);
- return new SummarizedShare()
+ shares.Add(new SummarizedShare()
{
Id = share.Id,
Alias = share.Alias,
@@ -76,12 +71,12 @@ namespace slskd.Shares.API
LocalPath = share.LocalPath,
Raw = share.Raw,
RemotePath = share.RemotePath,
- Directories = directories.Count(),
- Files = fileCount,
- };
- });
+ Directories = directories,
+ Files = files,
+ });
+ }
- return Ok(summary);
+ return Ok(shares);
}
/// <summary>
@@ -104,13 +99,7 @@ namespace slskd.Shares.API
return NotFound();
}
- var browse = await Shares.BrowseAsync();
- var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath));
- var fileCount = directories.Aggregate(seed: 0, (sum, directory) =>
- {
- sum += directory.FileCount;
- return sum;
- });
+ var (directories, files) = await Shares.SummarizeShareAsync(share);
var summary = new SummarizedShare()
{
@@ -120,8 +109,8 @@ namespace slskd.Shares.API
LocalPath = share.LocalPath,
Raw = share.Raw,
RemotePath = share.RemotePath,
- Directories = directories.Count(),
- Files = fileCount,
+ Directories = directories,
+ Files = files,
};
return Ok(summary);
@@ -159,8 +148,7 @@ namespace slskd.Shares.API
return NotFound();
}
- var contents = (await Shares.BrowseAsync())
- .Where(directory => directory.Name.StartsWith(share.RemotePath));
+ var contents = await Shares.BrowseAsync(share);
return Ok(contents);
}
diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs
index b625286e..73f6a4ad 100644
--- a/src/slskd/Shares/IShareService.cs
+++ b/src/slskd/Shares/IShareService.cs
@@ -42,7 +42,7 @@ namespace slskd.Shares
/// Returns the entire contents of the share.
/// </summary>
/// <returns>The entire contents of the share.</returns>
- Task<IEnumerable<Directory>> BrowseAsync();
+ Task<IEnumerable<Directory>> BrowseAsync(Share share = null);
/// <summary>
/// Returns the contents of the specified <paramref name="directory"/>.
@@ -77,6 +77,13 @@ namespace slskd.Shares
Task ScanAsync();
/// <summary>
+ /// Gets summary information for the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share to summarize.</param>
+ /// <returns>The summary information.</returns>
+ Task<(int Directories, int Files)> SummarizeShareAsync(Share share);
+
+ /// <summary>
/// Cancels the currently running scan, if one is running.
/// </summary>
/// <returns>A value indicating whether a scan was cancelled.</returns>
diff --git a/src/slskd/Shares/ISharedFileCache.cs b/src/slskd/Shares/ISharedFileCache.cs
index 73c9cd56..c59f5bba 100644
--- a/src/slskd/Shares/ISharedFileCache.cs
+++ b/src/slskd/Shares/ISharedFileCache.cs
@@ -35,8 +35,23 @@ namespace slskd.Shares
/// <summary>
/// Returns the contents of the cache.
/// </summary>
+ /// <param name="share">The optional share to which to limit the scope of the browse.</param>
/// <returns>The contents of the cache.</returns>
- IEnumerable<Directory> Browse();
+ IEnumerable<Directory> Browse(Share share = null);
+
+ /// <summary>
+ /// Returns the number of directories in the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share for which the directories are to be counted.</param>
+ /// <returns>The number of directories.</returns>
+ int CountDirectories(Share share);
+
+ /// <summary>
+ /// Returns the number of files in the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share for which the files are to be counted.</param>
+ /// <returns>The number of files.</returns>
+ int CountFiles(Share share);
/// <summary>
/// Scans the configured shares and fills the cache.
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs
index 17817b86..4307a98f 100644
--- a/src/slskd/Shares/ShareService.cs
+++ b/src/slskd/Shares/ShareService.cs
@@ -91,9 +91,9 @@ namespace slskd.Shares
/// Returns the entire contents of the share.
/// </summary>
/// <returns>The entire contents of the share.</returns>
- public Task<IEnumerable<Directory>> BrowseAsync()
+ public Task<IEnumerable<Directory>> BrowseAsync(Share share = null)
{
- var results = Cache.Browse();
+ var results = Cache.Browse(share);
var normalizedResults = results.Select(r => new Directory(r.Name.NormalizePath(), r.Files));
return Task.FromResult(normalizedResults);
@@ -170,6 +170,18 @@ namespace slskd.Shares
}
/// <summary>
+ /// Gets summary information for the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share to summarize.</param>
+ /// <returns>The summary information.</returns>
+ public Task<(int Directories, int Files)> SummarizeShareAsync(Share share)
+ {
+ var dirs = Cache.CountDirectories(share);
+ var files = Cache.CountFiles(share);
+ return Task.FromResult((dirs, files));
+ }
+
+ /// <summary>
/// Cancels the currently running scan, if one is running.
/// </summary>
/// <returns>A value indicating whether a scan was cancelled.</returns>
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs
index 17fc5fb5..a10202e2 100644
--- a/src/slskd/Shares/SharedFileCache.cs
+++ b/src/slskd/Shares/SharedFileCache.cs
@@ -25,6 +25,7 @@ namespace slskd.Shares
using System.Data;
using System.Diagnostics;
using System.Linq;
+ using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
@@ -68,41 +69,30 @@ namespace slskd.Shares
/// <summary>
/// Returns the contents of the cache.
/// </summary>
+ /// <param name="share">The optional share to which to limit the scope of the browse.</param>
/// <returns>The contents of the cache.</returns>
- public IEnumerable<Directory> Browse()
+ public IEnumerable<Directory> Browse(Share share = null)
{
var directories = new ConcurrentDictionary<string, Directory>();
+ string prefix = null;
+
+ if (share != null)
+ {
+ prefix = share.RemotePath + Path.DirectorySeparatorChar;
+ }
+
// Soulseek requires that each directory in the tree have an entry in the list returned in a browse response. if
// missing, files that are nested within directories which contain only directories (no files) are displayed as being
// 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 ListDirectories())
+ foreach (var directory in ListDirectories(prefix))
{
directories.TryAdd(directory, new Directory(directory));
}
- var files = new List<File>();
-
- using var conn = GetConnection();
- using var cmd = new SqliteCommand("SELECT maskedFilename, code, size, extension, attributeJson FROM files ORDER BY maskedFilename ASC;", conn);
- var reader = cmd.ExecuteReader();
-
- while (reader.Read())
- {
- var filename = reader.GetString(0);
- var code = reader.GetInt32(1);
- var size = reader.GetInt64(2);
- var extension = reader.GetString(3);
- var attributeJson = reader.GetString(4);
-
- var attributeList = attributeJson.FromJson<List<FileAttribute>>();
-
- var file = new File(code, filename, size, extension, attributeList);
-
- files.Add(file);
- }
+ var files = ListFiles(prefix, includeFullPath: true);
var groups = files
.GroupBy(file => Path.GetDirectoryName(file.Filename))
@@ -127,6 +117,28 @@ namespace slskd.Shares
}
/// <summary>
+ /// Returns the number of directories in the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share for which the directories are to be counted.</param>
+ /// <returns>The number of directories.</returns>
+ public int CountDirectories(Share share)
+ {
+ var path = share.RemotePath + Path.DirectorySeparatorChar;
+ return CountDirectories(path);
+ }
+
+ /// <summary>
+ /// Returns the number of files in the specified <paramref name="share"/>.
+ /// </summary>
+ /// <param name="share">The share for which the files are to be counted.</param>
+ /// <returns>The number of files.</returns>
+ public int CountFiles(Share share)
+ {
+ var path = share.RemotePath + Path.DirectorySeparatorChar;
+ return CountFiles(path);
+ }
+
+ /// <summary>
/// Scans the configured shares and fills the cache.
/// </summary>
/// <param name="shares">The list of shares from which to fill the cache.</param>
@@ -623,22 +635,60 @@ namespace slskd.Shares
sourceConn.BackupDatabase(backupConn);
}
- private int CountDirectories()
+ private int CountDirectories(string prefix = null)
{
using var conn = GetConnection();
- using var cmd = new SqliteCommand("SELECT COUNT(*) FROM directories;", conn);
- var reader = cmd.ExecuteReader();
- reader.Read();
- return reader.GetInt32(0);
+
+ SqliteCommand cmd = default;
+
+ try
+ {
+ if (string.IsNullOrEmpty(prefix))
+ {
+ cmd = new SqliteCommand("SELECT COUNT(*) FROM directories;", conn);
+ }
+ else
+ {
+ cmd = new SqliteCommand("SELECT COUNT(*) FROM directories WHERE name LIKE @prefix || '%'", conn);
+ cmd.Parameters.AddWithValue("prefix", prefix);
+ }
+
+ var reader = cmd.ExecuteReader();
+ reader.Read();
+ return reader.GetInt32(0);
+ }
+ finally
+ {
+ cmd.Dispose();
+ }
}
- private int CountFiles()
+ private int CountFiles(string prefix = null)
{
using var conn = GetConnection();
- using var cmd = new SqliteCommand("SELECT COUNT(*) FROM files;", conn);
- var reader = cmd.ExecuteReader();
- reader.Read();
- return reader.GetInt32(0);
+
+ SqliteCommand cmd = default;
+
+ try
+ {
+ if (string.IsNullOrEmpty(prefix))
+ {
+ cmd = new SqliteCommand("SELECT COUNT(*) FROM files;", conn);
+ }
+ else
+ {
+ cmd = new SqliteCommand("SELECT COUNT(*) FROM files WHERE maskedFilename LIKE @prefix || '%'", conn);
+ cmd.Parameters.AddWithValue("prefix", prefix);
+ }
+
+ var reader = cmd.ExecuteReader();
+ reader.Read();
+ return reader.GetInt32(0);
+ }
+ finally
+ {
+ cmd.Dispose();
+ }
}
private SqliteConnection GetConnection(string connectionString = null)
@@ -710,24 +760,42 @@ namespace slskd.Shares
return reader.GetInt64(0);
}
- private IEnumerable<string> ListDirectories()
+ private IEnumerable<string> ListDirectories(string prefix = null)
{
var results = new List<string>();
using var conn = GetConnection();
- using var cmd = new SqliteCommand("SELECT name FROM directories ORDER BY name ASC;", conn);
- var reader = cmd.ExecuteReader();
+ SqliteCommand cmd = default;
- while (reader.Read())
+ try
{
- results.Add(reader.GetString(0));
- }
+ if (string.IsNullOrEmpty(prefix))
+ {
+ cmd = new SqliteCommand("SELECT name FROM directories ORDER BY name ASC;", conn);
+ }
+ else
+ {
+ cmd = new SqliteCommand("SELECT name from directories WHERE name LIKE @prefix || '%' ORDER BY name ASC;", conn);
+ cmd.Parameters.AddWithValue("prefix", prefix);
+ }
+
+ var reader = cmd.ExecuteReader();
+
+ while (reader.Read())
+ {
+ results.Add(reader.GetString(0));
+ }
- return results;
+ return results;
+ }
+ finally
+ {
+ cmd.Dispose();
+ }
}
- private IEnumerable<File> ListFiles(string directory = null)
+ private IEnumerable<File> ListFiles(string directory = null, bool includeFullPath = false)
{
var results = new List<File>();
@@ -758,7 +826,9 @@ namespace slskd.Shares
var attributeList = attributeJson.FromJson<List<FileAttribute>>();
- var file = new File(code, filename: Path.GetFileName(filename), size, extension, attributeList);
+ filename = includeFullPath ? filename : Path.GetFileName(filename);
+
+ var file = new File(code, filename, size, extension, attributeList);
results.Add(file);
}