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;
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
163 public virtual Task<bool> StartDatabase(IDatabaseProvider Provider)
164 {
165 return Task.FromResult(true);
166 }
167
172 public virtual Task<bool> EndDatabase()
173 {
174 return Task.FromResult(true);
175 }
176
182 public virtual Task<bool> StartLedger(ILedgerProvider Provider)
183 {
184 return Task.FromResult(true);
185 }
186
191 public virtual Task<bool> EndLedger()
192 {
193 return Task.FromResult(true);
194 }
195
201 public virtual Task<bool> StartCollection(string CollectionName)
202 {
203 this.collectionName = CollectionName;
204 this.nrCollections++;
205
206 return Task.FromResult(true);
207 }
208
213 public virtual Task<bool> EndCollection()
214 {
215 this.collectionName = null;
216 return Task.FromResult(true);
217 }
218
223 public virtual Task<bool> StartIndex()
224 {
225 this.index.Clear();
226 this.nrIndices++;
227 return Task.FromResult(true);
228 }
229
234 public virtual Task<bool> EndIndex()
235 {
236 return Task.FromResult(true);
237 }
238
245 public virtual Task<bool> ReportIndexField(string FieldName, bool Ascending)
246 {
247 if (Ascending)
248 this.index.Add(FieldName);
249 else
250 this.index.Add("-" + FieldName);
251
252 return Task.FromResult(true);
253 }
254
260 public virtual Task<bool> StartBlock(string BlockID)
261 {
262 this.nrBlocks++;
263 return Task.FromResult(true);
264 }
265
270 public virtual Task<bool> EndBlock()
271 {
272 return Task.FromResult(true);
273 }
274
281 public virtual Task<bool> BlockMetaData(string Key, object Value)
282 {
283 return Task.FromResult(true);
284 }
285
292 public virtual Task<string> StartObject(string ObjectId, string TypeName)
293 {
294 this.objectId = this.MapObjectId(ObjectId);
295 this.typeName = TypeName;
296 this.nrObjects++;
297
298 return Task.FromResult(ObjectId);
299 }
300
301 private string MapObjectId(string ObjectId)
302 {
303 if (this.mapObjectIds)
304 {
305 string ObjectId0 = ObjectId;
306
307 if (!this.objectIdMap.TryGetValue(ObjectId0, out ObjectId))
308 {
309 byte[] A;
310 int i, c;
311
312 if (System.Guid.TryParse(ObjectId0, out Guid Guid))
313 A = Guid.ToByteArray();
314 else
315 A = Hashes.StringToBinary(ObjectId0);
316
317 if (!(A is null) && (c = A.Length) != this.objectIdByteCount)
318 {
319 if (c > this.objectIdByteCount)
320 {
321 if (c == 16)
322 {
323 for (i = this.objectIdByteCount; i < c; i++)
324 A[i] = 0;
325
326 ObjectId = new Guid(A).ToString();
327 }
328 else
329 {
330 Array.Resize(ref A, this.objectIdByteCount);
331 ObjectId = Hashes.BinaryToString(A);
332 }
333 }
334 else
335 {
336 Array.Resize(ref A, this.objectIdByteCount);
337
338 if (this.objectIdByteCount == 16)
339 ObjectId = new Guid(A).ToString();
340 else
341 ObjectId = Hashes.BinaryToString(A);
342 }
343
344 while (this.objectIdMap.ContainsKey(ObjectId))
345 {
346 if (this.objectIdByteCount == 16)
347 ObjectId = Guid.NewGuid().ToString();
348 else
349 {
350 A = Gateway.NextBytes(this.objectIdByteCount);
351 ObjectId = Hashes.BinaryToString(A);
352 }
353 }
354
355 this.objectIdMap[ObjectId0] = ObjectId;
356 }
357 else
358 ObjectId = ObjectId0;
359 }
360 }
361
362 return ObjectId;
363 }
364
369 public virtual Task<bool> EndObject()
370 {
371 this.objectId = null;
372 this.typeName = null;
373
374 return Task.FromResult(true);
375 }
376
385 public virtual Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
386 {
387 this.objectId = this.MapObjectId(ObjectId);
388 this.typeName = TypeName;
389 this.nrEntries++;
390
391 return Task.FromResult(true);
392 }
393
398 public virtual Task<bool> EndEntry()
399 {
400 this.objectId = null;
401 this.typeName = null;
402
403 return Task.FromResult(true);
404 }
405
411 public virtual async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
412 {
413 return
414 await this.StartEntry(string.Empty, string.Empty, EntryType.Clear, EntryTimestamp) &&
415 await this.EndEntry();
416 }
417
424 public virtual Task<bool> ReportProperty(string PropertyName, object PropertyValue)
425 {
426 this.nrProperties++;
427 return Task.FromResult(true);
428 }
429
434 public virtual Task<bool> StartFiles()
435 {
436 return Task.FromResult(true);
437 }
438
443 public virtual Task<bool> EndFiles()
444 {
445 return Task.FromResult(true);
446 }
447
454 public virtual Task<bool> ExportFile(string FileName, Stream File)
455 {
456 this.nrFiles++;
457 this.nrFileBytes += File.Length;
458
459 return Task.FromResult(true);
460 }
461
467 public virtual Task<bool> ReportError(string Message)
468 {
469 return Task.FromResult(true);
470 }
471
477 public virtual Task<bool> ReportException(Exception Exception)
478 {
479 return Task.FromResult(true);
480 }
481
487 public virtual Task<bool> UpdateClient(bool ForceUpdate)
488 {
489 return Task.FromResult(true);
490 }
491 }
492}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:4335
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.
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 > StartDatabase(IDatabaseProvider Provider)
Is called when export of database is started.
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.
virtual Task< bool > StartLedger(ILedgerProvider Provider)
Is called when export of ledger is started.
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 > 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:21
static IDatabaseProvider Provider
Registered database provider.
Definition: Database.cs:59
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:100
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:63
Interface for database providers that can be plugged into the static Database class.
int ObjectIdByteCount
Number of bytes used by an Object ID.
Interface for ledger providers that can be plugged into the static Ledger class.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9