Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into better-options
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Nov 29, 2024
2 parents 369ddeb + 05f90ea commit 15bab92
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
11 changes: 11 additions & 0 deletions src/Speckle.Sdk.Dependencies/Collections.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Frozen;

namespace Speckle.Sdk.Dependencies;

public static class Collections
{
public static IReadOnlyCollection<T> Freeze<T>(this IEnumerable<T> source) => source.ToFrozenSet();

public static IReadOnlyDictionary<TKey, TValue> Freeze<TKey, TValue>(this IDictionary<TKey, TValue> source)
where TKey : notnull => source.ToFrozenDictionary();
}
11 changes: 6 additions & 5 deletions src/Speckle.Sdk/Serialisation/V2/Receive/DeserializeProcess.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Dependencies;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation.Utilities;
using Speckle.Sdk.Transports;
Expand All @@ -22,7 +23,7 @@ public sealed class DeserializeProcess(
{
private readonly DeserializeProcessOptions _options = options ?? new(false);

private readonly ConcurrentDictionary<string, (string, IReadOnlyList<string>)> _closures = new();
private readonly ConcurrentDictionary<string, (string, IReadOnlyCollection<string>)> _closures = new();
private readonly ConcurrentDictionary<string, Base> _baseCache = new();
private readonly ConcurrentDictionary<string, Task> _activeTasks = new();

Expand Down Expand Up @@ -91,7 +92,7 @@ private async Task Traverse(string id, CancellationToken cancellationToken)
}
}

private (string, IReadOnlyList<string>) GetClosures(string id)
private (string, IReadOnlyCollection<string>) GetClosures(string id)
{
if (!_closures.TryGetValue(id, out var closures))
{
Expand All @@ -100,7 +101,7 @@ private async Task Traverse(string id, CancellationToken cancellationToken)
{
throw new SpeckleException($"Missing object id in SQLite cache: {id}");
}
var childrenIds = ClosureParser.GetClosures(json).OrderByDescending(x => x.Item2).Select(x => x.Item1).ToList();
var childrenIds = ClosureParser.GetClosures(json).OrderByDescending(x => x.Item2).Select(x => x.Item1).Freeze();
closures = (json, childrenIds);
_closures.TryAdd(id, closures);
}
Expand All @@ -114,15 +115,15 @@ public void DecodeOrEnqueueChildren(string id)
{
return;
}
(string json, IReadOnlyList<string> closures) = GetClosures(id);
(string json, IReadOnlyCollection<string> closures) = GetClosures(id);
var @base = Deserialise(id, json, closures);
_baseCache.TryAdd(id, @base);
//remove from JSON cache because we've finally made the Base
_closures.TryRemove(id, out _);
_activeTasks.TryRemove(id, out _);
}

