Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ValidateBackupFile.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
8
10{
15 {
19 protected List<string> index = new List<string>();
20
24 protected string collectionName = null;
25
29 protected string objectId = null;
30
34 protected string typeName = null;
35
39 protected readonly Dictionary<string, string> objectIdMap;
40
44 protected readonly bool mapObjectIds;
45
46 private readonly string fileName;
47 private readonly int objectIdByteCount;
48 private long nrCollections = 0;
49 private long nrIndices = 0;
50 private long nrBlocks = 0;
51 private long nrObjects = 0;
52 private long nrEntries = 0;
53 private long nrProperties = 0;
54 private long nrFiles = 0;
55 private long nrFileBytes = 0;
56
62 public ValidateBackupFile(string FileName, Dictionary<string, string> ObjectIdMap)
63 {
64 this.fileName = FileName;
65
66 if ((this.objectIdByteCount = Database.Provider.ObjectIdByteCount) < 16)
67 {
68 this.objectIdMap = ObjectIdMap ?? new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
69 this.mapObjectIds = true;
70 }
71 else
72 {
73 this.objectIdMap = null;
74 this.mapObjectIds = false;
75 }
76 }
77
81 public string FileName => this.fileName;
82
86 public long NrCollections => this.nrCollections;
87
91 public long NrIndices => this.nrIndices;
92
96 public long NrBlocks => this.nrBlocks;
97
101 public long NrObjects => this.nrObjects;
102
106 public long NrEntries => this.nrEntries;
107
111 public long NrProperties => this.nrProperties;
112
116 public long NrFiles => this.nrFiles;
117
121 public long NrFileBytes => this.nrFileBytes;
122
126 public Dictionary<string, string> ObjectIdMap => this.objectIdMap;
127
131 public virtual void Dispose()
132 {
133 }
134
138 public string[] CollectionNames => null;
139
144 public virtual Task<bool> Start()
145 {
146 return Task.FromResult(true);
147 }
148
153 public virtual Task<bool> End()
154 {
155 return Task.FromResult(true);
156 }
157
162 public virtual Task<bool> StartDatabase()
163 {
164 return Task.FromResult(true);
165 }
166
171 public virtual Task<bool> EndDatabase()
172 {
173 return Task.FromResult(true);
174 }
175
180 public virtual Task<bool> StartLedger()
181 {
182 return Task.FromResult(true);
183 }
184
189 public virtual Task<bool> EndLedger()
190 {
191 return Task.FromResult(true);
192 }
193
199 public virtual Task<bool> StartCollection(string CollectionName)
200 {
201 this.collectionName = CollectionName;
202 this.nrCollections++;
203
204 return Task.FromResult(true);
205 }
206
211 public virtual Task<bool> EndCollection()
212 {
213 this.collectionName = null;
214 return Task.FromResult(true);
215 }
216
221 public virtual Task<bool> StartIndex()
222 {
223 this.index.Clear();
224 this.nrIndices++;
225 return Task.FromResult(true);
226 }
227
232 public virtual Task<bool> EndIndex()
233 {
234 return Task.FromResult(true);
235 }
236
243 public virtual Task<bool> ReportIndexField(string FieldName, bool Ascending)
244 {
245 if (Ascending)
246 this.index.Add(FieldName);
247 else
248 this.index.Add("-" + FieldName);
249
250 return Task.FromResult(true);
251 }
252
258 public virtual Task<bool> StartBlock(string BlockID)
259 {
260 this.nrBlocks++;
261 return Task.FromResult(true);
262 }
263
268 public virtual Task<bool> EndBlock()
269 {
270 return Task.FromResult(true);
271 }
272
279 public virtual Task<bool> BlockMetaData(string Key, object Value)
280 {
281 return Task.FromResult(true);
282 }
283
290 public virtual Task<string> StartObject(string ObjectId, string TypeName)
291 {
292 this.objectId = this.MapObjectId(ObjectId);
293 this.typeName = TypeName;
294 this.nrObjects++;
295
296 return Task.FromResult(ObjectId);
297 }
298
299 private string MapObjectId(string ObjectId)
300 {
301 if (this.mapObjectIds)
302 {
303 string ObjectId0 = ObjectId;
304
305 if (!this.objectIdMap.TryGetValue(ObjectId0, out ObjectId))
306 {
307 byte[] A;
308 int i, c;
309
310 if (System.Guid.TryParse(ObjectId0, out Guid Guid))
311 A = Guid.ToByteArray();
312 else
313 A = Hashes.StringToBinary(ObjectId0);
314
315 if (!(A is null) && (c = A.Length) != this.objectIdByteCount)
316 {
317 if (c > this.objectIdByteCount)
318 {
319 if (c == 16)
320 {
321 for (i = this.objectIdByteCount; i < c; i++)
322 A[i] = 0;
323
324 ObjectId = new Guid(A).ToString();
325 }
326 else
327 {
328 Array.Resize(ref A, this.objectIdByteCount);
329 ObjectId = Hashes.BinaryToString(A);
330 }
331 }
332 else
333 {
334 Array.Resize(ref A, this.objectIdByteCount);
335
336 if (this.objectIdByteCount == 16)
337 ObjectId = new Guid(A).ToString();
338 else
339 ObjectId = Hashes.BinaryToString(A);
340 }
341
342 while (this.objectIdMap.ContainsKey(ObjectId))
343 {
344 if (this.objectIdByteCount == 16)
345 ObjectId = Guid.NewGuid().ToString();
346 else
347 {
348 A = Gateway.NextBytes(this.objectIdByteCount);
349 ObjectId = Hashes.BinaryToString(A);
350 }
351 }
352
353 this.objectIdMap[ObjectId0] = ObjectId;
354 }
355 else
356 ObjectId = ObjectId0;
357 }
358 }
359
360 return ObjectId;
361 }
362
367 public virtual Task<bool> EndObject()
368 {
369 this.objectId = null;
370 this.typeName = null;
371
372 return Task.FromResult(true);
373 }
374
383 public virtual Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
384 {
385 this.objectId = this.MapObjectId(ObjectId);
386 this.typeName = TypeName;
387 this.nrEntries++;
388
389 return Task.FromResult(true);
390 }
391
396 public virtual Task<bool> EndEntry()
397 {
398 this.objectId = null;
399 this.typeName = null;
400
401 return Task.FromResult(true);
402 }
403
409 public virtual async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
410 {
411 return
412 await this.StartEntry(string.Empty, string.Empty, EntryType.Clear, EntryTimestamp) &&
413 await this.EndEntry();
414 }
415
422 public virtual Task<bool> ReportProperty(string PropertyName, object PropertyValue)
423 {
424 this.nrProperties++;
425 return Task.FromResult(true);
426 }
427
432 public virtual Task<bool> StartFiles()
433 {
434 return Task.FromResult(true);
435 }
436
441 public virtual Task<bool> EndFiles()
442 {
443 return Task.FromResult(true);
444 }
445
452 public virtual Task<bool> ExportFile(string FileName, Stream File)
453 {
454 this.nrFiles++;
455 this.nrFileBytes += File.Length;
456
457 return Task.FromResult(true);
458 }
459
465 public virtual Task<bool> ReportError(string Message)
466 {
467 return Task.FromResult(true);
468 }
469
475 public virtual Task<bool> ReportException(Exception Exception)
476 {
477 return Task.FromResult(true);
478 }
479
485 public virtual Task<bool> UpdateClient(bool ForceUpdate)
486 {
487 return Task.FromResult(true);
488 }
489 }
490}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
Class validating the status of a backup file.
virtual Task< bool > EndIndex()
Is called when an index in a collection is finished.
long NrFiles
Number of files processed.
virtual Task< bool > EndBlock()
Is called when a block in a collection is finished.
virtual Task< bool > StartLedger()
Is called when export of ledger is started.
long NrEntries
Number of entries processed.
string objectId
ID of current object being exported.
virtual Task< bool > Start()
Starts export
Dictionary< string, string > ObjectIdMap
Object ID mapping, if available.
virtual Task< bool > EndCollection()
Is called when a collection is finished.
long NrCollections
Number of collections processed.
virtual void Dispose()
IDisposable.Dispose
virtual Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
virtual Task< bool > StartIndex()
Is called when an index in a collection is started.
string typeName
Type Name of current object being exported.
virtual Task< bool > End()
Ends export
long NrIndices
Number of indices processed.
List< string > index
Current index being exported.
ValidateBackupFile(string FileName, Dictionary< string, string > ObjectIdMap)
Class validating the status of a backup file.
string[] CollectionNames
Optional array of collection nmes to export. If null, all collections will be exported.
virtual Task< bool > EndLedger()
Is called when export of ledger is finished.
virtual Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
virtual Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
string collectionName
Current collection being exported.
virtual Task< bool > EndDatabase()
Is called when export of database is finished.
readonly bool mapObjectIds
If Object IDs are mapped.
long NrBlocks
Number of blocks processed.
virtual Task< string > StartObject(string ObjectId, string TypeName)
Is called when an object is started.
virtual Task< bool > UpdateClient(bool ForceUpdate)
If any clients should be updated about export status.
long NrFileBytes
Number of file byets processed.
virtual Task< bool > ExportFile(string FileName, Stream File)
Export file.
virtual Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
virtual Task< bool > StartFiles()
Starts export of files.
virtual Task< bool > ReportIndexField(string FieldName, bool Ascending)
Is called when a field in an index is reported.
virtual Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
long NrProperties
Number of propeties processed.
virtual Task< bool > ReportError(string Message)
Is called when an error is reported.
readonly Dictionary< string, string > objectIdMap
Object ID map, if available
long NrObjects
Number of objects processed.
virtual Task< bool > StartDatabase()
Is called when export of database is started.
virtual Task< bool > EndFiles()
Ends export of files.
virtual Task< bool > EndEntry()
Is called when an entry is finished.
virtual async Task< bool > CollectionCleared(DateTimeOffset EntryTimestamp)
Is called when a collection has been cleared.
virtual Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
virtual Task< bool > EndObject()
Is called when an object is finished.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static IDatabaseProvider Provider
Registered database provider.
Definition: Database.cs:57
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
int ObjectIdByteCount
Number of bytes used by an Object ID.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9