From d5033f435d077f2f08da9be06539781e6d4738a7 Mon Sep 17 00:00:00 2001 From: Gabriel Sales Date: Thu, 28 Nov 2024 15:19:07 -0300 Subject: [PATCH] Add disposible to methods sync --- src/SSync.Client.LitebDB/Sync/Synchronize.cs | 29 +++++- .../Sync/SchemaCollection.cs | 98 ++++++++++++------- 2 files changed, 88 insertions(+), 39 deletions(-) diff --git a/src/SSync.Client.LitebDB/Sync/Synchronize.cs b/src/SSync.Client.LitebDB/Sync/Synchronize.cs index 0f28d3b..9636c27 100644 --- a/src/SSync.Client.LitebDB/Sync/Synchronize.cs +++ b/src/SSync.Client.LitebDB/Sync/Synchronize.cs @@ -4,14 +4,16 @@ using SSync.Client.LitebDB.Exceptions; using SSync.Client.LitebDB.Extensions; using SSync.Client.LitebDB.Poco; +using System.Runtime.InteropServices; using System.Text; namespace SSync.Client.LitebDB.Sync { - public class Synchronize : ISynchronize + public class Synchronize : ISynchronize, IDisposable { private readonly LiteDatabase _db; - private readonly SynchronizeOptions? _options; + private SynchronizeOptions? _options; + private bool _disposedValue; public Synchronize(LiteDatabase db, SynchronizeOptions? options = null) { @@ -447,5 +449,28 @@ private void Log(object logMessage, string title = "log.txt") } } } + + public void Dispose() + { + Dispose(true); + + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + _db.Dispose(); + _options = null; + } + + _disposedValue = true; + } + } + + } } \ No newline at end of file diff --git a/src/SSync.Server.LitebDB/Sync/SchemaCollection.cs b/src/SSync.Server.LitebDB/Sync/SchemaCollection.cs index bfd0517..cbc7fd3 100644 --- a/src/SSync.Server.LitebDB/Sync/SchemaCollection.cs +++ b/src/SSync.Server.LitebDB/Sync/SchemaCollection.cs @@ -15,24 +15,25 @@ namespace SSync.Server.LitebDB.Sync { - public class SchemaCollection : ISchemaCollection + public class SchemaCollection : ISchemaCollection, IDisposable { private readonly ISSyncServices _syncServices; - private readonly IPullExecutionOrderStep _builder; + private readonly ISSyncDbContextTransaction _sSyncDbContextTransaction; + private IPullExecutionOrderStep? _pullBuilder; //TODO: test: USE SINGLETON - private readonly IPushExecutionOrderStep _pushBuilder; + private IPushExecutionOrderStep? _pushBuilder; - private readonly ISSyncDbContextTransaction _sSyncDbContextTransaction; private SSyncOptions? _options = null; + private bool _disposedValue; public SchemaCollection(ISSyncServices syncServices, - IPullExecutionOrderStep builder, + IPullExecutionOrderStep pullBuilder, IPushExecutionOrderStep pushBuilder, ISSyncDbContextTransaction sSyncDbContextTransaction) { _syncServices = syncServices; - _builder = builder; + _pullBuilder = pullBuilder; _pushBuilder = pushBuilder; _sSyncDbContextTransaction = sSyncDbContextTransaction; } @@ -51,7 +52,7 @@ public async Task> PullChangesAsync(SSyncParameter parameter, SSync var result = new List(); Log($"Start pull changes"); - var steps = _builder.GetSteps(); + var steps = _pullBuilder?.GetSteps(); if (steps is not null) { @@ -98,7 +99,7 @@ public async IAsyncEnumerable PullStreamChanges(SSyncParameter parameter _options = options; Log($"Start pull changes delta"); - var steps = _builder.GetSteps(); + var steps = _pullBuilder?.GetSteps(); if (steps is not null) { @@ -213,21 +214,21 @@ private async Task> CheckChanges( paramenter.CurrentColletion, syncTimestamp, new SchemaPullResult.Change( created: query - .Where(d => d.CreatedAt >= lastPulledAt) - .Where(d => d.UpdatedAt == d.CreatedAt) + .Where(d => d.CreatedAt >= lastPulledAt) + .Where(d => d.UpdatedAt == d.CreatedAt) .Where(d => !d.DeletedAt.HasValue) .ToList(), updated: query - .Where(d => d.UpdatedAt >= lastPulledAt) - .Where(d => d.UpdatedAt != d.CreatedAt) + .Where(d => d.UpdatedAt >= lastPulledAt) + .Where(d => d.UpdatedAt != d.CreatedAt) .Where(d => !d.DeletedAt.HasValue) .ToList(), @@ -252,39 +253,40 @@ private async Task ExecuteChanges(JsonArray changes, SSyncParameter pa ArgumentNullException.ThrowIfNull(changes); - var schemasSync = _pushBuilder.GetSchemas(); + var schemasSync = _pushBuilder?.GetSchemas(); - var collectionOrder = _pushBuilder.GetCollectionOrder(); - - var parseChangesMethod = GetType().GetMethod(nameof(ParseChanges), BindingFlags.Instance | BindingFlags.NonPublic); + if (schemasSync is not null) + { + var parseChangesMethod = GetType().GetMethod(nameof(ParseChanges), BindingFlags.Instance | BindingFlags.NonPublic); - ArgumentNullException.ThrowIfNull(parseChangesMethod); + ArgumentNullException.ThrowIfNull(parseChangesMethod); - var changesMap = new Dictionary>(); + var changesMap = new Dictionary>(); - foreach (var changeObj in changes) - { - if (changeObj is null) + foreach (var changeObj in changes) { - return DateTime.MinValue; - } - var collectionName = changeObj!["Collection"]?.ToString() ?? changeObj["collection"]?.ToString(); + if (changeObj is null) + { + return DateTime.MinValue; + } + var collectionName = changeObj!["Collection"]?.ToString() ?? changeObj["collection"]?.ToString(); - Log($"Start {collectionName}"); + Log($"Start {collectionName}"); - if (!string.IsNullOrEmpty(collectionName) && schemasSync.TryGetValue(collectionName, out var schemaType)) - { - var genericMethodParseChanges = parseChangesMethod.MakeGenericMethod(schemaType); + if (!string.IsNullOrEmpty(collectionName) && schemasSync.TryGetValue(collectionName, out var schemaType)) + { + var genericMethodParseChanges = parseChangesMethod.MakeGenericMethod(schemaType); - var task = (Task?)genericMethodParseChanges.Invoke(this, [changeObj, parameter]) ?? throw new PushChangeException("task of method not found"); + var task = (Task?)genericMethodParseChanges.Invoke(this, [changeObj, parameter]) ?? throw new PushChangeException("task of method not found"); - if (!await task) - { - Log($"Error in parse change of collection {collectionName} to type {schemaType.Name}", consoleColor: ConsoleColor.Red); + if (!await task) + { + Log($"Error in parse change of collection {collectionName} to type {schemaType.Name}", consoleColor: ConsoleColor.Red); - Log($"Not push changes", consoleColor: ConsoleColor.Red); + Log($"Not push changes", consoleColor: ConsoleColor.Red); - // throw new PushChangeException("Not push changes"); + // throw new PushChangeException("Not push changes"); + } } } } @@ -311,7 +313,7 @@ private async Task ParseChanges(JsonNode nodeChange, SSyncParamet var schemaPush = JsonSerializer.Deserialize>(jsonChange, new JsonSerializerOptions { PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase + PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); var result = true; @@ -324,7 +326,7 @@ private async Task ParseChanges(JsonNode nodeChange, SSyncParamet var requestHandler = _syncServices.PushRequestHandler(); - var lastPulledAtSync = DateTimeExtension.ParseDaTimeFromConfig(parameter.Timestamp,_options?.TimeConfig); + var lastPulledAtSync = DateTimeExtension.ParseDaTimeFromConfig(parameter.Timestamp, _options?.TimeConfig); if (schemaPush.Changes.Created.Any()) { @@ -383,5 +385,27 @@ private void Log(object logMessage, string title = "log.txt", ConsoleColor conso } } } + + public void Dispose() + { + Dispose(true); + + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + _pullBuilder = null; + _pushBuilder = null; + _options = null; + } + + _disposedValue = true; + } + } } } \ No newline at end of file