From 00aeeb5426be630fc1145d2c816046f3f631f78f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 31 Aug 2021 21:08:23 -0500 Subject: use named connection string --- src/slskd/Application/SharedFileCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 63413f53..6837bbf9 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -344,7 +344,7 @@ namespace slskd SQLite.Dispose(); } - SQLite = new SqliteConnection("Data Source=:memory:"); + SQLite = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); SQLite.Open(); using var cmd = new SqliteCommand("CREATE VIRTUAL TABLE cache USING fts5(filename)", SQLite); -- cgit v1.2.3 From 64355088fffe7781a0b78e373514d0fdb4053464 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 31 Aug 2021 21:16:59 -0500 Subject: create a new sqlite connection for each search --- src/slskd/Application/SharedFileCache.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 6837bbf9..bcd808ce 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -319,8 +319,13 @@ namespace slskd try { - using var cmd = new SqliteCommand(sql, SQLite); var results = new List(); + + using var sqlite = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); + using var cmd = new SqliteCommand(sql, sqlite); + + sqlite.Open(); + var reader = await cmd.ExecuteReaderAsync(); while (reader.Read()) -- cgit v1.2.3 From 11c3f3153c1a37a32546b3f36a07cd4eda1f4268 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 31 Aug 2021 21:54:51 -0500 Subject: cache SQLite connections by thread id --- src/slskd/Application/SharedFileCache.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index bcd808ce..d0486efd 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -29,6 +29,7 @@ namespace slskd using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Sqlite; + using Microsoft.Extensions.Caching.Memory; using Serilog; using Soulseek; @@ -59,6 +60,7 @@ namespace slskd private List Shares { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); + private ConcurrentDictionary SQLiteConnections { get; } = new ConcurrentDictionary(); /// /// Returns the contents of the cache. @@ -319,11 +321,10 @@ namespace slskd try { - var results = new List(); - - using var sqlite = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); + var sqlite = SQLiteConnections.GetOrAdd(Thread.CurrentThread.ManagedThreadId, new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared")); using var cmd = new SqliteCommand(sql, sqlite); + var results = new List(); sqlite.Open(); var reader = await cmd.ExecuteReaderAsync(); -- cgit v1.2.3 From f564152d88a8ed2df72326c032c89889bdf211df Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 1 Sep 2021 08:58:03 -0500 Subject: create a new connection for each search --- src/slskd/Application/SharedFileCache.cs | 19 +++++++++++-------- src/slskd/Program.cs | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index d0486efd..e9ebeb66 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -29,7 +29,6 @@ namespace slskd using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Sqlite; - using Microsoft.Extensions.Caching.Memory; using Serilog; using Soulseek; @@ -60,7 +59,6 @@ namespace slskd private List Shares { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); - private ConcurrentDictionary SQLiteConnections { get; } = new ConcurrentDictionary(); /// /// Returns the contents of the cache. @@ -308,6 +306,12 @@ namespace slskd /// The matching files. public async Task> SearchAsync(SearchQuery query) { + // ignore requests while the cache is building + if (SyncRoot.CurrentCount == 0) + { + return Enumerable.Empty(); + } + string Clean(string str) => str.Replace("/", " ") .Replace("\\", " ") .Replace(":", " ") @@ -318,14 +322,13 @@ namespace slskd var exclusions = string.Join(" OR ", query.Exclusions.Select(exclusion => $"\"{Clean(exclusion)}\"")); var sql = $"SELECT * FROM cache WHERE cache MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}'"; + var results = new List(); try { - var sqlite = SQLiteConnections.GetOrAdd(Thread.CurrentThread.ManagedThreadId, new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared")); - using var cmd = new SqliteCommand(sql, sqlite); - - var results = new List(); - sqlite.Open(); + using var conn = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); + using var cmd = new SqliteCommand(sql, conn); + await conn.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(); @@ -338,7 +341,7 @@ namespace slskd } catch (Exception ex) { - Log.Error(ex, "Malformed query: {Query}", query); + Log.Warning(ex, "Failed to execute shared file query '{Query}': {Message}", query, ex.Message); return Enumerable.Empty(); } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4489487a..ca7635c3 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -61,7 +61,8 @@ namespace slskd /// /// The default configuration filename. /// - public static readonly string DefaultConfigurationFile = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.yml"); + //public static readonly string DefaultConfigurationFile = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.yml"); + public static readonly string DefaultConfigurationFile = @"C:\Users\JP.WHATNET\slskd.yml"; /// /// The default incomplete download directory. -- cgit v1.2.3 From 633bbe6f774fe6af4e5dace4b32bc7da91e5d75d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 2 Sep 2021 09:01:12 -0500 Subject: atomically obtain or reject semaphore --- src/slskd/Application/SharedFileCache.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index e9ebeb66..a469de22 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -107,19 +107,17 @@ namespace slskd /// The operation context. public async Task FillAsync() { - if (SyncRoot.CurrentCount == 0) + // obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running. + if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0)) { Log.Warning("Shared file scan rejected; scan is already in progress."); throw new ShareScanInProgressException("Shared files are already being scanned."); } - await Task.Yield(); - - await SyncRoot.WaitAsync(); - try { - // todo: don't do this, but rather build a new cache and then swap it in + await Task.Yield(); + ResetCache(); State.SetValue(state => state with { Filling = true, FillProgress = 0 }); -- cgit v1.2.3