Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Ledger.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
6
7namespace Waher.Persistence
8{
13 public static class Ledger
14 {
15 private static ILedgerProvider provider = null;
16 private static bool locked = false;
17
25 public static void Register(ILedgerProvider LedgerProvider)
26 {
27 Register(LedgerProvider, true);
28 }
29
38 public static void Register(ILedgerProvider LedgerProvider, bool Lock)
39 {
40 if (!(provider is null))
41 {
42 if (locked)
43 throw new Exception("A ledger provider is already registered.");
44
45 provider.Unregister(externalEvents);
46 }
47
48 provider = LedgerProvider;
49 locked = Lock;
50
51 provider.Register(externalEvents);
52 }
53
54 private static readonly ExternalEvents externalEvents = new ExternalEvents();
55
56 private class ExternalEvents : ILedgerExternalEvents
57 {
58 public void RaiseEntryAdded(object Object)
59 {
60 Ledger.RaiseEntryAdded(Object);
61 }
62
63 public void RaiseEntryDeleted(object Object)
64 {
65 Ledger.RaiseEntryDeleted(Object);
66 }
67
68 public void RaiseEntryUpdated(object Object)
69 {
70 Ledger.RaiseEntryUpdated(Object);
71 }
72
73 public void RaiseCollectionCleared(string Collection)
74 {
75 Ledger.RaiseCollectionCleared(Collection);
76 }
77 }
78
83 {
84 get
85 {
86 if (!(provider is null))
87 return provider;
88 else
89 throw new Exception("A ledger provider has not been registered.");
90 }
91 }
92
93 internal static ILedgerProvider Stop()
94 {
95 ILedgerProvider Result = provider;
96 provider = new NullLedgerProvider();
97 locked = false;
98 return Result;
99 }
100
104 public static bool HasProvider
105 {
106 get => !(provider is null) && (!(provider is NullLedgerProvider) || locked);
107 }
108
112 public static bool Locked => locked;
113
118 public static async Task NewEntry(object Object)
119 {
120 await Provider.NewEntry(Object);
121 RaiseEntryAdded(Object);
122 }
123
124 private static void RaiseEntryAdded(object Object)
125 {
126 EntryAdded?.Raise(Provider, new ObjectEventArgs(Object));
127 }
128
132 public static event EventHandler<ObjectEventArgs> EntryAdded = null;
133
138 public static async Task UpdatedEntry(object Object)
139 {
140 await Provider.UpdatedEntry(Object);
141 RaiseEntryUpdated(Object);
142 }
143
144 private static void RaiseEntryUpdated(object Object)
145 {
146 EntryUpdated?.Raise(Provider, new ObjectEventArgs(Object));
147 }
148
152 public static event EventHandler<ObjectEventArgs> EntryUpdated = null;
153
158 public static async Task DeletedEntry(object Object)
159 {
160 await Provider.DeletedEntry(Object);
161 RaiseEntryDeleted(Object);
162 }
163
164 private static void RaiseEntryDeleted(object Object)
165 {
166 EntryDeleted?.Raise(Provider, new ObjectEventArgs(Object));
167 }
168
172 public static event EventHandler<ObjectEventArgs> EntryDeleted = null;
173
178 public static async Task ClearedCollection(string Collection)
179 {
180 await Provider.ClearedCollection(Collection);
181 RaiseCollectionCleared(Collection);
182 }
183
184 private static async void RaiseCollectionCleared(string Collection)
185 {
186 try
187 {
188 await CollectionCleared.Raise(Provider, new CollectionEventArgs(Collection));
189 }
190 catch (Exception ex)
191 {
192 Log.Exception(ex);
193 }
194 }
195
199 public static event EventHandlerAsync<CollectionEventArgs> CollectionCleared = null;
200
206 public static Task<ILedgerEnumerator<T>> GetEnumerator<T>()
207 {
208 return Provider.GetEnumerator<T>();
209 }
210
216 public static Task<ILedgerEnumerator<object>> GetEnumerator(string CollectionName)
217 {
218 return Provider.GetEnumerator(CollectionName);
219 }
220
225 public static Task<string[]> GetCollections()
226 {
227 return Provider.GetCollections();
228 }
229
235 public static Task<bool> Export(ILedgerExport Output)
236 {
237 return Provider.Export(Output, null);
238 }
239
247 public static Task<bool> Export(ILedgerExport Output, LedgerExportRestriction Restriction)
248 {
249 return Provider.Export(Output, Restriction);
250 }
251
260 public static Task<bool> Export(ILedgerExport Output, LedgerExportRestriction Restriction, ProfilerThread Thread)
261 {
262 return Provider.Export(Output, Restriction, Thread);
263 }
264
265 private static readonly object listeningSynchObj = new object();
266 private static int listeningCounter = 0;
267
274 {
275 lock (listeningSynchObj)
276 {
277 if (listeningCounter == 0)
278 {
279 Database.ObjectInserted += Database_ObjectInserted;
280 Database.ObjectUpdated += Database_ObjectUpdated;
281 Database.ObjectDeleted += Database_ObjectDeleted;
282 Database.CollectionCleared += Database_CollectionCleared;
283
284 listeningCounter++;
285 }
286 }
287
288 }
289
295 public static void StopListeningToDatabaseEvents()
296 {
297 lock (listeningSynchObj)
298 {
299 if (listeningCounter > 0)
300 {
301 listeningCounter--;
302
303 if (listeningCounter == 0)
304 {
305 Database.ObjectInserted -= Database_ObjectInserted;
306 Database.ObjectUpdated -= Database_ObjectUpdated;
307 Database.ObjectDeleted -= Database_ObjectDeleted;
308 Database.CollectionCleared -= Database_CollectionCleared;
309 }
310 }
311 }
312 }
313
314 private static async void Database_ObjectInserted(object Sender, ObjectEventArgs e)
315 {
316 try
317 {
319 await NewEntry(Obj);
320 }
321 catch (Exception ex)
322 {
323 Log.Exception(ex);
324 }
325 }
326
327 private static async void Database_ObjectUpdated(object Sender, ObjectEventArgs e)
328 {
329 try
330 {
332 await UpdatedEntry(Obj);
333 }
334 catch (Exception ex)
335 {
336 Log.Exception(ex);
337 }
338 }
339
340 private static async void Database_ObjectDeleted(object Sender, ObjectEventArgs e)
341 {
342 try
343 {
345 await DeletedEntry(Obj);
346 }
347 catch (Exception ex)
348 {
349 Log.Exception(ex);
350 }
351 }
352
353 private static async Task Database_CollectionCleared(object Sender, CollectionEventArgs e)
354 {
355 try
356 {
358 }
359 catch (Exception ex)
360 {
361 Log.Exception(ex);
362 }
363 }
364 }
365}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Event arguments for collection events.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< GenericObject > Generalize(object Object)
Creates a generalized representation of an object.
Definition: Database.cs:2473
Contains basic ledger export restrictions.
Static interface for ledger persistence. In order to work, a ledger provider has to be assigned to it...
Definition: Ledger.cs:14
static EventHandler< ObjectEventArgs > EntryUpdated
Event raised when an entry has been updated in the ledger.
Definition: Ledger.cs:152
static EventHandlerAsync< CollectionEventArgs > CollectionCleared
Event raised when a collection has been cleared.
Definition: Ledger.cs:199
static void Register(ILedgerProvider LedgerProvider)
Registers a ledger provider for use from the static Ledger class, throughout the lifetime of the appl...
Definition: Ledger.cs:25
static EventHandler< ObjectEventArgs > EntryDeleted
Event raised when an entry has been deleted in the ledger.
Definition: Ledger.cs:172
static bool Locked
If the datbase provider has been locked for the rest of the run-time of the application.
Definition: Ledger.cs:112
static bool HasProvider
If a ledger provider is registered.
Definition: Ledger.cs:105
static Task< bool > Export(ILedgerExport Output, LedgerExportRestriction Restriction)
Performs an export of the entire ledger.
Definition: Ledger.cs:247
static async Task DeletedEntry(object Object)
Deletes an entry in the ledger.
Definition: Ledger.cs:158
static ILedgerProvider Provider
Registered ledger provider.
Definition: Ledger.cs:83
static Task< string[]> GetCollections()
Gets an array of available collections.
Definition: Ledger.cs:225
static async Task NewEntry(object Object)
Adds an entry to the ledger.
Definition: Ledger.cs:118
static async Task ClearedCollection(string Collection)
Clears a collection in the ledger.
Definition: Ledger.cs:178
static Task< bool > Export(ILedgerExport Output, LedgerExportRestriction Restriction, ProfilerThread Thread)
Performs an export of the entire ledger.
Definition: Ledger.cs:260
static Task< bool > Export(ILedgerExport Output)
Performs an export of the entire ledger.
Definition: Ledger.cs:235
static EventHandler< ObjectEventArgs > EntryAdded
Event raised when an entry has been added to the ledger.
Definition: Ledger.cs:132
static async Task UpdatedEntry(object Object)
Updates an entry in the ledger.
Definition: Ledger.cs:138
static Task< ILedgerEnumerator< T > > GetEnumerator< T >()
Gets an eumerator for objects of type T .
Definition: Ledger.cs:206
static void StartListeningToDatabaseEvents()
Makes the ledger listen on database events. Each call to StartListeningToDatabaseEvents must be follo...
Definition: Ledger.cs:273
static Task< ILedgerEnumerator< object > > GetEnumerator(string CollectionName)
Gets an eumerator for objects in a collection.
Definition: Ledger.cs:216
static void Register(ILedgerProvider LedgerProvider, bool Lock)
Registers a ledger provider for use from the static Ledger class, throughout the lifetime of the appl...
Definition: Ledger.cs:38
static void StopListeningToDatabaseEvents()
Makes the ledger listen on database events. Each call to StartListeningToDatabaseEvents must be follo...
Definition: Ledger.cs:295
Event arguments for database object events.
Generic object. Contains a sequence of properties.
Class that keeps track of events and timing for one thread.
Interface for proxy for reporting changes to the ledger from external sources.
Interface for ledger providers that can be plugged into the static Ledger class.
Task ClearedCollection(string Collection)
Clears a collection in the ledger.
Task DeletedEntry(object Object)
Deletes an entry in the ledger.
Task< string[]> GetCollections()
Gets an array of available collections.
Task< bool > Export(ILedgerExport Output, LedgerExportRestriction Restriction)
Performs an export of the entire ledger.
void Unregister(ILedgerExternalEvents ExternalEvents)
Unregisters a recipient of external events.
Task NewEntry(object Object)
Adds an entry to the ledger.
void Register(ILedgerExternalEvents ExternalEvents)
Registers a recipient of external events.
Task< ILedgerEnumerator< object > > GetEnumerator(string CollectionName)
Gets an eumerator for objects in a collection.
Task UpdatedEntry(object Object)
Updates an entry in the ledger.