Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/12/11 10:12:50 (13 years ago)
Author:
epitzer
Message:

#1530 Split type and serializer tokens and include special handling for CachedTypeSerializer (this should also fix #1527)

Location:
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3
Files:
4 added
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs

    r5445 r6737  
    3232  /// instantiates objects and fills in values.
    3333  /// </summary>
    34   public class Deserializer {
     34  public sealed class Deserializer {
    3535
    3636    /// <summary>
     
    4040    private class Midwife {
    4141
     42      public string Name { get; private set; }
    4243      public int? Id { get; private set; }
    4344      public bool MetaMode { get; set; }
     
    5354      }
    5455
    55       public Midwife(Type type, ICompositeSerializer compositeSerializer, int? id) {
     56      public Midwife(Type type, ICompositeSerializer compositeSerializer, int? id, string name) {
    5657        this.type = type;
    5758        this.compositeSerializer = compositeSerializer;
    5859        this.Id = id;
     60        this.Name = name;
    5961        MetaMode = false;
    6062        metaInfo = new List<Tag>();
     
    8284
    8385    private readonly Dictionary<int, object> id2obj;
    84     private readonly Dictionary<Type, object> serializerMapping;
    8586    private readonly Stack<Midwife> parentStack;
    86     private readonly Dictionary<int, Type> typeIds;
    87     private Dictionary<Type, object> serializerInstances;
     87    private ReverseTypeCache typeCache;
    8888
    8989    /// <summary>
     
    9393    /// </summary>
    9494    /// <param name="typeCache">The type cache.</param>
    95     public Deserializer(
    96       IEnumerable<TypeMapping> typeCache) {
     95    public Deserializer(ReverseTypeCache typeCache) {
     96      this.typeCache = typeCache;
    9797      id2obj = new Dictionary<int, object>();
    9898      parentStack = new Stack<Midwife>();
    99       typeIds = new Dictionary<int, Type>();
    100       serializerMapping = new Dictionary<Type, object>();
    101       serializerInstances = new Dictionary<Type, object>();
    102       foreach (var typeMapping in typeCache) {
    103         AddTypeInfo(typeMapping);
    104       }
    105     }
    106 
    107     /// <summary>
    108     /// Adds additionaly type information.
    109     /// </summary>
    110     /// <param name="typeMapping">The new type mapping.</param>
    111     public void AddTypeInfo(TypeMapping typeMapping) {
    112       if (typeIds.ContainsKey(typeMapping.Id))
    113         return;
    114       try {
    115         Type type = TypeLoader.Load(typeMapping.TypeName);
    116         typeIds.Add(typeMapping.Id, type);
    117         Type serializerType = TypeLoader.Load(typeMapping.Serializer);
    118         object serializer;
    119         if (serializerInstances.ContainsKey(serializerType)) {
    120           serializer = serializerInstances[serializerType];
    121         } else {
    122           serializer = Activator.CreateInstance(serializerType, true);
    123           serializerInstances.Add(serializerType, serializer);
    124         }
    125         serializerMapping.Add(type, serializer);
    126       } catch (PersistenceException) {
    127         throw;
    128       } catch (Exception e) {
    129         throw new PersistenceException(string.Format(
    130           "Could not add type info for {0} ({1})",
    131           typeMapping.TypeName, typeMapping.Serializer), e);
    132       }
    13399    }
    134100
     
    157123        } else if (t == typeof(TypeToken)) {
    158124          Type((TypeToken)token);
     125        } else if (t == typeof(SerializerToken)) {
     126          Serializer((SerializerToken)token);
    159127        } else {
    160128          throw new PersistenceException("invalid token type");
     
    173141
    174142    private void Type(TypeToken token) {
    175       AddTypeInfo(new TypeMapping(token.Id, token.TypeName, token.Serializer));
     143      typeCache.AddType(token.Id, TypeLoader.Load(token.TypeName));
     144    }
     145
     146    private void Serializer(SerializerToken token) {
     147      typeCache.AddSerializer(token.Id, TypeLoader.Load(token.Serializer));
    176148    }
    177149
    178150    private void CompositeStartHandler(BeginToken token) {
    179151      InstantiateParent();
    180       Type type = typeIds[(int)token.TypeId];
     152      Type type = typeCache.GetType(token.TypeId);
    181153      try {
    182         parentStack.Push(new Midwife(type, (ICompositeSerializer)serializerMapping[type], token.Id));
     154        parentStack.Push(new Midwife(type, typeCache.GetCompositeSerializer(token.TypeId), token.Id, token.Name));
    183155      } catch (Exception e) {
    184156        if (e is InvalidCastException || e is KeyNotFoundException) {
     
    195167
    196168    private void CompositeEndHandler(EndToken token) {
    197       Type type = typeIds[(int)token.TypeId];
    198169      Midwife midwife = parentStack.Pop();
    199170      if (midwife.Obj == null)
    200171        CreateInstance(midwife);
    201172      midwife.Populate();
    202       SetValue(token.Name, midwife.Obj);
     173      SetValue(midwife.Name, midwife.Obj);
    203174    }
    204175
    205176    private void PrimitiveHandler(PrimitiveToken token) {
    206       Type type = typeIds[(int)token.TypeId];
    207177      try {
    208         object value = ((IPrimitiveSerializer)serializerMapping[type]).Parse(token.SerialData);
     178        object value = typeCache.GetPrimitiveSerializer(token.TypeId).Parse(token.SerialData);
    209179        if (token.Id != null)
    210180          id2obj[(int)token.Id] = value;
     
    214184          throw new PersistenceException(String.Format(
    215185            "Invalid primitive serializer configuration for type \"{0}\".",
    216             type.AssemblyQualifiedName), e);
     186            typeCache.GetType(token.TypeId).AssemblyQualifiedName), e);
    217187        } else {
    218188          throw new PersistenceException(String.Format(
    219189            "Unexpected exception while trying to parse object of type \"{0}\".",
    220             type.AssemblyQualifiedName), e);
     190            typeCache.GetType(token.TypeId).AssemblyQualifiedName), e);
    221191        }
    222192      }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs

    r5445 r6737  
    5757      if (type == typeof(TypeToken))
    5858        return Format((TypeToken)token);
     59      if (type == typeof(SerializerToken))
     60        return Format((SerializerToken)token);
    5961      throw new ApplicationException("Invalid token of type " + type.FullName);
    6062    }
     
    116118    protected abstract T Format(TypeToken typeToken);
    117119
     120    protected abstract T Format(SerializerToken serializerToken);
    118121  }
    119122}
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r6214 r6737  
    2323using System.Collections;
    2424using System.Collections.Generic;
    25 using System.Reflection;
     25using System.Linq;
    2626using System.Text;
    2727using HeuristicLab.Persistence.Auxiliary;
     
    4444  /// and traverses the object graph while the enumerator is iterated
    4545  /// </summary> 
    46   public class Serializer : IEnumerable<ISerializationToken> {
    47 
     46  public sealed class Serializer : IEnumerable<ISerializationToken> {
     47
     48    #region Nested Types
    4849    private class ReferenceEqualityComparer : IEqualityComparer<object> {
    4950
     
    5960
    6061    }
    61 
     62    #endregion
     63
     64    #region Fields
    6265    private readonly object obj;
    6366    private readonly string rootName;
    6467    private readonly Dictionary<object, int> obj2id;
    65     private readonly Dictionary<Type, int> typeCache;
    6668    private readonly Configuration configuration;
    6769    private readonly bool isTestRun;
    6870    private readonly List<Exception> exceptions;
     71    private readonly Stack<string> objectGraphTrace;
     72    private readonly CachedTypeSerializer typeSerializer;
     73    private readonly TypeCache typeCache;
     74    #endregion
    6975
    7076    /// <summary>
     
    8086    public bool InterleaveTypeInformation { get; set; }
    8187
    82     /// <summary>
    83     /// Contains a mapping of type id to type and serializer.
    84     /// </summary>
    85     /// <value>The type cache.</value>
    86     public List<TypeMapping> TypeCache {
    87       get {
    88         BuildTypeCache();
    89         return externalTypeCache;
    90       }
     88    public TypeCache TypeCache {
     89      get { return typeCache; }
    9190    }
    9291
     
    9594    /// necessary to deserialize the object graph again.   
    9695    /// </summary>
    97     public List<string> RequiredFiles {
    98       get {
    99         BuildTypeCache();
    100         return requiredFiles;
    101       }
    102     }
    103 
    104     private List<TypeMapping> externalTypeCache;
    105     private List<string> requiredFiles;
    106     private void BuildTypeCache() {
    107       externalTypeCache = new List<TypeMapping>();
    108       Dictionary<Assembly, bool> assemblies = new Dictionary<Assembly, bool>();
    109       foreach (var pair in typeCache) {
    110         string serializer = null;
    111         IPrimitiveSerializer f = configuration.GetPrimitiveSerializer(pair.Key);
    112         if (f != null) {
    113           serializer = f.GetType().AssemblyQualifiedName;
    114           assemblies[f.GetType().Assembly] = true;
    115         } else {
    116           ICompositeSerializer d = configuration.GetCompositeSerializer(pair.Key);
    117           serializer = d.GetType().AssemblyQualifiedName;
    118           assemblies[d.GetType().Assembly] = true;
    119         }
    120         externalTypeCache.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer));
    121         assemblies[pair.Key.Assembly] = true;
    122       }
    123       Dictionary<string, bool> files = new Dictionary<string, bool>();
    124       foreach (Assembly a in assemblies.Keys) {
    125         files[a.CodeBase] = true;
    126       }
    127       requiredFiles = new List<string>(files.Keys);
    128     }
    129 
    130     public IEnumerable<Type> SerializedTypes {
    131       get {
    132         return typeCache.Keys;
    133       }
    134     }
     96    public IEnumerable<string> GetRequiredFiles() {
     97      HashSet<string> files = new HashSet<string>();
     98      foreach (var t in typeCache.Types) {
     99        files.Add(t.Assembly.CodeBase);
     100        Type serializer = typeCache.GetSerializer(typeCache.GetOrCreateId(t));
     101        if (serializer != null)
     102          files.Add(serializer.Assembly.CodeBase);
     103      }
     104      return files;
     105    }
     106
     107    public IEnumerable<Type> SerializedTypes { get { return typeCache.Types; } }
    135108
    136109    /// <summary>
     
    139112    /// <param name="obj">The object to serialize.</param>
    140113    /// <param name="configuration">The configuration.</param>
    141     public Serializer(object obj, Configuration configuration) :
    142       this(obj, configuration, "ROOT") { }
     114    public Serializer(object obj, Configuration configuration)
     115      : this(obj, configuration, "ROOT") { }
    143116
    144117    /// <summary>
     
    163136      this.obj = obj;
    164137      this.rootName = rootName;
    165       this.configuration = configuration;
    166138      obj2id = new Dictionary<object, int>(new ReferenceEqualityComparer()) { { new object(), 0 } };
    167       typeCache = new Dictionary<Type, int>();
     139      typeCache = new TypeCache();
    168140      this.isTestRun = isTestRun;
    169141      this.exceptions = new List<Exception>();
     142      this.objectGraphTrace = new Stack<string>();
     143      this.typeSerializer = new CachedTypeSerializer(typeCache);
     144      this.configuration = new Configuration(
     145        configuration.Format,
     146        configuration.PrimitiveSerializers,
     147        new[] { typeSerializer }.Concat(configuration.CompositeSerializers));
    170148    }
    171149
     
    206184    }
    207185
    208     private Stack<string> objectGraphTrace = new Stack<string>();
    209186
    210187    private IEnumerator<ISerializationToken> Serialize(string name, object obj) {
    211 
    212188      object value = obj;
    213189      if (value == null)
     
    216192      if (obj2id.ContainsKey(value))
    217193        return ReferenceEnumerator(name, obj2id[value]);
    218       bool emitTypeInfo = false;
    219       if (!typeCache.ContainsKey(type)) {
    220         typeCache.Add(type, typeCache.Count);
    221         emitTypeInfo = InterleaveTypeInformation;
    222       }
    223       int typeId = typeCache[type];
     194      int typeId = typeCache.GetOrCreateId(type);
    224195      int? id = null;
    225196      if (!type.IsValueType) {
     
    230201        objectGraphTrace.Push(name);
    231202        IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type);
    232         if (primitiveSerializer != null)
     203        if (primitiveSerializer != null) {
     204          typeCache.SetSerializer(typeId, primitiveSerializer.GetType());
    233205          return PrimitiveEnumerator(
    234206            name,
    235207            typeId,
    236208            primitiveSerializer.Format(value),
    237             id,
    238             emitTypeInfo);
     209            id);
     210        }
    239211        ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type);
     212        typeCache.SetSerializer(typeId, compositeSerializer.GetType());
    240213        if (compositeSerializer != null)
    241214          return CompositeEnumerator(
     
    244217            id,
    245218            typeId,
    246             compositeSerializer.CreateMetaInfo(value),
    247             emitTypeInfo);
    248         throw CreatePersistenceException(type, "Could not determine how to serialize a value.");
    249       }
    250       catch (Exception x) {
     219            compositeSerializer.CreateMetaInfo(value));
     220        throw CreatePersistenceException(type, "Could not determine how to serialize a value.", null);
     221      } catch (Exception x) {
    251222        if (isTestRun) {
    252223          exceptions.Add(x);
     
    255226          throw;
    256227        } else {
    257           throw CreatePersistenceException(type, "Uncaught exception during serialization: " + x.Message);
     228          throw CreatePersistenceException(type, "Uncaught exception during serialization: ", x);
    258229        }
    259       }
    260       finally {
     230      } finally {
    261231        objectGraphTrace.Pop();
    262232      }
    263233    }
    264234
    265     private PersistenceException CreatePersistenceException(Type type, string message) {
     235    private PersistenceException CreatePersistenceException(Type type, string message, Exception x) {
    266236      StringBuilder sb = new StringBuilder();
    267237      sb.Append(message)
     
    295265    }
    296266
    297     private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name,
    298         int typeId, ISerialData serializedValue, int? id, bool emitTypeInfo) {
    299       if (emitTypeInfo) {
    300         var mapping = TypeCache[typeId];
    301         yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer);
     267    private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name, int typeId, ISerialData serializedValue, int? id) {
     268      if (InterleaveTypeInformation) {
     269        if (typeCache.HasUnpublishedType)
     270          yield return typeCache.PublishUnpublishedType();
     271        if (typeCache.HasUnpublishedSerializer)
     272          yield return typeCache.PublishUnpublishedSerializer();
    302273      }
    303274      yield return new PrimitiveToken(name, typeId, id, serializedValue);
    304275    }
    305276
    306     private IEnumerator<ISerializationToken> CompositeEnumerator(
    307         string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo,
    308         bool emitTypeInfo) {
    309       if (emitTypeInfo) {
    310         var mapping = TypeCache[typeId];
    311         yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer);
     277    private IEnumerator<ISerializationToken> CompositeEnumerator(string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo) {
     278      if (InterleaveTypeInformation) {
     279        if (typeCache.HasUnpublishedType)
     280          yield return typeCache.PublishUnpublishedType();
     281        if (typeCache.HasUnpublishedSerializer)
     282          yield return typeCache.PublishUnpublishedSerializer();
    312283      }
    313284      yield return new BeginToken(name, typeId, id);
     
    335306        }
    336307      }
    337       yield return new EndToken(name, typeId, id);
     308      yield return new EndToken();
    338309    }
    339310  }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/Tokens/TypeToken.cs

    r5445 r6737  
    4040
    4141    /// <summary>
    42     /// The full type name of the serialized used to
    43     /// serialize the type.
    44     /// </summary>
    45     public readonly string Serializer;
    46 
    47     /// <summary>
    4842    /// Initializes a new instance of the <see cref="TypeToken"/> class.
    4943    /// </summary>
     
    5246    /// <param name="serializer">The full name of the serializer
    5347    /// used to serialize the type.</param>
    54     public TypeToken(int id, string typeName, string serializer) {
     48    public TypeToken(int id, string typeName) {
    5549      Id = id;
    5650      TypeName = typeName;
    57       Serializer = serializer;
    5851    }
    5952  }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/DebugString/DebugStringGenerator.cs

    r5445 r6737  
    169169    }
    170170
     171    protected override string Format(SerializerToken serializerToken) {
     172      return string.Empty;
     173    }
     174
    171175    /// <summary>
    172176    /// Serializes the specified object.
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs

    r5489 r6737  
    5858    /// <returns>The token in serialized form.</returns>
    5959    protected override string Format(EndToken endToken) {
    60       return CreateNodeEnd(typeCache[endToken.TypeId]);
     60      return CreateNodeEnd("END");
    6161    }
    6262
     
    154154        File.Copy(tempfile, filename, true);
    155155        File.Delete(tempfile);
    156       }
    157       catch (Exception) {
     156      } catch (Exception) {
    158157        Logger.Warn("Exception caught, no data has been written.");
    159158        throw;
     
    190189          writer.Flush();
    191190        }
    192       }
    193       catch (PersistenceException) {
     191      } catch (PersistenceException) {
    194192        throw;
    195       }
    196       catch (Exception e) {
     193      } catch (Exception e) {
    197194        throw new PersistenceException("Unexpected exception during Serialization.", e);
    198195      }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r6211 r6737  
    157157      AddTypeInfo(beginToken.TypeId, dict);
    158158      return CreateNodeStart(XmlStringConstants.COMPOSITE, dict);
    159 
    160159    }
    161160
     
    164163        if (typeId == lastTypeToken.Id) {
    165164          dict.Add("typeName", lastTypeToken.TypeName);
    166           dict.Add("serializer", lastTypeToken.Serializer);
    167165          lastTypeToken = null;
    168166        } else {
    169167          FlushTypeToken();
     168        }
     169      }
     170      if (lastSerializerToken != null) {
     171        if (typeId == lastSerializerToken.Id) {
     172          dict.Add("serializer", lastSerializerToken.Serializer);
     173          lastSerializerToken = null;
     174        } else {
     175          FlushSerializerToken();
    170176        }
    171177      }
     
    238244
    239245    protected TypeToken lastTypeToken;
     246    protected SerializerToken lastSerializerToken;
    240247    /// <summary>
    241248    /// Formats the specified token.
     
    245252    protected override string Format(TypeToken token) {
    246253      lastTypeToken = token;
     254      return "";
     255    }
     256
     257    protected override string Format(SerializerToken token) {
     258      lastSerializerToken = token;
    247259      return "";
    248260    }
     
    255267          new Dictionary<string, string> {
    256268          {"id", lastTypeToken.Id.ToString()},
    257           {"typeName", lastTypeToken.TypeName },
    258           {"serializer", lastTypeToken.Serializer }});
     269          {"typeName", lastTypeToken.TypeName }});
    259270      } finally {
    260271        lastTypeToken = null;
     
    262273    }
    263274
     275    protected string FlushSerializerToken() {
     276      if (lastSerializerToken == null)
     277        return "";
     278      try {
     279        return CreateNode(XmlStringConstants.SERIALIZER,
     280          new Dictionary<string, string> {
     281          {"id", lastSerializerToken.Id.ToString()},
     282          {"serializer", lastSerializerToken.Serializer }});
     283      } finally {
     284        lastTypeToken = null;
     285      }
     286    }
     287
    264288    /// <summary>
    265289    /// Formats the specified type cache.
     
    267291    /// <param name="typeCache">The type cache.</param>
    268292    /// <returns>An enumerable of formatted type cache tags.</returns>
    269     public IEnumerable<string> Format(List<TypeMapping> typeCache) {
     293    public IEnumerable<string> Format(TypeCache typeCache) {
    270294      yield return CreateNodeStart(XmlStringConstants.TYPECACHE);
    271       foreach (var mapping in typeCache)
    272         yield return CreateNode(
    273           XmlStringConstants.TYPE,
    274           mapping.GetDict());
     295      foreach (var type in typeCache.Types) {
     296        int id = typeCache.GetOrCreateId(type);
     297        var dict = new Dictionary<string, string> {
     298          {"id", id.ToString()},
     299          {"typeName", type.AssemblyQualifiedName},
     300        };
     301        Type serializer = typeCache.GetSerializer(id);
     302        if (serializer != null)
     303          dict.Add("serializer", serializer.AssemblyQualifiedName);
     304        yield return CreateNode(XmlStringConstants.TYPE, dict);
     305      }
    275306      yield return CreateNodeEnd(XmlStringConstants.TYPECACHE);
    276307    }
     
    342373            writer.Flush();
    343374            if (includeAssemblies) {
    344               foreach (string name in serializer.RequiredFiles) {
     375              foreach (string name in serializer.GetRequiredFiles()) {
    345376                Uri uri = new Uri(name);
    346377                if (!uri.IsFile) {
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r6211 r6737  
    2626using System.IO.Compression;
    2727using System.Xml;
     28using HeuristicLab.Persistence.Auxiliary;
    2829using HeuristicLab.Persistence.Core;
    2930using HeuristicLab.Persistence.Core.Tokens;
     
    5859                     {XmlStringConstants.METAINFO, ParseMetaInfo},
    5960                     {XmlStringConstants.TYPE, ParseTypeInfo},
     61                     {XmlStringConstants.SERIALIZER, ParseSerializer},
    6062                   };
    6163    }
     
    106108      string serializer = reader.GetAttribute("serializer");
    107109      if (typeName != null)
    108         yield return new TypeToken(typeId, typeName, serializer);
     110        yield return new TypeToken(typeId, typeName);
     111      if (serializer != null)
     112        yield return new SerializerToken(typeId, serializer);
    109113      XmlReader inner = reader.ReadSubtree();
    110114      inner.Read();
     
    124128      string serializer = reader.GetAttribute("serializer");
    125129      if (typeName != null)
    126         yield return new TypeToken(typeId, typeName, serializer);
     130        yield return new TypeToken(typeId, typeName);
     131      if (serializer != null)
     132        yield return new SerializerToken(typeId, serializer);
    127133      yield return new BeginToken(name, typeId, id);
    128134      IEnumerator<ISerializationToken> iterator = GetEnumerator();
    129135      while (iterator.MoveNext())
    130136        yield return iterator.Current;
    131       yield return new EndToken(name, typeId, id);
     137      yield return new EndToken();
    132138    }
    133139
     
    151157
    152158    private IEnumerator<ISerializationToken> ParseTypeInfo() {
    153       yield return new TypeToken(
    154         int.Parse(reader.GetAttribute("id")),
    155         reader.GetAttribute("typeName"),
    156         reader.GetAttribute("serializer"));
     159      int id = int.Parse(reader.GetAttribute("id"));
     160      string typeName = reader.GetAttribute("typeName");
     161      yield return new TypeToken(id, typeName);
     162      string serializer = reader.GetAttribute("serializer");
     163      if (serializer != null)
     164        yield return new SerializerToken(id, serializer);
     165    }
     166
     167    private IEnumerator<ISerializationToken> ParseSerializer() {
     168      int id = int.Parse(reader.GetAttribute("id"));
     169      string serializer = reader.GetAttribute("serializer");
     170      yield return new SerializerToken(id, serializer);
    157171    }
    158172
     
    162176    /// <param name="reader">The reader.</param>
    163177    /// <returns>A list of type mapping entries.</returns>
    164     public static List<TypeMapping> ParseTypeCache(TextReader reader) {
    165       try {
    166         var typeCache = new List<TypeMapping>();
     178    public static ReverseTypeCache ParseTypeCache(TextReader reader) {
     179      ReverseTypeCache typeCache = new ReverseTypeCache();
     180      try {
    167181        XmlReader xmlReader = XmlReader.Create(reader);
    168182        while (xmlReader.Read()) {
    169183          if (xmlReader.Name == XmlStringConstants.TYPE) {
    170             typeCache.Add(new TypeMapping(
     184            int id = int.Parse(xmlReader.GetAttribute("id"));
     185            typeCache.AddType(id, TypeLoader.Load(xmlReader.GetAttribute("typeName")));
     186            string serializer = xmlReader.GetAttribute("serializer");
     187            if (serializer != null)
     188              typeCache.AddSerializer(id, TypeLoader.Load(serializer));
     189          } else if (xmlReader.Name == XmlStringConstants.SERIALIZER) {
     190            typeCache.AddSerializer(
    171191              int.Parse(xmlReader.GetAttribute("id")),
    172               xmlReader.GetAttribute("typeName"),
    173               xmlReader.GetAttribute("serializer")));
     192              TypeLoader.Load(xmlReader.GetAttribute("serializer")));
    174193          }
    175194        }
     
    221240        using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
    222241          XmlParser parser = new XmlParser(reader);
    223           Deserializer deserializer = new Deserializer(new TypeMapping[] { });
     242          Deserializer deserializer = new Deserializer(new ReverseTypeCache());
    224243          return deserializer.Deserialize(new AsyncBuffer<ISerializationToken>(parser));
    225244        }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlStringConstants.cs

    r5445 r6737  
    3030    public const string TYPECACHE = "TYPECACHE";
    3131    public const string TYPE = "TYPE";
     32    public const string SERIALIZER = "SERIALIZER";
    3233    public const string METAINFO = "METAINFO";
    3334  }
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r6702 r6737  
    126126    <Compile Include="Auxiliary\ReflectionTools.cs" />
    127127    <None Include="HeuristicLabPersistencePlugin.cs.frame" />
     128    <Compile Include="Core\CachedTypeSerializer.cs" />
    128129    <Compile Include="Core\Configuration.cs" />
     130    <Compile Include="Core\ReverseTypeCache.cs" />
    129131    <Compile Include="Core\PersistenceException.cs" />
    130132    <Compile Include="Core\PrimitiveSerializerBase.cs" />
    131133    <Compile Include="Core\FormatBase.cs" />
    132134    <Compile Include="Auxiliary\TypeExtensions.cs" />
     135    <Compile Include="Core\Tokens\SerializerToken.cs" />
    133136    <Compile Include="Core\Tokens\TypeToken.cs" />
    134     <Compile Include="Core\TypeMapping.cs" />
     137    <Compile Include="Core\TypeCache.cs" />
    135138    <Compile Include="Auxiliary\TypeName.cs" />
    136139    <Compile Include="Core\GeneratorBase.cs" />
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r6228 r6737  
    11291129      Assert.AreEqual("ROOT", ((BeginToken)it.Current).Name); it.MoveNext();
    11301130      Assert.AreEqual("ReadOnly", ((PrimitiveToken)it.Current).Name); it.MoveNext();
    1131       Assert.AreEqual("ROOT", ((EndToken)it.Current).Name); it.MoveNext();
    1132       var deserializer = new Deserializer(new[] {
    1133         new TypeMapping(0, typeof(OneWayTest).AssemblyQualifiedName, typeof(StorableSerializer).AssemblyQualifiedName),
    1134         new TypeMapping(1, typeof(string).AssemblyQualifiedName, typeof(String2XmlSerializer).AssemblyQualifiedName) });
     1131      ReverseTypeCache typeCache = new ReverseTypeCache();
     1132      typeCache.AddType(0, typeof(OneWayTest));
     1133      typeCache.AddSerializer(0, typeof(StorableSerializer));
     1134      typeCache.AddType(1, typeof(string));
     1135      typeCache.AddSerializer(1, typeof(String2XmlSerializer));
     1136      var deserializer = new Deserializer(typeCache);
    11351137      var newTest = (OneWayTest)deserializer.Deserialize(new ISerializationToken[] {
    11361138        new BeginToken("ROOT", 0, 0),
    11371139        new PrimitiveToken("WriteOnly", 1, 1, new XmlString("<![CDATA[serial data]]>")),
    1138         new EndToken("ROOT", 0, 0)
     1140        new EndToken()
    11391141      });
    11401142      Assert.AreEqual("serial data", newTest.value);
     
    11681170    }
    11691171
     1172    [TestMethod]
     1173    public void TestCachedTypeSerializer() {
     1174      List<Type> types = new List<Type> { typeof(object), typeof(UseCases), typeof(XmlGenerator) };
     1175      var s = new Serializer(types, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()));
     1176      var tokens = s.ToList();
     1177      foreach (var type in types)
     1178        Assert.IsTrue(s.SerializedTypes.Contains(type));
     1179      var d = new Deserializer(s.TypeCache.Reverse());
     1180      var newTypes = (List<Type>)d.Deserialize(tokens);
     1181      for (int i = 0; i < types.Count; i++)
     1182        Assert.AreEqual(types[i], newTypes[i]);
     1183    }
     1184
    11701185
    11711186
Note: See TracChangeset for help on using the changeset viewer.