Skip to content

Commit

Permalink
Add disposible to methods sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsales committed Nov 28, 2024
1 parent 59c1b3a commit d5033f4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 39 deletions.
29 changes: 27 additions & 2 deletions src/SSync.Client.LitebDB/Sync/Synchronize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}


}
}
98 changes: 61 additions & 37 deletions src/SSync.Server.LitebDB/Sync/SchemaCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -51,7 +52,7 @@ public async Task<List<object>> PullChangesAsync(SSyncParameter parameter, SSync
var result = new List<object>();

Log($"Start pull changes");
var steps = _builder.GetSteps();
var steps = _pullBuilder?.GetSteps();

if (steps is not null)
{
Expand Down Expand Up @@ -98,7 +99,7 @@ public async IAsyncEnumerable<object> PullStreamChanges(SSyncParameter parameter
_options = options;

Log($"Start pull changes delta");
var steps = _builder.GetSteps();
var steps = _pullBuilder?.GetSteps();

if (steps is not null)
{
Expand Down Expand Up @@ -213,21 +214,21 @@ private async Task<SchemaPullResult<TCollection>> CheckChanges<TCollection, TPar
DateTime lastPulledAt = paramenter.Timestamp;

var syncTimestamp = timestamp;

var changesOfTime = new SchemaPullResult<TCollection>(
paramenter.CurrentColletion,
syncTimestamp,
new SchemaPullResult<TCollection>.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(),

Expand All @@ -252,39 +253,40 @@ private async Task<DateTime> 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<string, SchemaPush<ISchema>>();
var changesMap = new Dictionary<string, SchemaPush<ISchema>>();

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<bool>?)genericMethodParseChanges.Invoke(this, [changeObj, parameter]) ?? throw new PushChangeException("task of method not found");
var task = (Task<bool>?)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");
}
}
}
}
Expand All @@ -311,7 +313,7 @@ private async Task<bool> ParseChanges<TSchema>(JsonNode nodeChange, SSyncParamet
var schemaPush = JsonSerializer.Deserialize<SchemaPush<TSchema>>(jsonChange, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

var result = true;
Expand All @@ -324,7 +326,7 @@ private async Task<bool> ParseChanges<TSchema>(JsonNode nodeChange, SSyncParamet

var requestHandler = _syncServices.PushRequestHandler<TSchema>();

var lastPulledAtSync = DateTimeExtension.ParseDaTimeFromConfig(parameter.Timestamp,_options?.TimeConfig);
var lastPulledAtSync = DateTimeExtension.ParseDaTimeFromConfig(parameter.Timestamp, _options?.TimeConfig);

if (schemaPush.Changes.Created.Any())
{
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit d5033f4

Please sign in to comment.