Put database context outside of worker. Reduces memory usage by quite a lot.

This commit is contained in:
2025-07-24 04:26:56 +01:00
parent 134f8c7183
commit 2d54cbdf23
2 changed files with 93 additions and 69 deletions

View File

@@ -22,14 +22,18 @@ using CompressionMode = SharpCompress.Compressors.CompressionMode;
namespace RomRepoMgr.Core.Workers;
public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
public sealed class FileImporter
(
Context _ctx,
ConcurrentBag<DbFile> _newFiles,
ConcurrentBag<DbDisk> _newDisks,
ConcurrentBag<DbMedia> _newMedias,
bool onlyKnown,
bool deleteAfterImport
)
{
const long BUFFER_SIZE = 131072;
static readonly Lock DbLock = new();
readonly Context _ctx = Context.Create(Settings.Settings.Current.DatabasePath);
readonly List<DbDisk> _newDisks = [];
readonly List<DbFile> _newFiles = [];
readonly List<DbMedia> _newMedias = [];
readonly Dictionary<string, DbDisk> _pendingDisksByMd5 = [];
readonly Dictionary<string, DbDisk> _pendingDisksBySha1 = [];
readonly Dictionary<string, DbFile> _pendingFiles = [];
@@ -1298,9 +1302,9 @@ public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
public void SaveChanges()
{
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);
SetMessage2?.Invoke(this,
SetMessage?.Invoke(this,
new MessageEventArgs
{
Message = Localization.SavingChangesToDatabase

View File

@@ -12,6 +12,8 @@ using Avalonia.Threading;
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database;
using RomRepoMgr.Database.Models;
using RomRepoMgr.Models;
using RomRepoMgr.Resources;
@@ -19,6 +21,11 @@ namespace RomRepoMgr.ViewModels;
public class ImportRomFolderViewModel : ViewModelBase
{
readonly Context _ctx = Context.Create(Settings.Settings.Current.DatabasePath);
readonly ConcurrentBag<DbDisk> _newDisks = [];
readonly ConcurrentBag<DbFile> _newFiles = [];
readonly ConcurrentBag<DbMedia> _newMedias = [];
readonly Stopwatch _stopwatch = new();
bool _canClose;
bool _canStart;
string _folderPath;
@@ -43,7 +50,7 @@ public class ImportRomFolderViewModel : ViewModelBase
string _statusMessage;
string _statusMessage2;
bool _statusMessage2Visible;
readonly Stopwatch _stopwatch = new();
public ImportRomFolderViewModel()
{
@@ -207,7 +214,7 @@ public class ImportRomFolderViewModel : ViewModelBase
void Start()
{
_rootImporter = new FileImporter(KnownOnlyChecked, RemoveFilesChecked);
_rootImporter = new FileImporter(_ctx, _newFiles, _newDisks, _newMedias, KnownOnlyChecked, RemoveFilesChecked);
_rootImporter.SetMessage += SetMessage;
_rootImporter.SetIndeterminateProgress += SetIndeterminateProgress;
_rootImporter.SetProgress += SetProgress;
@@ -310,7 +317,13 @@ public class ImportRomFolderViewModel : ViewModelBase
Indeterminate = true
};
var worker = new FileImporter(KnownOnlyChecked, RemoveFilesChecked);
var worker = new FileImporter(_ctx,
_newFiles,
_newDisks,
_newMedias,
KnownOnlyChecked,
RemoveFilesChecked);
worker.SetIndeterminateProgress2 += model.OnSetIndeterminateProgress;
worker.SetMessage2 += model.OnSetMessage;
worker.SetProgress2 += model.OnSetProgress;
@@ -322,13 +335,14 @@ public class ImportRomFolderViewModel : ViewModelBase
worker.ImportFile(file);
worker.SaveChanges();
Interlocked.Increment(ref _listPosition);
});
_stopwatch.Stop();
Console.WriteLine("Took " + _stopwatch.Elapsed.TotalSeconds + " seconds to process files.");
_rootImporter.SaveChanges();
_rootImporter.UpdateRomStats();
_listPosition = 0;
@@ -351,8 +365,8 @@ public class ImportRomFolderViewModel : ViewModelBase
ProgressMinimum = 0;
ProgressValue = 0;
ProgressIsIndeterminate = false;
Progress2Visible = true;
StatusMessage2Visible = true;
Progress2Visible = false;
StatusMessage2Visible = false;
_listPosition = 0;
_stopwatch.Restart();
@@ -366,12 +380,12 @@ public class ImportRomFolderViewModel : ViewModelBase
});
// Create FileImporter
var archiveImporter = new FileImporter(KnownOnlyChecked, RemoveFilesChecked);
archiveImporter.SetIndeterminateProgress2 += SetIndeterminateProgress2;
archiveImporter.SetMessage2 += SetMessage2;
archiveImporter.SetProgress2 += SetProgress2;
archiveImporter.SetProgressBounds2 += SetProgress2Bounds;
var archiveImporter = new FileImporter(_ctx,
_newFiles,
_newDisks,
_newMedias,
KnownOnlyChecked,
RemoveFilesChecked);
// Extract archive
bool ret = archiveImporter.ExtractArchive(archive);
@@ -387,7 +401,13 @@ public class ImportRomFolderViewModel : ViewModelBase
Indeterminate = true
};
var worker = new FileImporter(KnownOnlyChecked, RemoveFilesChecked);
var worker = new FileImporter(_ctx,
_newFiles,
_newDisks,
_newMedias,
KnownOnlyChecked,
RemoveFilesChecked);
worker.SetIndeterminateProgress2 += model.OnSetIndeterminateProgress;
worker.SetMessage2 += model.OnSetMessage;
worker.SetProgress2 += model.OnSetProgress;