add more disposal

This commit is contained in:
Adam Hathcock
2024-12-20 10:01:30 +00:00
parent 1b84c6cea7
commit cf0dfe8ef8
7 changed files with 44 additions and 44 deletions
@@ -1,11 +1,13 @@
using System.Text;
using Microsoft.Data.Sqlite;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Dependencies;
namespace Speckle.Sdk.SQLite;
public partial interface ISqLiteJsonCacheManager : IDisposable;
[GenerateAutoInterface]
public sealed class SqLiteJsonCacheManager : ISqLiteJsonCacheManager, IDisposable
public sealed class SqLiteJsonCacheManager : ISqLiteJsonCacheManager
{
private readonly string _connectionString;
private readonly CacheDbCommandPool _pool;
@@ -17,6 +19,7 @@ public sealed class SqLiteJsonCacheManager : ISqLiteJsonCacheManager, IDisposabl
_pool = new CacheDbCommandPool(_connectionString);
}
[AutoInterfaceIgnore]
public void Dispose() => _pool.Dispose();
private void Initialize()
@@ -135,7 +138,7 @@ public sealed class SqLiteJsonCacheManager : ISqLiteJsonCacheManager, IDisposabl
private void CreateBulkInsert(SqliteCommand cmd, IEnumerable<(string id, string json)> items)
{
StringBuilder sb = new();
StringBuilder sb = Pools.StringBuilders.Get();
sb.AppendLine(CacheDbCommands.Commands[(int)CacheOperation.BulkInsertOrIgnore]);
int i = 0;
foreach (var (id, json) in items)
@@ -150,6 +153,7 @@ public sealed class SqLiteJsonCacheManager : ISqLiteJsonCacheManager, IDisposabl
#pragma warning disable CA2100
cmd.CommandText = sb.ToString();
#pragma warning restore CA2100
Pools.StringBuilders.Return(sb);
}
public bool HasObject(string objectId) =>
@@ -5,9 +5,11 @@ using Speckle.Sdk.Transports;
namespace Speckle.Sdk.Serialisation.V2;
public class DummySqLiteJsonCacheManager : ISqLiteJsonCacheManager
public sealed class DummySqLiteJsonCacheManager : ISqLiteJsonCacheManager
{
public IEnumerable<string> GetAllObjects() => throw new NotImplementedException();
public void Dispose() { }
public IReadOnlyCollection<string> GetAllObjects() => throw new NotImplementedException();
public void DeleteObject(string id) => throw new NotImplementedException();
@@ -13,6 +13,7 @@ public record DeserializeProcessOptions(
bool SkipInvalidConverts = false
);
public partial interface IDeserializeProcess : IDisposable;
[GenerateAutoInterface]
public sealed class DeserializeProcess(
IProgress<ProgressArgs>? progress,
@@ -29,6 +30,10 @@ public sealed class DeserializeProcess(
public IReadOnlyDictionary<string, Base> BaseCache => _baseCache;
public long Total { get; private set; }
[AutoInterfaceIgnore]
public void Dispose() => objectLoader.Dispose();
public async Task<Base> Deserialize(string rootId, CancellationToken cancellationToken)
{
@@ -9,6 +9,7 @@ using Speckle.Sdk.Transports;
namespace Speckle.Sdk.Serialisation.V2.Receive;
public partial interface IObjectLoader : IDisposable;
[GenerateAutoInterface]
public sealed class ObjectLoader(
ISqLiteJsonCacheManager sqLiteJsonCacheManager,
@@ -20,6 +21,8 @@ public sealed class ObjectLoader(
private long _checkCache;
private long _cached;
private DeserializeProcessOptions _options = new(false);
[AutoInterfaceIgnore]
public void Dispose() => sqLiteJsonCacheManager.Dispose();
public async Task<(string, IReadOnlyCollection<string>)> GetAndCache(
string rootId,
@@ -0,0 +1,19 @@
using System.Text;
namespace Speckle.Sdk.Serialisation.V2.Send;
public readonly record struct BaseItem(Id Id, Json Json, bool NeedsStorage, Dictionary<Id, int>? Closures) : IHasSize
{
public int Size { get; } = Encoding.UTF8.GetByteCount(Json.Value);
public bool Equals(BaseItem? other)
{
if (other is null)
{
return false;
}
return string.Equals(Id.Value, other.Value.Id.Value, StringComparison.OrdinalIgnoreCase);
}
public override int GetHashCode() => Id.GetHashCode();
}
@@ -1,5 +1,4 @@
using System.Collections.Concurrent;
using System.Text;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Common;
using Speckle.Sdk.Dependencies;
@@ -25,24 +24,9 @@ public readonly record struct SerializeProcessResults(
IReadOnlyDictionary<Id, ObjectReference> ConvertedReferences
);
public readonly record struct BaseItem(Id Id, Json Json, bool NeedsStorage, Closures? Closures) : IHasSize
{
public int Size { get; } = Encoding.UTF8.GetByteCount(Json.Value);
public bool Equals(BaseItem? other)
{
if (other is null)
{
return false;
}
return string.Equals(Id.Value, other.Value.Id.Value, StringComparison.OrdinalIgnoreCase);
}
public override int GetHashCode() => Id.GetHashCode();
}
public partial interface ISerializeProcess : IDisposable;
[GenerateAutoInterface]
public class SerializeProcess(
public sealed class SerializeProcess(
IProgress<ProgressArgs>? progress,
ISqLiteJsonCacheManager sqLiteJsonCacheManager,
IServerObjectManager serverObjectManager,
@@ -64,6 +48,8 @@ public class SerializeProcess(
private long _uploaded;
private long _cached;
[AutoInterfaceIgnore]
public void Dispose() => sqLiteJsonCacheManager.Dispose();
public async Task<SerializeProcessResults> Serialize(Base root, CancellationToken cancellationToken)
{
@@ -21,11 +21,6 @@ public interface ISerializeProcessFactory
IProgress<ProgressArgs>? progress,
DeserializeProcessOptions? options = null
);
public ISerializeProcess CreateSerializeProcess(
SerializeProcessOptions? options = null,
IProgress<ProgressArgs>? progress = null
);
}
public class SerializeProcessFactory(
@@ -56,23 +51,6 @@ public class SerializeProcessFactory(
);
}
public ISerializeProcess CreateSerializeProcess(
SerializeProcessOptions? options = null,
IProgress<ProgressArgs>? progress = null
)
{
var sqLiteJsonCacheManager = new DummySqLiteJsonCacheManager();
var serverObjectManager = new DummySendServerObjectManager();
return new SerializeProcess(
progress,
sqLiteJsonCacheManager,
serverObjectManager,
baseChildFinder,
objectSerializerFactory,
options
);
}
public IDeserializeProcess CreateDeserializeProcess(
Uri url,
string streamId,
@@ -84,7 +62,10 @@ public class SerializeProcessFactory(
var sqLiteJsonCacheManager = sqLiteJsonCacheManagerFactory.CreateFromStream(streamId);
var serverObjectManager = serverObjectManagerFactory.Create(url, streamId, authorizationToken);
#pragma warning disable CA2000
//owned by process, refactor later
var objectLoader = new ObjectLoader(sqLiteJsonCacheManager, serverObjectManager, progress);
#pragma warning restore CA2000
return new DeserializeProcess(progress, objectLoader, objectDeserializerFactory, options);
}
}