Changeset 17068 for stable/HeuristicLab.Persistence/3.3/Default/Xml
- Timestamp:
- 07/04/19 15:26:27 (5 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Persistence
- Property svn:mergeinfo changed
/branches/2965_CancelablePersistence/HeuristicLab.Persistence (added) merged: 16325,16433 /trunk/HeuristicLab.Persistence reverse-merged: 16117,16356,16389 /trunk/sources/HeuristicLab.Persistence reverse-merged: 15581,15583,15589
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r15584 r17068 26 26 using System.Linq; 27 27 using System.Text; 28 using System.Threading; 28 29 using HeuristicLab.Persistence.Core; 29 30 using HeuristicLab.Persistence.Core.Tokens; … … 61 62 } 62 63 63 protected enum NodeType { Start, End, Inline } 64 protected enum NodeType { Start, End, Inline }; 64 65 65 66 protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) { … … 256 257 {"typeName", lastTypeToken.TypeName }, 257 258 {"serializer", lastTypeToken.Serializer }}); 258 } 259 finally { 259 } finally { 260 260 lastTypeToken = null; 261 261 } … … 284 284 /// <param name="o">The object.</param> 285 285 /// <param name="filename">The filename.</param> 286 public static void Serialize(object o, string filename ) {287 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal );286 public static void Serialize(object o, string filename, CancellationToken cancellationToken = default(CancellationToken)) { 287 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal, cancellationToken); 288 288 } 289 289 … … 296 296 /// <param name="filename">The filename.</param> 297 297 /// <param name="compression">ZIP file compression level</param> 298 public static void Serialize(object o, string filename, CompressionLevel compression ) {299 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression );298 public static void Serialize(object o, string filename, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 299 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression, cancellationToken); 300 300 } 301 301 … … 307 307 /// <param name="filename">The filename.</param> 308 308 /// <param name="config">The configuration.</param> 309 public static void Serialize(object obj, string filename, Configuration config) { 310 Serialize(obj, filename, config, false, CompressionLevel.Optimal); 311 } 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies 314 , CompressionLevel compression) { 309 public static void Serialize(object obj, string filename, Configuration config, CancellationToken cancellationToken = default(CancellationToken)) { 310 Serialize(obj, filename, config, false, CompressionLevel.Optimal, cancellationToken); 311 } 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 315 314 Serializer serializer = new Serializer(obj, config); 316 Serialize(stream, includeAssemblies, compression, serializer );317 } 318 319 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer ) {315 Serialize(stream, includeAssemblies, compression, serializer, cancellationToken); 316 } 317 318 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) { 320 319 try { 320 cancellationToken.ThrowIfCancellationRequested(); 321 321 DateTime start = DateTime.Now; 322 322 serializer.InterleaveTypeInformation = false; … … 326 326 using (StreamWriter writer = new StreamWriter(entry.Open())) { 327 327 foreach (ISerializationToken token in serializer) { 328 cancellationToken.ThrowIfCancellationRequested(); 328 329 string line = generator.Format(token); 329 330 writer.Write(line); … … 338 339 if (includeAssemblies) { 339 340 foreach (string name in serializer.RequiredFiles) { 341 cancellationToken.ThrowIfCancellationRequested(); 340 342 Uri uri = new Uri(name); 341 343 if (!uri.IsFile) { … … 348 350 byte[] buffer = new byte[1024 * 1024]; 349 351 while (true) { 352 cancellationToken.ThrowIfCancellationRequested(); 350 353 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 351 354 if (bytesRead == 0) … … 360 363 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 361 364 (DateTime.Now - start).TotalSeconds, compression)); 362 } 363 catch (Exception) { 365 } catch (Exception) { 364 366 Logger.Warn("Exception caught, no data has been serialized."); 365 367 throw; … … 375 377 /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param> 376 378 /// <param name="compression">The ZIP compression level.</param> 377 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) { 379 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 380 string tempfile = null; 378 381 try { 379 stringtempfile = Path.GetTempFileName();382 tempfile = Path.GetTempFileName(); 380 383 381 384 using (FileStream stream = File.Create(tempfile)) { 382 Serialize(obj, stream, config, includeAssemblies, compression );383 } 384 385 Serialize(obj, stream, config, includeAssemblies, compression, cancellationToken); 386 } 387 // copy only if needed 385 388 File.Copy(tempfile, filename, true); 386 File.Delete(tempfile); 387 } 388 catch (Exception) { 389 } catch (Exception) { 389 390 Logger.Warn("Exception caught, no data has been written."); 390 391 throw; 392 } finally { 393 if (tempfile != null && File.Exists(tempfile)) 394 File.Delete(tempfile); 391 395 } 392 396 } … … 399 403 /// <param name="stream">The stream.</param> 400 404 /// <param name="compressionType">Type of compression, default is GZip.</param> 401 public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip ) {402 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType );405 public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 406 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType, cancellationToken); 403 407 } 404 408 … … 411 415 /// <param name="config">The configuration.</param> 412 416 /// <param name="compressionType">Type of compression, default is GZip.</param> 413 public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip ) {414 Serialize(obj, stream, config, false, compressionType );417 public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 418 Serialize(obj, stream, config, false, compressionType, cancellationToken); 415 419 } 416 420 … … 423 427 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 424 428 /// <param name="compressionType">Type of compression, default is GZip.</param> 425 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, 426 CompressionType compressionType = CompressionType.GZip) { 429 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 427 430 try { 428 431 Serializer serializer = new Serializer(obj, config); 429 432 if (compressionType == CompressionType.Zip) { 430 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal );433 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal, cancellationToken); 431 434 } else { 432 Serialize(stream, serializer); 433 } 434 } 435 catch (PersistenceException) { 435 Serialize(stream, serializer, cancellationToken); 436 } 437 } catch (PersistenceException) { 436 438 throw; 437 } 438 catch (Exception e) { 439 } catch (Exception e) { 439 440 throw new PersistenceException("Unexpected exception during Serialization.", e); 440 441 } … … 451 452 /// <param name="compressionType">Type of compression, default is GZip.</param> 452 453 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, 453 CompressionType compressionType = CompressionType.GZip ) {454 CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 454 455 try { 455 456 Serializer serializer = new Serializer(obj, config); 456 457 if (compressionType == CompressionType.Zip) { 457 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer );458 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer, cancellationToken); 458 459 } else { 459 Serialize(stream, serializer );460 Serialize(stream, serializer, cancellationToken); 460 461 } 461 462 types = serializer.SerializedTypes; 462 } 463 catch (PersistenceException) { 463 } catch (PersistenceException) { 464 464 throw; 465 } 466 catch (Exception e) { 465 } catch (Exception e) { 467 466 throw new PersistenceException("Unexpected exception during Serialization.", e); 468 467 } 469 468 } 470 469 471 private static void Serialize(Stream stream, Serializer serializer) { 470 private static void Serialize(Stream stream, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) { 471 cancellationToken.ThrowIfCancellationRequested(); 472 472 using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) { 473 473 serializer.InterleaveTypeInformation = true; 474 474 XmlGenerator generator = new XmlGenerator(); 475 475 foreach (ISerializationToken token in serializer) { 476 cancellationToken.ThrowIfCancellationRequested(); 476 477 string line = generator.Format(token); 477 478 writer.Write(line);
Note: See TracChangeset
for help on using the changeset viewer.