Skip to content

Use a sqlite manager per stream that's kept around forever #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Speckle.Sdk/SQLite/SqLiteJsonCacheManagerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Speckle.InterfaceGenerator;
using System.Collections.Concurrent;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Logging;
using Speckle.Sdk.Serialisation.Utilities;

Expand All @@ -8,12 +9,21 @@ namespace Speckle.Sdk.SQLite;
public class SqLiteJsonCacheManagerFactory : ISqLiteJsonCacheManagerFactory
{
public const int INITIAL_CONCURRENCY = 4;

private readonly ConcurrentDictionary<string, ISqLiteJsonCacheManager> _cachedManagers = new();

private ISqLiteJsonCacheManager Create(string path, int concurrency) => new SqLiteJsonCacheManager(path, concurrency);

public ISqLiteJsonCacheManager CreateForUser(string scope) =>
Create(Path.Combine(SpecklePathProvider.UserApplicationDataPath(), "Speckle", $"{scope}.db"), 1);

public ISqLiteJsonCacheManager CreateFromStream(string streamId) =>
Create(SqlitePaths.GetDBPath(streamId), INITIAL_CONCURRENCY);
public ISqLiteJsonCacheManager CreateFromStream(string streamId)
{
if (!_cachedManagers.TryGetValue(streamId, out var manager))
{
manager = Create(SqlitePaths.GetDBPath(streamId), INITIAL_CONCURRENCY);
_cachedManagers.TryAdd(streamId, manager);
}
return manager;
}
}
2 changes: 2 additions & 0 deletions src/Speckle.Sdk/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ SpeckleSdkOptions speckleSdkOptions
typeof(Client)
);
serviceCollection.AddMatchingInterfacesAsTransient(typeof(GraphQLRetry).Assembly);
//we want to make sqlite pools be singletons per stream so needs a singleton factory
serviceCollection.TryAddSingleton<ISqLiteJsonCacheManagerFactory, SqLiteJsonCacheManagerFactory>();
return serviceCollection;
}

Expand Down
Loading