Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RestoreBackupFile.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Reflection;
5using System.Runtime.ExceptionServices;
6using System.Threading.Tasks;
7using Waher.Events;
11using Waher.Security;
12
14{
19 {
20 private readonly List<object> objects = new List<object>();
21 private GenericObject obj = null;
22 private EntryType entryType;
23 private int nrObjectsInBulk = 0;
24 private int nrObjectsFailed = 0;
25
31 public RestoreBackupFile(string FileName, Dictionary<string, string> ObjectIdMap)
32 : base(FileName, ObjectIdMap)
33 {
34 }
35
39 public int NrObjectsFailed => this.nrObjectsFailed;
40
45 public override Task<bool> Start()
46 {
47 PropertyInfo PI = Database.Provider.GetType().GetProperty("DeleteObsoleteKeys");
48 if (!(PI is null) && PI.PropertyType == typeof(bool))
49 PI.SetValue(Database.Provider, false);
50
51 return base.Start();
52 }
53
58 public override Task<bool> End()
59 {
60 PropertyInfo PI = Database.Provider.GetType().GetProperty("DeleteObsoleteKeys");
61 if (!(PI is null) && PI.PropertyType == typeof(bool))
62 PI.SetValue(Database.Provider, true);
63
64 return base.End();
65 }
66
72 public override async Task<bool> StartCollection(string CollectionName)
73 {
74 if (!await base.StartCollection(CollectionName))
75 return false;
76
77 await Database.Provider.Clear(CollectionName);
78 await Database.StartBulk();
79 this.nrObjectsInBulk = 0;
80
81 return true;
82 }
83
88 public override async Task<bool> EndCollection()
89 {
90 int c = this.objects.Count;
91 if (c > 0)
92 {
93 try
94 {
95 await Database.Provider.Insert(this.objects);
96 }
97 catch (Exception ex)
98 {
99 Log.Exception(ex);
100 this.nrObjectsFailed += this.objects.Count;
101 }
102
103 this.objects.Clear();
104
105 this.nrObjectsInBulk += c;
106 }
107
108 await Database.EndBulk();
109
110 return await base.EndCollection();
111 }
112
117 public override async Task<bool> EndIndex()
118 {
119 await Database.Provider.AddIndex(this.collectionName, this.index.ToArray());
120 return true;
121 }
122
129 public override async Task<string> StartObject(string ObjectId, string TypeName)
130 {
131 ObjectId = await base.StartObject(ObjectId, TypeName);
132 this.obj = new GenericObject(this.collectionName, TypeName, Guid.Parse(ObjectId));
133 return ObjectId;
134 }
135
140 public override async Task<bool> EndObject()
141 {
142 if (!(this.obj is null))
143 {
144 this.objects.Add(this.obj);
145 this.obj = null;
146
147 int c = this.objects.Count;
148 if (c >= 100)
149 {
150 try
151 {
152 await Database.Provider.Insert(this.objects);
153 }
154 catch (Exception ex)
155 {
156 Log.Exception(ex);
157 this.nrObjectsFailed += this.objects.Count;
158 }
159
160 this.objects.Clear();
161
162 this.nrObjectsInBulk += c;
163 if (this.nrObjectsInBulk >= 1000)
164 {
165 await Database.EndBulk();
166 await Database.StartBulk();
167 this.nrObjectsInBulk = 0;
168 }
169 }
170 }
171
172 return await base.EndObject();
173 }
174
181 public override Task<bool> ReportProperty(string PropertyName, object PropertyValue)
182 {
183 if (!(this.obj is null))
184 {
185 if (this.mapObjectIds)
186 {
187 if (PropertyValue is string s)
188 {
189 if (this.objectIdMap.TryGetValue(s, out s))
190 PropertyValue = s;
191 }
192 else if (PropertyValue is Guid id)
193 {
194 s = id.ToString();
195
196 if (this.objectIdMap.TryGetValue(s, out string s2))
197 {
198 if (Guid.TryParse(s2, out id))
199 PropertyValue = id;
200 else
201 PropertyValue = s2;
202 }
203 }
204 else if (PropertyValue is byte[] bin)
205 {
206 s = Hashes.BinaryToString(bin);
207
208 if (this.objectIdMap.TryGetValue(s, out string s2))
209 {
210 if (Guid.TryParse(s2, out id))
211 PropertyValue = id;
212 else
213 {
214 byte[] bin2 = Hashes.StringToBinary(s2);
215
216 if (bin2 is null)
217 PropertyValue = s2;
218 else if (bin2.Length == 16)
219 PropertyValue = new Guid(bin2);
220 else
221 PropertyValue = bin2;
222 }
223 }
224 }
225 }
226
227 this.obj[PropertyName] = PropertyValue;
228 }
229
230 return base.ReportProperty(PropertyName, PropertyValue);
231 }
232
241 public override async Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
242 {
243 if (!await base.StartEntry(ObjectId, TypeName, EntryType, EntryTimestamp))
244 return false;
245
246 this.obj = new GenericObject(this.collectionName, TypeName, string.IsNullOrEmpty(ObjectId) ? Guid.Empty : Guid.Parse(ObjectId));
247 this.entryType = EntryType;
248
249 return true;
250 }
251
256 public override async Task<bool> EndEntry()
257 {
258 if (this.entryType == EntryType.Clear)
260 else if (!(this.obj is null))
261 {
263
264 switch (this.entryType)
265 {
266 case EntryType.New:
267 case EntryType.Update:
268 if (Obj2 is null)
269 await Database.Provider.Insert(this.obj);
270 else if (!Obj2.Equals(this.obj))
271 await Database.Provider.Update(this.obj);
272 break;
273
274 case EntryType.Delete:
275 if (!(Obj2 is null))
276 await Database.Provider.Delete(Obj2);
277 break;
278 }
279
280 this.obj = null;
281 this.nrObjectsInBulk++;
282 if (this.nrObjectsInBulk >= 1000)
283 {
284 await Database.EndBulk();
285 await Database.StartBulk();
286 this.nrObjectsInBulk = 0;
287 }
288 }
289
290 return await base.EndEntry();
291 }
292
297 public override Task<bool> StartFiles()
298 {
299 string Folder2;
300
302 {
303 foreach (string Folder in Rec.Folders)
304 {
305 if (Directory.Exists(Folder2 = Path.Combine(Gateway.RootFolder, Folder)))
306 Directory.Delete(Folder2, true);
307 }
308 }
309
310 return base.StartFiles();
311 }
312
319 public override async Task<bool> ExportFile(string FileName, Stream File)
320 {
321 await base.ExportFile(FileName, File);
322
323 string Folder = Path.GetDirectoryName(FileName);
324 if (!Directory.Exists(Folder))
325 Directory.CreateDirectory(Folder);
326
327 try
328 {
329 using (FileStream f = System.IO.File.Create(FileName))
330 {
331 await File.CopyToAsync(f);
332 }
333 }
334 catch (Exception ex)
335 {
336 using (FileStream f = System.IO.File.OpenRead(FileName))
337 {
338 if (File.Length == f.Length)
339 {
340 File.Position = 0;
341
342 byte[] Buf1 = new byte[65536];
343 byte[] Buf2 = new byte[65536];
344 long l = File.Length;
345 int i, c;
346 bool Same = true;
347
348 while ((c = (int)Math.Min(l - File.Position, 65536)) > 0)
349 {
350 await File.ReadAllAsync(Buf1, 0, c);
351 await f.ReadAllAsync(Buf2, 0, c);
352
353 for (i = 0; i < c; i++)
354 {
355 if (Buf1[i] != Buf2[i])
356 {
357 Same = false;
358 break;
359 }
360 }
361 }
362
363 if (!Same)
364 ExceptionDispatchInfo.Capture(ex).Throw();
365 }
366 }
367 }
368
369 return true;
370 }
371
372 }
373}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Information about an exportable folder category
Definition: Export.cs:560
Static class managing data export.
Definition: Export.cs:19
static FolderCategory[] GetRegisteredFolders()
Gets registered exportable folders.
Definition: Export.cs:543
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static string RootFolder
Web root folder.
Definition: Gateway.cs:2379
Class restoring the contents of a backup file.
int NrObjectsFailed
Number of objects that have not been possible to import.
override Task< bool > End()
Ends export
override Task< bool > StartFiles()
Starts export of files.
override async Task< string > StartObject(string ObjectId, string TypeName)
Is called when an object is started.
override async Task< bool > ExportFile(string FileName, Stream File)
Export file.
override async Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
override Task< bool > Start()
Starts export
override async Task< bool > EndObject()
Is called when an object is finished.
override async Task< bool > EndEntry()
Is called when an entry is finished.
override async Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
override async Task< bool > EndIndex()
Is called when an index in a collection is finished.
override async Task< bool > EndCollection()
Is called when a collection is finished.
override Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
RestoreBackupFile(string FileName, Dictionary< string, string > ObjectIdMap)
Class restoring the contents of a backup file.
Class validating the status of a backup file.
string objectId
ID of current object being exported.
Dictionary< string, string > ObjectIdMap
Object ID mapping, if available.
List< string > index
Current index being exported.
string collectionName
Current collection being exported.
readonly bool mapObjectIds
If Object IDs are mapped.
readonly Dictionary< string, string > objectIdMap
Object ID map, if available
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task EndBulk()
Ends bulk-processing of data. Must be called once for every call to StartBulk.
Definition: Database.cs:1494
static IDatabaseProvider Provider
Registered database provider.
Definition: Database.cs:57
static Task StartBulk()
Starts bulk-proccessing of data. Must be followed by a call to EndBulk.
Definition: Database.cs:1486
Generic object. Contains a sequence of properties.
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
Task Update(object Object)
Updates an object in the database.
Task< object > TryLoadObject(string CollectionName, object ObjectId)
Tries to load an object given its Object ID ObjectId and its collection name CollectionName .
Task AddIndex(string CollectionName, string[] FieldNames)
Adds an index to a collection, if one does not already exist.
Task Delete(object Object)
Deletes an object in the database.
Task Start()
Called when processing starts.
Task Clear(string CollectionName)
Clears a collection of all objects.
Task Insert(object Object)
Inserts an object into the database.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9