Skip to content

Commit

Permalink
Merge pull request Mimetis#220 from Mimetis/Mimetis-Core30-Compatibility
Browse files Browse the repository at this point in the history
Fix .Net Core30 Compatibility issues
  • Loading branch information
Mimetis committed Feb 19, 2020
2 parents 503f2f2 + e6f7f68 commit 43ec12c
Show file tree
Hide file tree
Showing 51 changed files with 496 additions and 621 deletions.
20 changes: 11 additions & 9 deletions Projects/Dotmim.Sync.Core/Batch/BatchInfo.cs
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace Dotmim.Sync.Batch
{
Expand Down Expand Up @@ -101,7 +102,7 @@ public string GetDirectoryFullPath()
/// <summary>
/// Check if this batchinfo has some data (in memory or not)
/// </summary>
public bool HasData()
public async Task<bool> HasDataAsync()
{
if (InMemory && InMemoryData != null && InMemoryData.HasTables && InMemoryData.HasRows)
return true;
Expand All @@ -110,7 +111,7 @@ public bool HasData()
{
foreach (var bpi in BatchPartsInfo)
{
bpi.LoadBatch(schema, GetDirectoryFullPath());
await bpi.LoadBatchAsync(schema, GetDirectoryFullPath()).ConfigureAwait(false);

var hasData = bpi.Data.HasRows;

Expand Down Expand Up @@ -144,8 +145,7 @@ public IEnumerable<SyncTable> GetTable(string tableName, string schemaName)
{
if (batchPartinInfo.Tables != null && batchPartinInfo.Tables.Any(t => t == tableInfo))
{
batchPartinInfo.LoadBatch(schema, GetDirectoryFullPath());

batchPartinInfo.LoadBatchAsync(schema, GetDirectoryFullPath()).ConfigureAwait(false).GetAwaiter().GetResult();

// Get the table from the batchPartInfo
// generate a tmp SyncTable for
Expand Down Expand Up @@ -189,7 +189,7 @@ public void EnsureLastBatch()
/// <summary>
/// Add changes to batch info.
/// </summary>
public void AddChanges(SyncSet changes, int batchIndex = 0, bool isLastBatch = true)
public async Task AddChangesAsync(SyncSet changes, int batchIndex = 0, bool isLastBatch = true)
{
if (this.InMemory)
{
Expand All @@ -199,7 +199,7 @@ public void AddChanges(SyncSet changes, int batchIndex = 0, bool isLastBatch = t
{
var bpId = this.GenerateNewFileName(batchIndex.ToString());
//var fileName = Path.Combine(this.GetDirectoryFullPath(), bpId);
var bpi = BatchPartInfo.CreateBatchPartInfo(batchIndex, changes, bpId, GetDirectoryFullPath(), isLastBatch);
var bpi = await BatchPartInfo.CreateBatchPartInfoAsync(batchIndex, changes, bpId, GetDirectoryFullPath(), isLastBatch).ConfigureAwait(false);

// add the batchpartinfo tp the current batchinfo
this.BatchPartsInfo.Add(bpi);
Expand All @@ -210,13 +210,15 @@ public void AddChanges(SyncSet changes, int batchIndex = 0, bool isLastBatch = t
/// <summary>
/// generate a batch file name
/// </summary>
private string GenerateNewFileName(string batchIndex)
internal string GenerateNewFileName(string batchIndex)
{
if (batchIndex.Length == 1)
batchIndex = $"00{batchIndex}";
batchIndex = $"000{batchIndex}";
else if (batchIndex.Length == 2)
batchIndex = $"0{batchIndex}";
batchIndex = $"00{batchIndex}";
else if (batchIndex.Length == 3)
batchIndex = $"0{batchIndex}";
else if (batchIndex.Length == 4)
batchIndex = $"{batchIndex}";
else
throw new OverflowException("too much batches !!!");
Expand Down
39 changes: 31 additions & 8 deletions Projects/Dotmim.Sync.Core/Batch/BatchPartInfo.cs
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Dotmim.Sync.Batch
{
Expand All @@ -23,7 +24,7 @@ public class BatchPartInfo
/// <summary>
/// Loads the batch file and import the rows in a SyncSet instance
/// </summary>
public void LoadBatch(SyncSet schema, string directoryFullPath)
public async Task LoadBatchAsync(SyncSet schema, string directoryFullPath)
{

if (string.IsNullOrEmpty(this.FileName))
Expand All @@ -33,7 +34,7 @@ public void LoadBatch(SyncSet schema, string directoryFullPath)
var set = schema.Clone();

// Get a Batch part, and deserialise the file into a the BatchPartInfo Set property
var data = Deserialize(this.FileName, directoryFullPath);
var data = await DeserializeAsync(this.FileName, directoryFullPath);

// Import data in a typed Set
set.ImportContainerSet(data, true);
Expand Down Expand Up @@ -77,7 +78,7 @@ public BatchPartInfo()
}


private static ContainerSet Deserialize(string fileName, string directoryFullPath)
private static async Task<ContainerSet> DeserializeAsync(string fileName, string directoryFullPath)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(fileName);
Expand All @@ -92,14 +93,14 @@ private static ContainerSet Deserialize(string fileName, string directoryFullPat
var jsonConverter = new JsonConverter<ContainerSet>();
using (var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
return jsonConverter.Deserialize(fs);
return await jsonConverter.DeserializeAsync(fs);
}
}

/// <summary>
/// Serialize a container set instance
/// </summary>
private static void Serialize(ContainerSet set, string fileName, string directoryFullPath)
internal static async Task SerializeAsync(ContainerSet set, string fileName, string directoryFullPath)
{
if (set == null)
return;
Expand All @@ -116,23 +117,45 @@ private static void Serialize(ContainerSet set, string fileName, string director

using (var f = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite))
{
var bytes = jsonConverter.Serialize(set);
var bytes = await jsonConverter.SerializeAsync(set);
f.Write(bytes, 0, bytes.Length);
}
}


/// <summary>
/// Create a new BPI, and serialize the changeset if not in memory
/// </summary>
internal static BatchPartInfo CreateBatchPartInfo2(int batchIndex, SyncSet set, string fileName, bool isLastBatch)
{
BatchPartInfo bpi = null;

// Create a batch part
// The batch part creation process will serialize the changesSet to the disk
bpi = new BatchPartInfo { FileName = fileName };

bpi.Index = batchIndex;
bpi.IsLastBatch = isLastBatch;

// Even if the set is empty (serialized on disk), we should retain the tables names
if (set != null)
bpi.Tables = set.Tables.Select(t => new BatchPartTableInfo(t.TableName, t.SchemaName)).ToArray();

return bpi;
}

/// <summary>
/// Create a new BPI, and serialize the changeset if not in memory
/// </summary>
internal static BatchPartInfo CreateBatchPartInfo(int batchIndex, SyncSet set, string fileName, string directoryFullPath, bool isLastBatch)
internal static async Task<BatchPartInfo> CreateBatchPartInfoAsync(int batchIndex, SyncSet set, string fileName, string directoryFullPath, bool isLastBatch)
{
BatchPartInfo bpi = null;

// Create a batch part
// The batch part creation process will serialize the changesSet to the disk

// Serialize the file !
Serialize(set.GetContainerSet(), fileName, directoryFullPath);
await SerializeAsync(set.GetContainerSet(), fileName, directoryFullPath);

bpi = new BatchPartInfo { FileName = fileName };

Expand Down
8 changes: 4 additions & 4 deletions Projects/Dotmim.Sync.Core/CoreProvider.ApplyChanges.cs
Expand Up @@ -24,9 +24,9 @@ public abstract partial class CoreProvider
var changeApplicationAction = ChangeApplicationAction.Continue;
var changesApplied = new DatabaseChangesApplied();


var hasChanges = await message.Changes.HasDataAsync();
// Check if we have some data available
if (!message.Changes.HasData())
if (!hasChanges)
return (context, changesApplied);

context.SyncStage = SyncStage.DatabaseChangesApplying;
Expand Down Expand Up @@ -151,9 +151,9 @@ private ChangeApplicationAction ResetInternal(SyncContext context, SyncSet schem
var syncAdapter = builder.CreateSyncAdapter(connection, transaction);
syncAdapter.ApplyType = applyType;


var hasChanges = await message.Changes.HasDataAsync();
// Each table in the messages contains scope columns. Don't forget it
if (message.Changes.HasData())
if (hasChanges)
{
// getting the table to be applied
// we may have multiple batch files, so we can have multipe sync tables with the same name
Expand Down
10 changes: 5 additions & 5 deletions Projects/Dotmim.Sync.Core/CoreProvider.GetChanges.cs
Expand Up @@ -37,7 +37,7 @@ public abstract partial class CoreProvider

if (context.SyncWay == SyncWay.Upload && context.SyncType == SyncType.Reinitialize)
{
(batchInfo, changesSelected) = this.GetEmptyChanges(message);
(batchInfo, changesSelected) = await this.GetEmptyChangesAsync(message);
return (context, batchInfo, changesSelected);
}

Expand Down Expand Up @@ -154,7 +154,7 @@ public abstract partial class CoreProvider
continue;

// add changes to batchinfo
batchInfo.AddChanges(changesSet, batchIndex, false);
await batchInfo.AddChangesAsync(changesSet, batchIndex, false);

// increment batch index
batchIndex++;
Expand Down Expand Up @@ -192,7 +192,7 @@ public abstract partial class CoreProvider
// We are in batch mode, and we are at the last batchpart info
// Even if we don't have rows inside, we return the changesSet, since it contains at leaset schema
if (changesSet != null && changesSet.HasTables)
batchInfo.AddChanges(changesSet, batchIndex, true);
await batchInfo.AddChangesAsync(changesSet, batchIndex, true).ConfigureAwait(false);

// Check the last index as the last batch
batchInfo.EnsureLastBatch();
Expand All @@ -205,7 +205,7 @@ public abstract partial class CoreProvider
/// <summary>
/// Generate an empty BatchInfo
/// </summary>
internal (BatchInfo, DatabaseChangesSelected) GetEmptyChanges(MessageGetChangesBatch message)
internal async Task<(BatchInfo, DatabaseChangesSelected)> GetEmptyChangesAsync(MessageGetChangesBatch message)
{
// Get config
var isBatched = message.BatchSize > 0;
Expand All @@ -221,7 +221,7 @@ public abstract partial class CoreProvider
var batchInfo = new BatchInfo(!isBatched, changesSet, message.BatchDirectory); ;

// add changes to batchInfo
batchInfo.AddChanges(new SyncSet());
await batchInfo.AddChangesAsync(new SyncSet()).ConfigureAwait(false);

// Create a new empty in-memory batch info
return (batchInfo, new DatabaseChangesSelected());
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/CoreProvider.Metadatas.cs
Expand Up @@ -29,7 +29,7 @@ public abstract partial class CoreProvider
// Delete metadatas
syncAdapter.DeleteMetadatas(timestampLimit);
}
return context;
return await Task.FromResult(context).ConfigureAwait(false);
}

}
Expand Down
10 changes: 5 additions & 5 deletions Projects/Dotmim.Sync.Core/CoreProvider.Snapshot.cs
Expand Up @@ -116,7 +116,7 @@ public abstract partial class CoreProvider
continue;

// add changes to batchinfo
batchInfo.AddChanges(changesSet, batchIndex, false);
await batchInfo.AddChangesAsync(changesSet, batchIndex, false);

// increment batch index
batchIndex++;
Expand All @@ -139,7 +139,7 @@ public abstract partial class CoreProvider


if (changesSet != null && changesSet.HasTables)
batchInfo.AddChanges(changesSet, batchIndex, true);
await batchInfo.AddChangesAsync(changesSet, batchIndex, true);

// Check the last index as the last batch
batchInfo.EnsureLastBatch();
Expand All @@ -153,7 +153,7 @@ public abstract partial class CoreProvider

using (var f = new FileStream(summaryFileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
var bytes = jsonConverter.Serialize(batchInfo);
var bytes = await jsonConverter.SerializeAsync(batchInfo);
f.Write(bytes, 0, bytes.Length);
}

Expand All @@ -167,7 +167,7 @@ public abstract partial class CoreProvider
/// destination knowledge, and change data retriever parameters.
/// </summary>
/// <returns>A DbSyncContext object that will be used to retrieve the modified data.</returns>
public virtual (SyncContext, BatchInfo) GetSnapshot(
public virtual async Task<(SyncContext, BatchInfo)> GetSnapshotAsync(
SyncContext context, SyncSet schema, string batchDirectory,
CancellationToken cancellationToken, IProgress<ProgressArgs> progress = null)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public abstract partial class CoreProvider

using (var fs = new FileStream(summaryFileName, FileMode.Open, FileAccess.Read))
{
batchInfo = jsonConverter.Deserialize(fs);
batchInfo = await jsonConverter.DeserializeAsync(fs).ConfigureAwait(false);
}

batchInfo.SetSchema(changesSet);
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/CoreProvider.cs
Expand Up @@ -59,7 +59,7 @@ public abstract partial class CoreProvider
/// Returns the Task associated with given type of BaseArgs
/// Because we are not doing anything else than just returning a task, no need to use async / await. Just return the Task itself
/// </summary>
public Task InterceptAsync<T>(T args) where T : ProgressArgs
public Task InterceptAsync<T>(T args) where T : ProgressArgs
{
if (this.interceptors == null)
return Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/Dotmim.Sync.Core.csproj
Expand Up @@ -11,7 +11,7 @@
<PackageTags>Data, Database, Sync, Synchronization, Framework, Sql, MySql, Sqlite, Dotmim.Sync, NetStandard, SyncFramework</PackageTags>

<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>0.4.2</Version>
<Version>0.4.3</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>

<LangVersion>7.3</LangVersion>
Expand Down
3 changes: 3 additions & 0 deletions Projects/Dotmim.Sync.Core/Interceptors/ChangesArgs.cs
Expand Up @@ -146,6 +146,9 @@ public TableChangesApplyingArgs(SyncContext context, IEnumerable<SyncRow> rows,

}




/// <summary>
/// All table changes applied on a provider
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions Projects/Dotmim.Sync.Core/Interceptors/InterceptorExtensions.cs
Expand Up @@ -74,6 +74,18 @@ public static void OnOutdated(this IOrchestrator orchestrator, Action<OutdatedAr
=> orchestrator.SetInterceptor(action);


/// <summary>
/// Occurs just before saving a serialized set to disk
/// </summary>
public static void OnSerializingSet(this IOrchestrator orchestrator, Func<SerializingSetArgs, Task> func)
=> orchestrator.SetInterceptor(func);

/// <summary>
/// Occurs just before saving a serialized set to disk
/// </summary>
public static void OnSerializingSet(this IOrchestrator orchestrator, Action<SerializingSetArgs> action)
=> orchestrator.SetInterceptor(action);

/// <summary>
/// Intercept the provider when an apply change is failing
/// </summary>
Expand Down
40 changes: 40 additions & 0 deletions Projects/Dotmim.Sync.Core/Interceptors/SerializationArgs.cs
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;

namespace Dotmim.Sync
{
/// <summary>
/// Raise before serialize a change set to get a byte array
/// </summary>
public class SerializingSetArgs : ProgressArgs
{
public SerializingSetArgs(SyncContext context, ContainerSet set, DbConnection connection, DbTransaction transaction)
: base(context, connection, transaction)
{
this.Set = set;
}

/// <summary>
/// Gets the data that will be serialized on disk
/// </summary>
public byte[] Data { get; set; }

/// <summary>
/// Set to serialize
/// </summary>
public ContainerSet Set { get; }
}


/// <summary>
/// Raise just after loading a binary change set from disk, just before calling the deserializer
/// </summary>
public class DeserializingSetArgs : SerializingSetArgs
{
public DeserializingSetArgs(SyncContext context, ContainerSet set, DbConnection connection, DbTransaction transaction) : base(context, set, connection, transaction)
{
}
}
}

0 comments on commit 43ec12c

Please sign in to comment.