Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PreviewIdApplicationSource.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Events;
9using Waher.Script;
15
17{
22 {
26 public const string SourceID = "PreviewIdApplications";
27
28 private static readonly Dictionary<string, PreviewIdApplication> applications =
29 new Dictionary<string, PreviewIdApplication>();
30 private static DateTime loaded = DateTime.MinValue;
31
36 {
37 }
38
42 public string CollectionName => SourceID;
43
47 public string TypeName => string.Empty;
48
52 public string Name => SourceID;
53
59 public Grade Supports(string Name)
60 {
61 return Name == SourceID ? Grade.Perfect : Grade.NotAtAll;
62 }
63
69 public bool IsSource(string Name)
70 {
71 return Name == SourceID;
72 }
73
79 public Task<bool> IsLabel(string Label)
80 {
81 switch (Label)
82 {
83 case "Id":
84 case "Provider":
85 case "State":
86 case "Account":
87 case "Created":
88 case "Updated":
89 case "From":
90 case "To":
91 case "Properties":
92 case "ClientKeyName":
93 case "ServerSignature":
94 case "ClientPubKey":
95 case "ClientSignature":
96 case "Attachments":
97 case "NrPeerReviews":
98 case "Version":
99 return Task.FromResult(true);
100
101 default:
102 return Task.FromResult(false);
103 }
104 }
105
111 public Task CreateIndex(string Name, string[] Fields) => throw InvalidOperation();
112
117 public Task<bool> DropIndex(string Name) => throw InvalidOperation();
118
122 public Task DropCollection() => throw InvalidOperation();
123
124 private static InvalidOperationException InvalidOperation()
125 {
126 return new InvalidOperationException("The source is read-only.");
127 }
128
140 public Task<int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
141 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
142 {
143 throw InvalidOperation();
144 }
145
151 public Task Update(bool Lazy, IEnumerable<object> Objects) => throw InvalidOperation();
152
158 public Task Insert(bool Lazy, object Object) => throw InvalidOperation();
159
173 public async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
174 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
175 ScriptNode Node)
176 {
177 IResultSetEnumerator e = await this.Find(Offset, Top, Generic, Where, Variables, Order, Node);
178
179 if (Processor.IsAsynchronous)
180 {
181 while (await e.MoveNextAsync())
182 {
183 if (!await Processor.ProcessAsync(e.Current))
184 return false;
185 }
186
187 await Processor.FlushAsync();
188 }
189 else
190 {
191 while (await e.MoveNextAsync())
192 {
193 if (!Processor.Process(e.Current))
194 return false;
195 }
196
197 Processor.Flush();
198 }
199
200 return true;
201 }
202
214 public async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic,
215 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
216 ScriptNode Node)
217 {
218 PreviewIdApplication[] Applications;
219
220 using (Semaphore Lock = await Semaphores.BeginWrite(SourceID))
221 {
222 DateTime TP = DateTime.UtcNow;
223 int i, c;
224
225 if (TP.Subtract(loaded).TotalDays >= 1)
226 {
227 applications.Clear();
228
229 string PreviewFolder = LegalComponent.GetPreviewFolder();
230 string[] PreviewApplications = Directory.GetFiles(PreviewFolder, "*.id",
231 SearchOption.TopDirectoryOnly);
232
233 for (i = 0, c = PreviewApplications.Length; i < c; i++)
234 {
235 string FileName = PreviewApplications[i];
236 PreviewIdApplication Application = new PreviewIdApplication(FileName,
237 File.GetCreationTimeUtc(FileName),
238 File.GetLastAccessTimeUtc(FileName));
239
240 try
241 {
242 LegalIdentity Identity = await Application.Load();
243 if (!(Identity is null))
244 applications[FileName] = Application;
245 }
246 catch (Exception ex)
247 {
248 Log.Exception(ex);
249 }
250 }
251 }
252
253 i = 0;
254 c = applications.Count;
255
256 Applications = new PreviewIdApplication[c];
257
258 foreach (PreviewIdApplication Application in applications.Values)
259 Applications[i++] = Application.Clone(Variables, Node);
260
261 loaded = TP;
262 }
263
264 ObjectVector v = new ObjectVector(Applications);
265
266 return await VectorSource.Find(v, Offset, Top, Generic, Where, Variables,
267 Order, Node);
268 }
269
270 internal static async Task PreviewFileSaved(string FileName, LegalIdentity Identity)
271 {
272 PreviewIdApplication Application = new PreviewIdApplication(FileName,
273 File.GetCreationTimeUtc(FileName),
274 File.GetLastAccessTimeUtc(FileName),
275 Identity);
276
277 using Semaphore Lock = await Semaphores.BeginWrite(SourceID);
278
279 applications[FileName] = Application;
280 }
281
282 internal static async Task PreviewFileDeleted(string FileName)
283 {
284 using Semaphore Lock = await Semaphores.BeginWrite(SourceID);
285
286 applications.Remove(FileName);
287 }
288 }
289}
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
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:17
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:91
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
Definition: VectorSource.cs:75
Collection of variables.
Definition: Variables.cs:25
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for processors of objects.
Definition: IProcessor.cs:9
bool Process(T Object)
Processes an object synchronously.
bool Flush()
Called at the end of processing, to allow for flushing of buffers, etc.
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Task< bool > FlushAsync()
Called at the end of processing, to allow for flushing of buffers, etc.
Task< bool > ProcessAsync(T Object)
Processes an object asynchronously.
Interface for named data sources that can be used in SQL statements.
Grade
Grade enumeration
Definition: Grade.cs:7