Skip to content

Commit

Permalink
add cache base option
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Nov 28, 2024
1 parent 428841e commit 369ddeb
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 19 deletions.
47 changes: 47 additions & 0 deletions src/Speckle.Sdk/Serialisation/V2/Send/EmptyDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace Speckle.Sdk.Serialisation.V2.Send;

public class EmptyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => throw new NotImplementedException();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public void Add(KeyValuePair<TKey, TValue> item) { }

public void Clear() => throw new NotImplementedException();

public bool Contains(KeyValuePair<TKey, TValue> item) => false;

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => throw new NotImplementedException();

public bool Remove(KeyValuePair<TKey, TValue> item) => false;

public int Count => 0;
public bool IsReadOnly => false;

public void Add(TKey key, TValue value) { }

public bool ContainsKey(TKey key) => false;

public bool Remove(TKey key) => false;

public bool TryGetValue(TKey key, [UnscopedRef] out TValue value)
{
value = default!;
return false;
}

public TValue this[TKey key]
{
#pragma warning disable CA1065
get => throw new NotImplementedException();
#pragma warning restore CA1065
set { }
}

public ICollection<TKey> Keys { get; }
public ICollection<TValue> Values { get; }
}
7 changes: 3 additions & 4 deletions src/Speckle.Sdk/Serialisation/V2/Send/ObjectSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Drawing;
using System.Globalization;
using Speckle.DoubleNumerics;
Expand All @@ -21,7 +20,7 @@ public class ObjectSerializer : IObjectSerializer
{
private HashSet<object> _parentObjects = new();
private readonly Closures _currentClosures = new();
private readonly ConcurrentDictionary<Base, CacheInfo> _baseCache;
private readonly IDictionary<Base, CacheInfo> _baseCache;

private readonly bool _trackDetachedChildren;
private readonly IBasePropertyGatherer _propertyGatherer;
Expand All @@ -42,7 +41,7 @@ public class ObjectSerializer : IObjectSerializer
/// <param name="cancellationToken"></param>
public ObjectSerializer(
IBasePropertyGatherer propertyGatherer,
ConcurrentDictionary<Base, CacheInfo> baseCache,
IDictionary<Base, CacheInfo> baseCache,
bool trackDetachedChildren = false,
CancellationToken cancellationToken = default
)
Expand Down Expand Up @@ -70,7 +69,7 @@ public ObjectSerializer(
{
throw new SpeckleSerializeException($"Failed to extract (pre-serialize) properties from the {baseObj}", ex);
}
_baseCache.TryAdd(baseObj, new(item.Item2, _currentClosures));
_baseCache[baseObj] = new(item.Item2, _currentClosures);
yield return (item.Item1, item.Item2);
foreach (var chunk in _chunks)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Concurrent;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Models;

Expand All @@ -7,8 +6,6 @@ namespace Speckle.Sdk.Serialisation.V2.Send;
[GenerateAutoInterface]
public class ObjectSerializerFactory(IBasePropertyGatherer propertyGatherer) : IObjectSerializerFactory
{
public IObjectSerializer Create(
ConcurrentDictionary<Base, CacheInfo> baseCache,
CancellationToken cancellationToken
) => new ObjectSerializer(propertyGatherer, baseCache, true, cancellationToken);
public IObjectSerializer Create(IDictionary<Base, CacheInfo> baseCache, CancellationToken cancellationToken) =>
new ObjectSerializer(propertyGatherer, baseCache, true, cancellationToken);
}
9 changes: 5 additions & 4 deletions src/Speckle.Sdk/Serialisation/V2/Send/SerializeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Speckle.Sdk.Serialisation.V2.Send;

public record SerializeProcessOptions(bool SkipCacheRead, bool SkipCacheWrite, bool SkipServer);
public record SerializeProcessOptions(bool SkipCacheRead, bool SkipCacheWrite, bool SkipServer, bool CacheBases);

public readonly record struct SerializeProcessResults(
string RootId,
Expand All @@ -25,8 +25,11 @@ public class SerializeProcess(
SerializeProcessOptions? options = null
) : ChannelSaver, ISerializeProcess
{
private readonly SerializeProcessOptions _options = options ?? new(false, false, false, true);
private readonly ConcurrentDictionary<Id, Json> _jsonCache = new();
private readonly ConcurrentDictionary<Base, CacheInfo> _baseCache = new();

private readonly IDictionary<Base, CacheInfo> _baseCache =
options?.CacheBases ?? true ? new ConcurrentDictionary<Base, CacheInfo>() : new EmptyDictionary<Base, CacheInfo>();
private readonly ConcurrentDictionary<Id, ObjectReference> _objectReferences = new();

private long _totalFound;
Expand All @@ -35,8 +38,6 @@ public class SerializeProcess(
private long _cached;
private long _serialized;

private readonly SerializeProcessOptions _options = options ?? new(false, false, false);

public async Task<SerializeProcessResults> Serialize(Base root, CancellationToken cancellationToken)
{
var channelTask = Start(cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion tests/Speckle.Sdk.Serialization.Testing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
streamId,
token,
progress,
new SerializeProcessOptions(skipCacheSendCheck, skipCacheSendSave, true)
new SerializeProcessOptions(skipCacheSendCheck, skipCacheSendSave, true, true)
);
await process2.Serialize(@base, default).ConfigureAwait(false);
Console.WriteLine("Detach");
Expand Down
12 changes: 8 additions & 4 deletions tests/Speckle.Sdk.Serialization.Tests/DetachedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public void Setup()
}

[Test(Description = "Checks that all typed properties (including obsolete ones) are returned")]
public async Task CanSerialize_New_Detached()
[TestCase(true)]
[TestCase(false)]
public async Task CanSerialize_New_Detached(bool cacheBases)
{
var expectedJson = """
{
Expand Down Expand Up @@ -74,7 +76,7 @@ public async Task CanSerialize_New_Detached()
new DummyServerObjectManager(),
new BaseChildFinder(new BasePropertyGatherer()),
new ObjectSerializerFactory(new BasePropertyGatherer()),
new SerializeProcessOptions(false, false, true)
new SerializeProcessOptions(false, false, true, cacheBases)
);
await process2.Serialize(@base, default).ConfigureAwait(false);

Expand Down Expand Up @@ -191,7 +193,9 @@ public void GetPropertiesExpected_All()
}

[Test(Description = "Checks that all typed properties (including obsolete ones) are returned")]
public async Task CanSerialize_New_Detached2()
[TestCase(true)]
[TestCase(false)]
public async Task CanSerialize_New_Detached2(bool cacheBases)
{
var root = """
Expand Down Expand Up @@ -266,7 +270,7 @@ public async Task CanSerialize_New_Detached2()
new DummyServerObjectManager(),
new BaseChildFinder(new BasePropertyGatherer()),
new ObjectSerializerFactory(new BasePropertyGatherer()),
new SerializeProcessOptions(false, false, true)
new SerializeProcessOptions(false, false, true, cacheBases)
);
var results = await process2.Serialize(@base, default).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public async Task Roundtrip_Test_New(string fileName, string rootId, int oldCoun
new DummySendServerObjectManager(newIdToJson),
new BaseChildFinder(new BasePropertyGatherer()),
new ObjectSerializerFactory(new BasePropertyGatherer()),
new SerializeProcessOptions(true, true, false)
new SerializeProcessOptions(true, true, false, true)
);
var (rootId2, _) = await serializeProcess.Serialize(root, default);

Expand Down

0 comments on commit 369ddeb

Please sign in to comment.