private Base Deserialise(string id, string json, IReadOnlyList<string> closures)
private Base Deserialise(string id, string json, IReadOnlyCollection<string> closures)
{
if (_baseCache.TryGetValue(id, out var baseObject))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Speckle.Sdk.Serialisation.V2.Receive;
[GenerateAutoInterface]
public sealed class ObjectDeserializer(
string currentId,
IReadOnlyList<string> currentClosures,
IReadOnlyCollection<string> currentClosures,
IReadOnlyDictionary<string, Base> references,
SpeckleObjectSerializerPool pool,
DeserializeProcessOptions? options = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Speckle.InterfaceGenerator;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Models;

namespace Speckle.Sdk.Serialisation.V2.Receive;
Expand All @@ -8,7 +8,7 @@ public class ObjectDeserializerFactory : IObjectDeserializerFactory
{
public IObjectDeserializer Create(
string currentId,
IReadOnlyList<string> currentClosures,
IReadOnlyCollection<string> currentClosures,
IReadOnlyDictionary<string, Base> references,
DeserializeProcessOptions? options = null
) => new ObjectDeserializer(currentId, currentClosures, references, SpeckleObjectSerializerPool.Instance, options);
Expand Down
7 changes: 4 additions & 3 deletions src/Speckle.Sdk/Serialisation/V2/Receive/ObjectLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Common;
using Speckle.Sdk.Dependencies;
using Speckle.Sdk.Dependencies.Serialization;
using Speckle.Sdk.Serialisation.Utilities;
using Speckle.Sdk.SQLite;
Expand All @@ -19,7 +20,7 @@ public sealed class ObjectLoader(
private long _cached;
private DeserializeProcessOptions _options = new(false);

public async Task<(string, IReadOnlyList<string>)> GetAndCache(
public async Task<(string, IReadOnlyCollection<string>)> GetAndCache(
string rootId,
DeserializeProcessOptions options,
CancellationToken cancellationToken
Expand All @@ -41,12 +42,12 @@ CancellationToken cancellationToken
.DownloadSingleObject(rootId, progress, cancellationToken)
.NotNull()
.ConfigureAwait(false);
List<string> allChildrenIds = ClosureParser
IReadOnlyCollection<string> allChildrenIds = ClosureParser
.GetClosures(rootJson)
.OrderByDescending(x => x.Item2)
.Select(x => x.Item1)
.Where(x => !x.StartsWith("blob", StringComparison.Ordinal))
.ToList();
.Freeze();
_allChildrenCount = allChildrenIds.Count;
await GetAndCache(allChildrenIds, cancellationToken).ConfigureAwait(false);

Expand Down
6 changes: 3 additions & 3 deletions src/Speckle.Sdk/Serialisation/V2/Send/ObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Speckle.Sdk.Helpers;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation.Utilities;
using Closures = System.Collections.Generic.Dictionary<Speckle.Sdk.Serialisation.Id, int>;
using Closures = System.Collections.Generic.IReadOnlyDictionary<Speckle.Sdk.Serialisation.Id, int>;

namespace Speckle.Sdk.Serialisation.V2.Send;

Expand All @@ -19,7 +19,7 @@ namespace Speckle.Sdk.Serialisation.V2.Send;
public class ObjectSerializer : IObjectSerializer
{
private HashSet<object> _parentObjects = new();
private readonly Closures _currentClosures = new();
private readonly Dictionary<Id, int> _currentClosures = new();
private readonly IDictionary<Base, CacheInfo> _baseCache;

private readonly bool _trackDetachedChildren;
Expand Down Expand Up @@ -362,7 +362,7 @@ private void SerializeOrChunkProperty(object? baseValue, JsonWriter jsonWriter,
SerializeProperty(baseValue, jsonWriter, inheritedDetachInfo: detachInfo);
}

private static void MergeClosures(Closures current, Closures child)
private static void MergeClosures(Dictionary<Id, int> current, Closures child)
{
foreach (var closure in child)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Speckle.Sdk/Serialisation/V2/Send/SerializeProcess.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Common;
using Speckle.Sdk.Dependencies;
using Speckle.Sdk.Dependencies.Serialization;
using Speckle.Sdk.Models;
using Speckle.Sdk.SQLite;
Expand Down Expand Up @@ -43,7 +44,7 @@ public async Task<SerializeProcessResults> Serialize(Base root, CancellationToke
var channelTask = Start(cancellationToken);
await Traverse(root, true, cancellationToken).ConfigureAwait(false);
await channelTask.ConfigureAwait(false);
return new(root.id, _objectReferences);
return new(root.id, _objectReferences.Freeze());
}

private async Task Traverse(Base obj, bool isEnd, CancellationToken cancellationToken)
Expand Down
8 changes: 4 additions & 4 deletions tests/Speckle.Sdk.Serialization.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class SerializationTests
{
private class TestLoader(string json) : IObjectLoader
{
public Task<(string, IReadOnlyList<string>)> GetAndCache(
public Task<(string, IReadOnlyCollection<string>)> GetAndCache(
string rootId,
DeserializeProcessOptions? options,
CancellationToken cancellationToken
)
{
var childrenIds = ClosureParser.GetChildrenIds(json).ToList();
return Task.FromResult<(string, IReadOnlyList<string>)>((json, childrenIds));
return Task.FromResult<(string, IReadOnlyCollection<string>)>((json, childrenIds));
}

public string? LoadId(string id) => null;
Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task RunTest2(string fileName)

public class TestObjectLoader(Dictionary<string, string> idToObject) : IObjectLoader
{
public Task<(string, IReadOnlyList<string>)> GetAndCache(
public Task<(string, IReadOnlyCollection<string>)> GetAndCache(
string rootId,
DeserializeProcessOptions? options,
CancellationToken cancellationToken
Expand All @@ -99,7 +99,7 @@ CancellationToken cancellationToken
}

var allChildren = ClosureParser.GetChildrenIds(json).ToList();
return Task.FromResult<(string, IReadOnlyList<string>)>((json, allChildren));
return Task.FromResult<(string, IReadOnlyCollection<string>)>((json, allChildren));
}

public string? LoadId(string id) => idToObject.GetValueOrDefault(id);
Expand Down

0 comments on commit 15bab92

Please sign in to comment.