Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive/3.3/Util/PersistenceUtil.cs @ 17087

Last change on this file since 17087 was 17087, checked in by abeham, 5 years ago

#2520:

  • fixed loading of samples, removed TODO (due to upgrade of Attic 1.2)
  • corrected check when old persistence should be used to retry download from hive
File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.IO.Compression;
26using Google.Protobuf;
27using HEAL.Attic;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Clients.Hive {
31  public static class PersistenceUtil {
32    public static byte[] Serialize(object obj, out IEnumerable<Type> types) {
33      var ser = new ProtoBufSerializer();
34      var bytes = ser.Serialize(obj, out SerializationInfo info);
35      types = info.SerializedTypes;
36      return bytes;
37    }
38
39    public static byte[] Serialize(object obj) {
40      var ser = new ProtoBufSerializer();
41      return ser.Serialize(obj);
42    }
43
44    public static T Deserialize<T>(byte[] sjob) {
45      var ser = new ProtoBufSerializer();
46      try {
47        return (T)ser.Deserialize(sjob);
48      } catch (PersistenceException e) {
49        // Exceptions may arise because data in Hive was uploaded with former XML-based
50        // persistence or with Attic prior to 1.2.
51        if (e.InnerException is InvalidDataException) {
52          // We assume the data was serialized with HEAL.Attic < 1.2 which did not use
53          // DeflateStream, but as of 1.2 uses DeflateStream by default
54          var compressedData = Compress(sjob);
55          try {
56            // retry with the job's data compressed
57            return (T)ser.Deserialize(compressedData);
58          } catch (PersistenceException e2) {
59            if (e2.InnerException is InvalidProtocolBufferException
60              || e2.InnerException is InvalidDataException) {
61              // retry deserialize original bytes with old persistence
62              return DeserializeWithXmlParser<T>(sjob);
63            } else throw;
64          }
65        }
66        if (e.InnerException is InvalidProtocolBufferException) {
67          // We assume the data was not serialized with HEAL.Attic, but with the former
68          // XML-based persistence
69          return DeserializeWithXmlParser<T>(sjob);
70        } else throw;
71      }
72    }
73
74    private static byte[] Compress(byte[] sjob) {
75      using (var memStream = new MemoryStream(sjob.Length)) {
76        using (var deflateStream = new DeflateStream(memStream, CompressionMode.Compress)) {
77          deflateStream.Write(sjob, 0, sjob.Length);
78        }
79        return memStream.ToArray();
80      }
81    }
82
83    private static T DeserializeWithXmlParser<T>(byte[] sjob) {
84      using (MemoryStream memStream = new MemoryStream(sjob)) {
85        return XmlParser.Deserialize<T>(memStream);
86      }
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.