Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/10 15:50:50 (15 years ago)
Author:
epitzer
Message:

add support for type information interleaving and subsequently true streaming (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Core
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs

    r3004 r3005  
    7777      parentStack = new Stack<Midwife>();
    7878      typeIds = new Dictionary<int, Type>();
    79       serializerMapping = CreateSerializers(typeCache);
    80     }
    81 
    82     private Dictionary<Type, object> CreateSerializers(IEnumerable<TypeMapping> typeCache) {
    83       Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>();
     79      serializerMapping = new Dictionary<Type, object>();
     80      foreach (var typeMapping in typeCache) {
     81        AddTypeInfo(typeMapping);
     82      }
     83    }
     84
     85    private Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>();   
     86
     87    public void AddTypeInfo(TypeMapping typeMapping) {
     88      if (typeIds.ContainsKey(typeMapping.Id))
     89        return;
    8490      try {
    85         var map = new Dictionary<Type, object>();
    86         foreach (var typeMapping in typeCache) {
    87           Type type = TypeLoader.Load(typeMapping.TypeName);
    88           typeIds.Add(typeMapping.Id, type);
    89           Type serializerType = TypeLoader.Load(typeMapping.Serializer);
    90           object serializer;
    91           if (serializerInstances.ContainsKey(serializerType))
    92             serializer = serializerInstances[serializerType];
    93           else
    94             serializer = Activator.CreateInstance(serializerType, true);
    95           map.Add(type, serializer);
    96         }
    97         return map;
     91        Type type = TypeLoader.Load(typeMapping.TypeName);
     92        typeIds.Add(typeMapping.Id, type);
     93        Type serializerType = TypeLoader.Load(typeMapping.Serializer);
     94        object serializer;
     95        if (serializerInstances.ContainsKey(serializerType))
     96          serializer = serializerInstances[serializerType];
     97        else
     98          serializer = Activator.CreateInstance(serializerType, true);
     99        serializerMapping.Add(type, serializer);
    98100      } catch (PersistenceException) {
    99101        throw;
    100102      } catch (Exception e) {
    101         throw new PersistenceException(
    102           "The serialization type cache could not be loaded.\r\n" +
    103           "This usualy happens when you are missing an Assembly or Plugin.", e);
     103        throw new PersistenceException(string.Format(
     104          "Could not add type info for {0} ({1})",
     105          typeMapping.TypeName, typeMapping.Serializer), e);
    104106      }
    105107    }
     
    125127        } else if (t == typeof(MetaInfoEndToken)) {
    126128          MetaInfoEnd((MetaInfoEndToken)token);
     129        } else if (t == typeof(TypeToken)) {
     130          Type((TypeToken)token);
    127131        } else {
    128132          throw new PersistenceException("invalid token type");
     
    138142      if (!m.MetaMode && m.Obj == null)
    139143        CreateInstance(m);
     144    }
     145
     146    private void Type(TypeToken token) {
     147      AddTypeInfo(new TypeMapping(token.Id, token.TypeName, token.Serializer));
    140148    }
    141149
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs

    r3004 r3005  
    3434      if (type == typeof(MetaInfoEndToken))
    3535        return Format((MetaInfoEndToken)token);
     36      if (type == typeof(TypeToken))
     37        return Format((TypeToken)token);
    3638      throw new ApplicationException("Invalid token of type " + type.FullName);
    3739    }
     
    4446    protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
    4547    protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
     48    protected abstract T Format(TypeToken typeToken);
    4649
    4750  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r3004 r3005  
    4949    private readonly bool isTestRun;
    5050    private readonly List<Exception> exceptions;
     51
     52    public bool InterleaveTypeInformation { get; set; }
    5153
    5254    /// <summary>
     
    106108    /// don't stop at the first exception</param>
    107109    public Serializer(object obj, Configuration configuration, string rootName, bool isTestRun) {
     110      this.InterleaveTypeInformation = false;
    108111      this.obj = obj;
    109112      this.rootName = rootName;
     
    144147      if (obj2id.ContainsKey(value))
    145148        return ReferenceEnumerator(accessor.Name, obj2id[value]);
    146       if (!typeCache.ContainsKey(type))
     149      bool emitTypeInfo = false;
     150      if (!typeCache.ContainsKey(type)) {
    147151        typeCache.Add(type, typeCache.Count);
     152        emitTypeInfo = InterleaveTypeInformation;
     153      }
    148154      int typeId = typeCache[type];
    149155      int? id = null;
     
    155161        IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type);
    156162        if (primitiveSerializer != null)
    157           return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id);
     163          return PrimitiveEnumerator(
     164            accessor.Name,
     165            typeId,
     166            primitiveSerializer.Format(value),
     167            id,
     168            emitTypeInfo);
    158169        ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type);
    159170        if (compositeSerializer != null)
    160           return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value));
     171          return CompositeEnumerator(
     172            accessor.Name,
     173            compositeSerializer.Decompose(value),
     174            id,
     175            typeId,
     176            compositeSerializer.CreateMetaInfo(value),
     177            emitTypeInfo);
    161178        throw CreatePersistenceException(type);
    162179      } catch (Exception x) {
     
    200217
    201218    private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name,
    202         int typeId, ISerialData serializedValue, int? id) {
     219        int typeId, ISerialData serializedValue, int? id, bool emitTypeInfo) {
     220      if (emitTypeInfo) {
     221        var mapping = TypeCache[typeId];
     222        yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer);
     223      }
    203224      yield return new PrimitiveToken(name, typeId, id, serializedValue);
    204225    }
    205226
    206227    private IEnumerator<ISerializationToken> CompositeEnumerator(
    207         string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo) {
     228        string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo,
     229        bool emitTypeInfo) {
     230      if (emitTypeInfo) {
     231        var mapping = TypeCache[typeId];
     232        yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer);
     233      }
    208234      yield return new BeginToken(name, typeId, id);
    209235      bool first = true;
Note: See TracChangeset for help on using the changeset viewer.