forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedger.cs
More file actions
165 lines (148 loc) · 4.7 KB
/
Copy pathLedger.cs
File metadata and controls
165 lines (148 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Waher.Persistence.Filters;
using Waher.Persistence.Serialization;
namespace Waher.Persistence
{
/// <summary>
/// Static interface for ledger persistence. In order to work, a ledger provider has to be assigned to it. This is
/// ideally done as one of the first steps in the startup of an application.
/// </summary>
public static class Ledger
{
private static ILedgerProvider provider = null;
private static bool locked = false;
/// <summary>
/// Registers a ledger provider for use from the static <see cref="Ledger"/> class,
/// throughout the lifetime of the application.
///
/// Note: Only one ledger provider can be registered.
/// </summary>
/// <param name="LedgerProvider">Ledger provider to use.</param>
public static void Register(ILedgerProvider LedgerProvider)
{
Register(LedgerProvider, true);
}
/// <summary>
/// Registers a ledger provider for use from the static <see cref="Ledger"/> class,
/// throughout the lifetime of the application.
///
/// Note: Only one ledger provider can be registered.
/// </summary>
/// <param name="LedgerProvider">Ledger provider to use.</param>
/// <param name="Lock">If the ledger provider should be locked for the rest of the running time of the application.</param>
public static void Register(ILedgerProvider LedgerProvider, bool Lock)
{
if (provider != null && locked)
throw new Exception("A ledger provider is already registered.");
provider = LedgerProvider;
locked = Lock;
}
/// <summary>
/// Registered ledger provider.
/// </summary>
public static ILedgerProvider Provider
{
get
{
if (!(provider is null))
return provider;
else
throw new Exception("A ledger provider has not been registered.");
}
}
internal static ILedgerProvider Stop()
{
ILedgerProvider Result = provider;
provider = new NullLedgerProvider();
locked = false;
return Result;
}
/// <summary>
/// If a ledger provider is registered.
/// </summary>
public static bool HasProvider
{
get => !(provider is null) && (!(provider is NullLedgerProvider) || locked);
}
/// <summary>
/// If the datbase provider has been locked for the rest of the run-time of the application.
/// </summary>
public static bool Locked
{
get { return locked; }
}
/// <summary>
/// Adds an entry to the ledger.
/// </summary>
/// <param name="Object">New object.</param>
public static Task NewEntry(object Object)
{
return Provider.NewEntry(Object);
}
/// <summary>
/// Updates an entry in the ledger.
/// </summary>
/// <param name="Object">Updated object.</param>
public static Task UpdatedEntry(object Object)
{
return Provider.UpdatedEntry(Object);
}
/// <summary>
/// Deletes an entry in the ledger.
/// </summary>
/// <param name="Object">Deleted object.</param>
public static Task DeletedEntry(object Object)
{
return Provider.DeletedEntry(Object);
}
/// <summary>
/// Gets an eumerator for objects of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Type of object entries to enumerate.</typeparam>
/// <returns>Enumerator object.</returns>
public static Task<ILedgerEnumerator<T>> GetEnumerator<T>()
{
return Provider.GetEnumerator<T>();
}
/// <summary>
/// Gets an eumerator for objects in a collection.
/// </summary>
/// <param name="CollectionName">Collection to enumerate.</param>
/// <returns>Enumerator object.</returns>
public static Task<ILedgerEnumerator<object>> GetEnumerator(string CollectionName)
{
return Provider.GetEnumerator(CollectionName);
}
/// <summary>
/// Gets an array of available collections.
/// </summary>
/// <returns>Array of collections.</returns>
public static Task<string[]> GetCollections()
{
return Provider.GetCollections();
}
/// <summary>
/// Performs an export of the entire ledger.
/// </summary>
/// <param name="Output">Ledger will be output to this interface.</param>
/// <returns>Task object for synchronization purposes.</returns>
public static Task Export(ILedgerExport Output)
{
return Provider.Export(Output, null);
}
/// <summary>
/// Performs an export of the entire ledger.
/// </summary>
/// <param name="Output">Ledger will be output to this interface.</param>
/// <param name="CollectionNames">Optional array of collections to export. If null, all collections will be exported.</param>
/// <returns>Task object for synchronization purposes.</returns>
public static Task Export(ILedgerExport Output, string[] CollectionNames)
{
return Provider.Export(Output, CollectionNames);
}
}
}