Skip to content

Commit

Permalink
Add background flush to SQLite key/value store (#360)
Browse files Browse the repository at this point in the history
* Add flush interval to SqliteKeyValueStoreOptions

* Use background database flush if configured

Updates `SqliteKeyValueStore` to allow a background flush interval to be specified (opt-in; default behaviour is to continue to write to the database immediately).

When a flush interval is specified, write and delete operations are put into a dictionary indexed by data store key (value = the bytes to write for that key; null value = delete the key). A background task performs a flush at the interval specified.

* Add typed singleton KV store registration

When registering an `IKeyValueStore` service of type `T` with the dependency injection container, two singleton services are now registered:

- `T`
- `IKeyValueStore` (which is registered using a service factory that gets the `T` service)

* Bump package references

* Remove use of deprecated property

`SchemaGeneratorConfiguration.PropertyNamingMethod` has been deprecated in favour of the new `PropertyNameResolver` property in a recent JsonSchema.Net.Generation package update
  • Loading branch information
wazzamatazz authored Nov 2, 2023
1 parent 8945e5a commit 198ec4a
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 81 deletions.
26 changes: 13 additions & 13 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,37 @@
<ItemGroup>
<PackageVersion Include="Castle.Core" Version="5.1.1" />
<PackageVersion Include="CsvHelper" Version="30.0.1" />
<PackageVersion Include="Google.Protobuf" Version="3.24.3" />
<PackageVersion Include="Grpc.AspNetCore.Server" Version="2.57.0" />
<PackageVersion Include="Grpc.Net.Client" Version="2.57.0" />
<PackageVersion Include="Grpc.Tools" Version="2.58.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks" Version="9.1.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks.AspNetCore" Version="9.1.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks.DependencyInjection" Version="9.1.0" />
<PackageVersion Include="Google.Protobuf" Version="3.25.0" />
<PackageVersion Include="Grpc.AspNetCore.Server" Version="2.58.0" />
<PackageVersion Include="Grpc.Net.Client" Version="2.58.0" />
<PackageVersion Include="Grpc.Tools" Version="2.59.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks" Version="11.0.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks.AspNetCore" Version="11.0.0" />
<PackageVersion Include="IntelligentPlant.BackgroundTasks.DependencyInjection" Version="11.0.0" />
<PackageVersion Include="Jaahas.HttpRequestTransformer" Version="2.2.0" />
<PackageVersion Include="JsonSchema.Net.Generation" Version="3.3.2" />
<PackageVersion Include="JsonSchema.Net.Generation" Version="3.4.1" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.11" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.13" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="7.0.11" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="7.0.13" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageVersion Include="Microsoft.FASTER.Core" Version="2.6.0" />
<PackageVersion Include="Microsoft.FASTER.Core" Version="2.6.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.Web.LibraryManager.Build" Version="$(PkgVersion_Microsoft_Web_LibraryManager_Build)" />
<PackageVersion Include="MiniValidation" Version="0.8.0" />
<PackageVersion Include="MiniValidation" Version="0.9.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nito.AsyncEx.Coordination" Version="5.1.2" />
<PackageVersion Include="NSwag.AspNetCore" Version="13.19.0" />
<PackageVersion Include="NSwag.AspNetCore" Version="13.20.0" />
<PackageVersion Include="OpenTelemetry" Version="$(PkgVersion_OpenTelemetry)" />
<PackageVersion Include="OpenTelemetry.Api" Version="$(PkgVersion_OpenTelemetry_Api)" />
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="$(PkgVersion_OpenTelemetry_Exporter_Console)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ IKeyValueStore implementationInstance
throw new ArgumentNullException(nameof(implementationInstance));
}

builder.Services.AddSingleton(implementationInstance);
builder.Services.AddSingleton(implementationInstance.GetType(), implementationInstance);
builder.Services.AddSingleton(typeof(IKeyValueStore), implementationInstance);

return builder;
}
Expand All @@ -244,7 +245,8 @@ public static IAdapterConfigurationBuilder AddKeyValueStore<T>(this IAdapterConf
throw new ArgumentNullException(nameof(builder));
}

builder.Services.AddSingleton<IKeyValueStore, T>();
builder.Services.AddSingleton<T>();
builder.Services.AddSingleton<IKeyValueStore>(sp => sp.GetRequiredService<T>());
return builder;
}

Expand Down Expand Up @@ -277,8 +279,9 @@ public static IAdapterConfigurationBuilder AddKeyValueStore<T>(this IAdapterConf
if (implementationFactory == null) {
throw new ArgumentNullException(nameof(implementationFactory));
}

builder.Services.AddSingleton<IKeyValueStore, T>(implementationFactory);

builder.Services.AddSingleton(implementationFactory);
builder.Services.AddSingleton<IKeyValueStore>(sp => sp.GetRequiredService<T>());
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#nullable enable
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.Dispose() -> void
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.FlushAsync() -> System.Threading.Tasks.ValueTask
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.WaitForNextFlushAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStoreOptions.EnableRawWrites.get -> bool
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStoreOptions.EnableRawWrites.set -> void
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStoreOptions.FlushInterval.get -> System.TimeSpan
DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStoreOptions.FlushInterval.set -> void
override DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.AllowRawWrites.get -> bool
override DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.ReadAsync<T>(DataCore.Adapter.Services.KVKey key) -> System.Threading.Tasks.ValueTask<T?>
override DataCore.Adapter.KeyValueStore.Sqlite.SqliteKeyValueStore.ReadRawAsync(DataCore.Adapter.Services.KVKey key) -> System.Threading.Tasks.ValueTask<byte[]?>
Expand Down
Loading

0 comments on commit 198ec4a

Please sign in to comment.