12 internal sealed
class StorageService : IStorageService, IDisposable
14 private readonly LinkedList<TaskCompletionSource<bool>> tasksWaiting =
new();
15 private readonly
string dataFolder;
18 private bool? initialized =
null;
19 private bool started =
false;
24 public StorageService()
26 string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
27 this.dataFolder = Path.Combine(appDataFolder,
"Data");
33 public string DataFolder => this.dataFolder;
35 #region LifeCycle management
38 public async Task Init(CancellationToken? cancellationToken)
40 lock (this.tasksWaiting)
53 if (this.databaseProvider is
null)
55 this.databaseProvider = await this.CreateDatabaseFile();
58 await this.databaseProvider.
Start();
61 if (this.databaseProvider is not
null)
72 ServiceRef.LogService.LogException(e1);
84 Directory.Delete(this.dataFolder,
true);
86 this.databaseProvider = await this.CreateDatabaseFile();
90 await this.databaseProvider.
Start();
103 ServiceRef.LogService.LogException(e3);
113 this.InitDone(
false);
116 private void InitDone(
bool Result)
118 lock (this.tasksWaiting)
120 this.initialized = Result;
122 foreach (TaskCompletionSource<bool> Wait
in this.tasksWaiting)
123 Wait.TrySetResult(Result);
125 this.tasksWaiting.Clear();
130 public Task<bool> WaitInitDone()
132 lock (this.tasksWaiting)
134 if (this.initialized.HasValue)
135 return Task.FromResult<
bool>(this.initialized.Value);
137 TaskCompletionSource<bool> Wait =
new();
138 this.tasksWaiting.AddLast(Wait);
145 public async Task Shutdown()
147 lock (this.tasksWaiting)
149 this.initialized =
null;
150 this.started =
false;
155 if (this.persistedEventLog is not
null)
158 this.persistedEventLog.
Dispose();
159 this.persistedEventLog =
null;
162 if (this.databaseProvider is not
null)
165 await this.databaseProvider.
Flush();
166 await this.databaseProvider.
Stop();
167 this.databaseProvider =
null;
172 ServiceRef.LogService.LogException(ex);
176 private Task<FilesProvider> CreateDatabaseFile()
178 FilesProvider.AsyncFileIo =
false;
181 (
int)Constants.Timeouts.Database.TotalMilliseconds, ServiceRef.CryptoService.GetCustomKey);
187 public void Dispose()
189 this.persistedEventLog?.
Dispose();
190 this.persistedEventLog =
null;
192 this.databaseProvider?.
Dispose();
193 this.databaseProvider =
null;
198 public async Task Insert(
object obj)
204 public async Task Update(
object obj)
210 public Task<T> FindFirstDeleteRest<T>() where T : class
212 return Database.FindFirstDeleteRest<T>();
215 public Task<T> FindFirstIgnoreRest<T>() where T : class
217 return Database.FindFirstIgnoreRest<T>();
228 public void FlagForRepair()
230 this.DeleteFile(
"Start.txt");
231 this.DeleteFile(
"Stop.txt");
234 private void DeleteFile(
string FileName)
238 FileName = Path.Combine(this.dataFolder, FileName);
240 if (File.Exists(FileName))
241 File.Delete(FileName);
Static class managing the application event log. Applications and services log events on this static ...
static void Register(IEventSink EventSink)
Registers an event sink with the event log. Call Unregister(IEventSink) to unregister it,...
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
static bool Unregister(IEventSink EventSink)
Unregisters an event sink from the event log.
Creates an even sink that stores incoming (logged) events in the local object database,...
override void Dispose()
IDisposable.Dispose()
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static bool HasProvider
If a database provider is registered.
static void Register(IDatabaseProvider DatabaseProvider)
Registers a database provider for use from the static Database class, throughout the lifetime of the ...
static IDatabaseProvider Provider
Registered database provider.
static Task< bool > Export(IDatabaseExport Output)
Performs an export of the database.
static async Task Update(object Object)
Updates an object in the database.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Persists objects into binary files.
async Task Stop()
Called when processing ends.
async Task Flush()
Persists any pending changes.
async Task< string[]> RepairIfInproperShutdown(string XsltPath)
Checks if the database needs repairing. This is done by checking the last start and stop timetamps to...
Task Start()
Called when processing starts.
static Task< FilesProvider > CreateAsync(string Folder, string DefaultCollectionName, int BlockSize, int BlocksInCache, int BlobBlockSize, Encoding Encoding, int TimeoutMilliseconds, CustomKeyHandler CustomKeyMethod)
Persists objects into binary files.
void Dispose()
IDisposable.Dispose
Task Flush()
Persists any pending changes.
Interface for database exports.