Skip to content

Commit

Permalink
clean up from progress (#78)
Browse files Browse the repository at this point in the history
* progress intermediate commit

* add progress for download

* remove unused code

* remove batch sent callbacks

* multi-threaded deserialize works

* Progress for download and deserialization

* Fix tests

* Have less indeterminate deserialization

* fix deserialization

* make download faster with buffered stream

* put local receive back

* remove unused callback

* fmt

* Progress for serialization and upload

* fix uploading

* clean up from progress

* merge fixes and fmt
  • Loading branch information
adamhathcock authored Aug 22, 2024
1 parent 0f116ad commit 4c3e572
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 166 deletions.
2 changes: 1 addition & 1 deletion src/Speckle.Sdk/Models/Blob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public string GetFileHash()
{
if ((_isHashExpired || _hash == null) && filePath != null)
{
_hash = Utilities.HashFile(filePath);
_hash = HashUtility.HashFile(filePath);
}

return _hash;
Expand Down
26 changes: 26 additions & 0 deletions src/Speckle.Sdk/Models/HashUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;

namespace Speckle.Sdk.Models;

public static class HashUtility
{
public enum HashingFunctions
{
SHA256,
MD5
}

public const int HASH_LENGTH = 32;

[SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms")]
public static string HashFile(string filePath, HashingFunctions func = HashingFunctions.SHA256)
{
using HashAlgorithm hashAlgorithm = func == HashingFunctions.MD5 ? MD5.Create() : SHA256.Create();

using var stream = File.OpenRead(filePath);

var hash = hashAlgorithm.ComputeHash(stream);
return BitConverter.ToString(hash, 0, HASH_LENGTH).Replace("-", "").ToLowerInvariant();
}
}
61 changes: 0 additions & 61 deletions src/Speckle.Sdk/Models/Utilities.cs

This file was deleted.

13 changes: 6 additions & 7 deletions src/Speckle.Sdk/Serialisation/BaseObjectDeserializerV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Speckle.Sdk.Host;
using Speckle.Sdk.Logging;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation.SerializationUtilities;
using Speckle.Sdk.Serialisation.Utilities;
using Speckle.Sdk.Transports;

namespace Speckle.Sdk.Serialisation;
Expand All @@ -15,6 +15,7 @@ public sealed class BaseObjectDeserializerV2
{
private bool _isBusy;
private readonly object _callbackLock = new();
private readonly object?[] _invokeNull = [null];

// id -> Base if already deserialized or id -> Task<object> if was handled by a bg thread
private Dictionary<string, object?>? _deserializedObjects;
Expand All @@ -37,9 +38,7 @@ public sealed class BaseObjectDeserializerV2

public string? BlobStorageFolder { get; set; }
public TimeSpan Elapsed { get; private set; }

public static int DefaultNumberThreads => Math.Min(Environment.ProcessorCount, 6); //6 threads seems the sweet spot, see performance test project
public int WorkerThreadCount { get; set; } = DefaultNumberThreads;
public int WorkerThreadCount { get; set; } = Math.Min(Environment.ProcessorCount, 6); //6 threads seems the sweet spot, see performance test project;

/// <param name="rootObjectJson">The JSON string of the object to be deserialized <see cref="Base"/></param>
/// <returns>A <see cref="Base"/> typed object deserialized from the <paramref name="rootObjectJson"/></returns>
Expand Down Expand Up @@ -318,7 +317,7 @@ private Base Dict2Base(Dictionary<string, object?> dictObj)
dictObj.Remove(TYPE_DISCRIMINATOR);
dictObj.Remove("__closure");

var staticProperties = BaseObjectSerializationUtilities.GetTypeProperties(typeName);
var staticProperties = TypeCache.GetTypeProperties(typeName);
foreach (var entry in dictObj)
{
if (staticProperties.TryGetValue(entry.Key, out PropertyInfo? value) && value.CanWrite)
Expand Down Expand Up @@ -359,10 +358,10 @@ private Base Dict2Base(Dictionary<string, object?> dictObj)
bb.filePath = bb.GetLocalDestinationPath(BlobStorageFolder);
}

var onDeserializedCallbacks = BaseObjectSerializationUtilities.GetOnDeserializedCallbacks(typeName);
var onDeserializedCallbacks = TypeCache.GetOnDeserializedCallbacks(typeName);
foreach (MethodInfo onDeserialized in onDeserializedCallbacks)
{
onDeserialized.Invoke(baseObj, new object?[] { null });
onDeserialized.Invoke(baseObj, _invokeNull);
}

return baseObj;
Expand Down
3 changes: 1 addition & 2 deletions src/Speckle.Sdk/Serialisation/BaseObjectSerializerV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Speckle.Sdk.Models;
using Speckle.Sdk.Transports;
using Constants = Speckle.Sdk.Helpers.Constants;
using Utilities = Speckle.Sdk.Models.Utilities;

namespace Speckle.Sdk.Serialisation;

Expand Down Expand Up @@ -438,7 +437,7 @@ private void UpdateParentClosures(string objectId)
private static string ComputeId(IReadOnlyDictionary<string, object?> obj)
{
string serialized = JsonConvert.SerializeObject(obj);
string hash = Crypt.Sha256(serialized, length: Utilities.HASH_LENGTH);
string hash = Crypt.Sha256(serialized, length: HashUtility.HASH_LENGTH);
return hash;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Speckle.Sdk/Serialisation/SpeckleDeserializeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Speckle.Sdk.Serialisation;

public class SpeckleDeserializeException : SpeckleException
{
public SpeckleDeserializeException(string message, Exception? inner = null)
: base(message, inner) { }

public SpeckleDeserializeException(string message)
: base(message) { }
}
17 changes: 1 addition & 16 deletions src/Speckle.Sdk/Serialisation/SpeckleSerializerException.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
using Speckle.Sdk.Logging;

namespace Speckle.Sdk.Serialisation;
namespace Speckle.Sdk.Serialisation;

public class SpeckleSerializeException : SpeckleException
{
public SpeckleSerializeException() { }

public SpeckleSerializeException(string message, Exception? inner = null)
: base(message, inner) { }

public SpeckleSerializeException(string message)
: base(message) { }
}

public class SpeckleDeserializeException : SpeckleException
{
public SpeckleDeserializeException() { }

public SpeckleDeserializeException(string message, Exception? inner = null)
: base(message, inner) { }

public SpeckleDeserializeException(string message)
: base(message) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;
namespace Speckle.Sdk.Serialisation.Utilities;

internal static class CallSiteCache
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Speckle.Newtonsoft.Json;
using Speckle.Sdk.Common;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;
namespace Speckle.Sdk.Serialisation.Utilities;

public static class ClosureParser
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Speckle.Sdk.Logging;
using Speckle.Sdk.Common;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;
namespace Speckle.Sdk.Serialisation.Utilities;

internal enum WorkerThreadTaskType
{
Expand Down Expand Up @@ -48,7 +48,7 @@ protected override void ThreadMain()

try
{
(string objectJson, long? current, long? total) = ((string, long?, long?))inputValue!;
(string objectJson, long? current, long? total) = ((string, long?, long?))inputValue.NotNull();
var result = RunOperation(taskType, objectJson, current, total, _serializer);
tcs.SetResult(result);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Speckle.Sdk/Serialisation/Utilities/OperationTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Speckle.Sdk.Serialisation.Utilities;

internal readonly struct OperationTask<T>
where T : struct
{
public readonly T OperationType;
public readonly object? InputValue;
public readonly TaskCompletionSource<object?>? Tcs;

public OperationTask(T operationType, object? inputValue = null, TaskCompletionSource<object?>? tcs = null)
{
OperationType = operationType;
InputValue = inputValue;
Tcs = tcs;
}

public void Deconstruct(out T operationType, out object? inputValue, out TaskCompletionSource<object?>? tcs)
{
operationType = OperationType;
inputValue = InputValue;
tcs = Tcs;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;

internal readonly struct OperationTask<T>
where T : struct
{
public readonly T OperationType;
public readonly object? InputValue;
public readonly TaskCompletionSource<object?>? Tcs;

public OperationTask(T operationType, object? inputValue = null, TaskCompletionSource<object?>? tcs = null)
{
OperationType = operationType;
InputValue = inputValue;
Tcs = tcs;
}

public void Deconstruct(out T operationType, out object? inputValue, out TaskCompletionSource<object?>? tcs)
{
operationType = OperationType;
inputValue = InputValue;
tcs = Tcs;
}
}
namespace Speckle.Sdk.Serialisation.Utilities;

internal abstract class ParallelOperationExecutor<TOperation> : IDisposable
where TOperation : struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Runtime.Serialization;
using Speckle.Sdk.Host;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;
namespace Speckle.Sdk.Serialisation.Utilities;

internal static class BaseObjectSerializationUtilities
internal static class TypeCache
{
#region Getting Types
private static ConcurrentDictionary<string, IReadOnlyDictionary<string, PropertyInfo>> s_typeProperties = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Speckle.DoubleNumerics;
using Speckle.Sdk.Logging;

namespace Speckle.Sdk.Serialisation.SerializationUtilities;
namespace Speckle.Sdk.Serialisation.Utilities;

internal static class ValueConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Speckle.Sdk.Common;
using Speckle.Sdk.Helpers;
using Speckle.Sdk.Logging;
using Speckle.Sdk.Serialisation.SerializationUtilities;
using Speckle.Sdk.Serialisation.Utilities;

namespace Speckle.Sdk.Transports.ServerUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Setup()
/// </summary>
/// <remarks>
/// If you're tempted to add to this list, please ensure both our serializer and deserializer support properties of this type
/// Check the <see cref="Speckle.Sdk.Serialisation.SerializationUtilities.ValueConverter"/>
/// Check the <see cref="Speckle.Sdk.Serialisation.Utilities.ValueConverter"/>
/// Check the <see cref="BaseObjectSerializerV2"/>
/// (or is an interface where all concrete types are supported)
/// You should also consider adding a test in SerializerNonBreakingChanges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Speckle.Sdk.Host;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation;
using Speckle.Sdk.Serialisation.SerializationUtilities;

namespace Speckle.Sdk.Serialization.Tests;

Expand Down
42 changes: 1 addition & 41 deletions tests/Speckle.Sdk.Tests.Unit/Models/UtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Speckle.Sdk.Tests.Unit.Models;

[TestFixture(TestOf = typeof(Crypt))]
public sealed class UtilitiesTests
public sealed class HashUtilityTests
{
[Test]
[TestOf(nameof(Crypt.Md5))]
Expand All @@ -29,44 +29,4 @@ public void Sha256(string input, string expected)
Assert.That(lower, Is.EqualTo(expected.ToLower()));
Assert.That(upper, Is.EqualTo(expected.ToUpper()));
}

[Test]
public void FlattenToNativeConversion()
{
var singleObject = new object();
var nestedObjects = new List<object>()
{
new List<object>()
{
new(), // obj 1
new() // obj 2
},
new() // obj 3
};

var testEnum = new List<object>() { new(), new() }.Select(o => o);

var nestedObjectsWithEnumerableInherited = new List<object>()
{
new List<object>()
{
new(), // obj 1
new(), // obj 2
testEnum // obj 3
},
new() // obj 4
};

var parentTestEnumFlattened = Utilities.FlattenToHostConversionResult(testEnum);
var singleObjectFlattened = Utilities.FlattenToHostConversionResult(singleObject);
var nestedObjectsFlattened = Utilities.FlattenToHostConversionResult(nestedObjects);
var nestedObjectsWithEnumerableInheritedFlattened = Utilities.FlattenToHostConversionResult(
nestedObjectsWithEnumerableInherited
);

Assert.That(parentTestEnumFlattened.Count, Is.EqualTo(1));
Assert.That(singleObjectFlattened.Count, Is.EqualTo(1));
Assert.That(nestedObjectsFlattened.Count, Is.EqualTo(3));
Assert.That(nestedObjectsWithEnumerableInheritedFlattened.Count, Is.EqualTo(4));
}
}
Loading

0 comments on commit 4c3e572

Please sign in to comment.