Recommended use in ASP.NET Core applications #53
-
Thank you for interesing project! I am thinking of creating a (experimental) timeseries database on top of ZoneTree. What is the recommended use in multi-thread/async/multi-user applications (eg. ASP.NET Core)? Create a new instance of the ZoneTree class per request or have it as a singleton? If ZoneTree is to be a singleton, should maintenance ( I think it would be good to mention the answers to these questions in the documentation. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Thank you for your interest in the project! I'm glad to hear you're considering using ZoneTree for your timeseries database. Here are the answers to your questions: First Question: ZoneTree can be used in various patterns depending on your application's requirements. It is thread-safe, meaning you can have a single ZoneTree instance accessible from all threads without needing additional synchronization. Second Question: The
You can integrate this maintainer in an ASP.NET Core application using the public sealed class ZoneTreeMaintainerHostedService<TKey, TValue> : IHostedService, IDisposable
{
private readonly ZoneTreeMaintainer<TKey, TValue> _zoneTreeMaintainer;
public ZoneTreeMaintainerHostedService(IZoneTree<TKey, TValue> zoneTree)
{
_zoneTreeMaintainer = new ZoneTreeMaintainer<TKey, TValue>(zoneTree, false);
}
public Task StartAsync(CancellationToken cancellationToken)
{
// Note:
// EnableJobForCleaningInactiveCaches periodically cleans up the disk segment read buffers (default: once every 5 seconds).
// You can omit this to keep data in memory forever to speed up reads.
// The trade-off between memory usage and read speed.
_zoneTreeMaintainer.EnableJobForCleaningInactiveCaches= true;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_zoneTreeMaintainer.EnableJobForCleaningInactiveCaches= false;
_zoneTreeMaintainer.CompleteRunningTasks();
return Task.CompletedTask;
}
public void Dispose() => _zoneTreeMaintainer?.Dispose();
}
builder.Services.AddSingleton((serviceProvider) =>
{
return new ZoneTreeFactory<string, string>().OpenOrCreate();
});
builder.Services.AddHostedService<ZoneTreeMaintainerHostedService<string, string>>() |
Beta Was this translation helpful? Give feedback.
-
When instantiating ZoneTree per request, will ZoneTree prove to access files from multiple instances fully in read-write mode, or are there any limitations?
Thanks for the code. |
Beta Was this translation helpful? Give feedback.
-
I was thinking of having a separate ZoneTree database for each bucket. But according to your answers, it makes no sense to think about it and it is more efficient to use ZoneTree as a singlton. |
Beta Was this translation helpful? Give feedback.
Thank you for your interest in the project! I'm glad to hear you're considering using ZoneTree for your timeseries database. Here are the answers to your questions:
First Question:
ZoneTree can be used in various patterns depending on your application's requirements. It is thread-safe, meaning you can have a single ZoneTree instance accessible from all threads without needing additional synchronization.
Second Question:
The
ZoneTreeMaintainer
class is responsible for maintaining a ZoneTree instance. Here are the key events and tasks it handles:OnMutableSegmentMovedForward Event:
ReadOnlySegmentsCount
andReadOnlySegmentsRecordCount
meet the desired criteria.On…