Browse Source

Merge pull request #392 from ionite34/db-thread-fix

pull/324/head
Ionite 12 months ago committed by GitHub
parent
commit
b957f6645a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      StabilityMatrix.Core/Database/LiteDbContext.cs
  2. 22
      StabilityMatrix.Core/Extensions/LiteDBExtensions.cs

12
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<LiteDatabaseAsync> 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<LiteDatabaseAsync>(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);

22
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<Type, (Type PropertyType, string MemberName, bool IsList)> Mapper = new();
private static readonly ConcurrentDictionary<Type, (Type PropertyType, string MemberName, bool IsList)> Mapper =
new();
public static void Register<T, TU>(Expression<Func<T, List<TU>?>> exp, string? collection = null)
{
@ -16,7 +18,7 @@ public static class LiteDBExtensions
if (member == null)
throw new ArgumentException("Expecting Member Expression");
BsonMapper.Global.Entity<T>().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<T, TU>(Expression<Func<T, TU?>> exp, string? collection = null)
@ -25,12 +27,13 @@ public static class LiteDBExtensions
if (member == null)
throw new ArgumentException("Expecting Member Expression");
BsonMapper.Global.Entity<T>().DbRef(exp, collection);
Mapper.Add(typeof(T), (typeof(TU), member.Name, false));
Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, false));
}
public static ILiteCollection<T>? IncludeAll<T>(this ILiteCollection<T> col)
{
if (!Mapper.ContainsKey(typeof(T))) return null;
if (!Mapper.ContainsKey(typeof(T)))
return null;
var stringList = new List<string>();
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<T> IncludeAll<T>(this ILiteCollectionAsync<T> col)
{
if (!Mapper.ContainsKey(typeof(T))) return col;
if (!Mapper.ContainsKey(typeof(T)))
return col;
var stringList = new List<string>();
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));
}
}

Loading…
Cancel
Save