From 6943397420b2fd304842db857f710e9d256a4917 Mon Sep 17 00:00:00 2001 From: Ionite Date: Wed, 6 Dec 2023 16:47:57 -0500 Subject: [PATCH 1/2] Switch to ConcurrentDictionary for LiteDBExtensions --- .../Extensions/LiteDBExtensions.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs b/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs index a40b4213..3377a2e3 100644 --- a/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs +++ b/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using System.Collections.Concurrent; +using System.Linq.Expressions; using System.Reflection; using LiteDB; using LiteDB.Async; @@ -8,7 +9,8 @@ namespace StabilityMatrix.Core.Extensions; // ReSharper disable once InconsistentNaming public static class LiteDBExtensions { - private static readonly Dictionary Mapper = new(); + private static readonly ConcurrentDictionary Mapper = + new(); public static void Register(Expression?>> exp, string? collection = null) { @@ -16,7 +18,7 @@ public static class LiteDBExtensions if (member == null) throw new ArgumentException("Expecting Member Expression"); BsonMapper.Global.Entity().DbRef(exp, collection); - Mapper.Add(typeof(T), (typeof(TU), member.Name, true)); + Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, true)); } public static void Register(Expression> exp, string? collection = null) @@ -25,12 +27,13 @@ public static class LiteDBExtensions if (member == null) throw new ArgumentException("Expecting Member Expression"); BsonMapper.Global.Entity().DbRef(exp, collection); - Mapper.Add(typeof(T), (typeof(TU), member.Name, false)); + Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, false)); } public static ILiteCollection? IncludeAll(this ILiteCollection col) { - if (!Mapper.ContainsKey(typeof(T))) return null; + if (!Mapper.ContainsKey(typeof(T))) + return null; var stringList = new List(); var key = typeof(T); @@ -45,12 +48,13 @@ public static class LiteDBExtensions flag = false; } - return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression) keySelector)); + return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector)); } - + public static ILiteCollectionAsync IncludeAll(this ILiteCollectionAsync col) { - if (!Mapper.ContainsKey(typeof(T))) return col; + if (!Mapper.ContainsKey(typeof(T))) + return col; var stringList = new List(); var key = typeof(T); @@ -65,6 +69,6 @@ public static class LiteDBExtensions flag = false; } - return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression) keySelector)); + return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector)); } } From 20d8a065f0a2a4e60cecde5715f511e4aa65c99a Mon Sep 17 00:00:00 2001 From: Ionite Date: Wed, 6 Dec 2023 16:53:55 -0500 Subject: [PATCH 2/2] Change database init to Lazy to be thread safe --- StabilityMatrix.Core/Database/LiteDbContext.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/StabilityMatrix.Core/Database/LiteDbContext.cs b/StabilityMatrix.Core/Database/LiteDbContext.cs index 2fad7570..429c092b 100644 --- a/StabilityMatrix.Core/Database/LiteDbContext.cs +++ b/StabilityMatrix.Core/Database/LiteDbContext.cs @@ -16,8 +16,8 @@ public class LiteDbContext : ILiteDbContext private readonly ISettingsManager settingsManager; private readonly DebugOptions debugOptions; - private LiteDatabaseAsync? database; - public LiteDatabaseAsync Database => database ??= CreateDatabase(); + private readonly Lazy lazyDatabase; + public LiteDatabaseAsync Database => lazyDatabase.Value; // Notification events public event EventHandler? CivitModelsChanged; @@ -46,6 +46,8 @@ public class LiteDbContext : ILiteDbContext this.logger = logger; this.settingsManager = settingsManager; this.debugOptions = debugOptions.Value; + + lazyDatabase = new Lazy(CreateDatabase); } private LiteDatabaseAsync CreateDatabase() @@ -164,11 +166,11 @@ public class LiteDbContext : ILiteDbContext public void Dispose() { - if (database is not null) + if (lazyDatabase.IsValueCreated) { try { - database.Dispose(); + Database.Dispose(); } catch (ObjectDisposedException) { } catch (ApplicationException) @@ -176,8 +178,6 @@ public class LiteDbContext : ILiteDbContext // Ignores a mutex error from library // https://stability-matrix.sentry.io/share/issue/5c62f37462444e7eab18cea314af231f/ } - - database = null; } GC.SuppressFinalize(this);