Changeset 6737 for branches/PersistenceSpeedUp/HeuristicLab.Persistence
- Timestamp:
- 09/12/11 10:12:50 (13 years ago)
- 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 32 32 /// instantiates objects and fills in values. 33 33 /// </summary> 34 public class Deserializer {34 public sealed class Deserializer { 35 35 36 36 /// <summary> … … 40 40 private class Midwife { 41 41 42 public string Name { get; private set; } 42 43 public int? Id { get; private set; } 43 44 public bool MetaMode { get; set; } … … 53 54 } 54 55 55 public Midwife(Type type, ICompositeSerializer compositeSerializer, int? id ) {56 public Midwife(Type type, ICompositeSerializer compositeSerializer, int? id, string name) { 56 57 this.type = type; 57 58 this.compositeSerializer = compositeSerializer; 58 59 this.Id = id; 60 this.Name = name; 59 61 MetaMode = false; 60 62 metaInfo = new List<Tag>(); … … 82 84 83 85 private readonly Dictionary<int, object> id2obj; 84 private readonly Dictionary<Type, object> serializerMapping;85 86 private readonly Stack<Midwife> parentStack; 86 private readonly Dictionary<int, Type> typeIds; 87 private Dictionary<Type, object> serializerInstances; 87 private ReverseTypeCache typeCache; 88 88 89 89 /// <summary> … … 93 93 /// </summary> 94 94 /// <param name="typeCache">The type cache.</param> 95 public Deserializer( 96 IEnumerable<TypeMapping> typeCache) {95 public Deserializer(ReverseTypeCache typeCache) { 96 this.typeCache = typeCache; 97 97 id2obj = new Dictionary<int, object>(); 98 98 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 }133 99 } 134 100 … … 157 123 } else if (t == typeof(TypeToken)) { 158 124 Type((TypeToken)token); 125 } else if (t == typeof(SerializerToken)) { 126 Serializer((SerializerToken)token); 159 127 } else { 160 128 throw new PersistenceException("invalid token type"); … … 173 141 174 142 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)); 176 148 } 177 149 178 150 private void CompositeStartHandler(BeginToken token) { 179 151 InstantiateParent(); 180 Type type = type Ids[(int)token.TypeId];152 Type type = typeCache.GetType(token.TypeId); 181 153 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)); 183 155 } catch (Exception e) { 184 156 if (e is InvalidCastException || e is KeyNotFoundException) { … … 195 167 196 168 private void CompositeEndHandler(EndToken token) { 197 Type type = typeIds[(int)token.TypeId];198 169 Midwife midwife = parentStack.Pop(); 199 170 if (midwife.Obj == null) 200 171 CreateInstance(midwife); 201 172 midwife.Populate(); 202 SetValue( token.Name, midwife.Obj);173 SetValue(midwife.Name, midwife.Obj); 203 174 } 204 175 205 176 private void PrimitiveHandler(PrimitiveToken token) { 206 Type type = typeIds[(int)token.TypeId];207 177 try { 208 object value = ((IPrimitiveSerializer)serializerMapping[type]).Parse(token.SerialData);178 object value = typeCache.GetPrimitiveSerializer(token.TypeId).Parse(token.SerialData); 209 179 if (token.Id != null) 210 180 id2obj[(int)token.Id] = value; … … 214 184 throw new PersistenceException(String.Format( 215 185 "Invalid primitive serializer configuration for type \"{0}\".", 216 type .AssemblyQualifiedName), e);186 typeCache.GetType(token.TypeId).AssemblyQualifiedName), e); 217 187 } else { 218 188 throw new PersistenceException(String.Format( 219 189 "Unexpected exception while trying to parse object of type \"{0}\".", 220 type .AssemblyQualifiedName), e);190 typeCache.GetType(token.TypeId).AssemblyQualifiedName), e); 221 191 } 222 192 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs
r5445 r6737 57 57 if (type == typeof(TypeToken)) 58 58 return Format((TypeToken)token); 59 if (type == typeof(SerializerToken)) 60 return Format((SerializerToken)token); 59 61 throw new ApplicationException("Invalid token of type " + type.FullName); 60 62 } … … 116 118 protected abstract T Format(TypeToken typeToken); 117 119 120 protected abstract T Format(SerializerToken serializerToken); 118 121 } 119 122 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r6214 r6737 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System. Reflection;25 using System.Linq; 26 26 using System.Text; 27 27 using HeuristicLab.Persistence.Auxiliary; … … 44 44 /// and traverses the object graph while the enumerator is iterated 45 45 /// </summary> 46 public class Serializer : IEnumerable<ISerializationToken> { 47 46 public sealed class Serializer : IEnumerable<ISerializationToken> { 47 48 #region Nested Types 48 49 private class ReferenceEqualityComparer : IEqualityComparer<object> { 49 50 … … 59 60 60 61 } 61 62 #endregion 63 64 #region Fields 62 65 private readonly object obj; 63 66 private readonly string rootName; 64 67 private readonly Dictionary<object, int> obj2id; 65 private readonly Dictionary<Type, int> typeCache;66 68 private readonly Configuration configuration; 67 69 private readonly bool isTestRun; 68 70 private readonly List<Exception> exceptions; 71 private readonly Stack<string> objectGraphTrace; 72 private readonly CachedTypeSerializer typeSerializer; 73 private readonly TypeCache typeCache; 74 #endregion 69 75 70 76 /// <summary> … … 80 86 public bool InterleaveTypeInformation { get; set; } 81 87 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; } 91 90 } 92 91 … … 95 94 /// necessary to deserialize the object graph again. 96 95 /// </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; } } 135 108 136 109 /// <summary> … … 139 112 /// <param name="obj">The object to serialize.</param> 140 113 /// <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") { } 143 116 144 117 /// <summary> … … 163 136 this.obj = obj; 164 137 this.rootName = rootName; 165 this.configuration = configuration;166 138 obj2id = new Dictionary<object, int>(new ReferenceEqualityComparer()) { { new object(), 0 } }; 167 typeCache = new Dictionary<Type, int>();139 typeCache = new TypeCache(); 168 140 this.isTestRun = isTestRun; 169 141 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)); 170 148 } 171 149 … … 206 184 } 207 185 208 private Stack<string> objectGraphTrace = new Stack<string>();209 186 210 187 private IEnumerator<ISerializationToken> Serialize(string name, object obj) { 211 212 188 object value = obj; 213 189 if (value == null) … … 216 192 if (obj2id.ContainsKey(value)) 217 193 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); 224 195 int? id = null; 225 196 if (!type.IsValueType) { … … 230 201 objectGraphTrace.Push(name); 231 202 IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type); 232 if (primitiveSerializer != null) 203 if (primitiveSerializer != null) { 204 typeCache.SetSerializer(typeId, primitiveSerializer.GetType()); 233 205 return PrimitiveEnumerator( 234 206 name, 235 207 typeId, 236 208 primitiveSerializer.Format(value), 237 id ,238 emitTypeInfo);209 id); 210 } 239 211 ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type); 212 typeCache.SetSerializer(typeId, compositeSerializer.GetType()); 240 213 if (compositeSerializer != null) 241 214 return CompositeEnumerator( … … 244 217 id, 245 218 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) { 251 222 if (isTestRun) { 252 223 exceptions.Add(x); … … 255 226 throw; 256 227 } else { 257 throw CreatePersistenceException(type, "Uncaught exception during serialization: " + x.Message);228 throw CreatePersistenceException(type, "Uncaught exception during serialization: ", x); 258 229 } 259 } 260 finally { 230 } finally { 261 231 objectGraphTrace.Pop(); 262 232 } 263 233 } 264 234 265 private PersistenceException CreatePersistenceException(Type type, string message ) {235 private PersistenceException CreatePersistenceException(Type type, string message, Exception x) { 266 236 StringBuilder sb = new StringBuilder(); 267 237 sb.Append(message) … … 295 265 } 296 266 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(); 302 273 } 303 274 yield return new PrimitiveToken(name, typeId, id, serializedValue); 304 275 } 305 276 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(); 312 283 } 313 284 yield return new BeginToken(name, typeId, id); … … 335 306 } 336 307 } 337 yield return new EndToken( name, typeId, id);308 yield return new EndToken(); 338 309 } 339 310 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/Tokens/TypeToken.cs
r5445 r6737 40 40 41 41 /// <summary> 42 /// The full type name of the serialized used to43 /// serialize the type.44 /// </summary>45 public readonly string Serializer;46 47 /// <summary>48 42 /// Initializes a new instance of the <see cref="TypeToken"/> class. 49 43 /// </summary> … … 52 46 /// <param name="serializer">The full name of the serializer 53 47 /// used to serialize the type.</param> 54 public TypeToken(int id, string typeName , string serializer) {48 public TypeToken(int id, string typeName) { 55 49 Id = id; 56 50 TypeName = typeName; 57 Serializer = serializer;58 51 } 59 52 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/DebugString/DebugStringGenerator.cs
r5445 r6737 169 169 } 170 170 171 protected override string Format(SerializerToken serializerToken) { 172 return string.Empty; 173 } 174 171 175 /// <summary> 172 176 /// Serializes the specified object. -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs
r5489 r6737 58 58 /// <returns>The token in serialized form.</returns> 59 59 protected override string Format(EndToken endToken) { 60 return CreateNodeEnd( typeCache[endToken.TypeId]);60 return CreateNodeEnd("END"); 61 61 } 62 62 … … 154 154 File.Copy(tempfile, filename, true); 155 155 File.Delete(tempfile); 156 } 157 catch (Exception) { 156 } catch (Exception) { 158 157 Logger.Warn("Exception caught, no data has been written."); 159 158 throw; … … 190 189 writer.Flush(); 191 190 } 192 } 193 catch (PersistenceException) { 191 } catch (PersistenceException) { 194 192 throw; 195 } 196 catch (Exception e) { 193 } catch (Exception e) { 197 194 throw new PersistenceException("Unexpected exception during Serialization.", e); 198 195 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r6211 r6737 157 157 AddTypeInfo(beginToken.TypeId, dict); 158 158 return CreateNodeStart(XmlStringConstants.COMPOSITE, dict); 159 160 159 } 161 160 … … 164 163 if (typeId == lastTypeToken.Id) { 165 164 dict.Add("typeName", lastTypeToken.TypeName); 166 dict.Add("serializer", lastTypeToken.Serializer);167 165 lastTypeToken = null; 168 166 } else { 169 167 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(); 170 176 } 171 177 } … … 238 244 239 245 protected TypeToken lastTypeToken; 246 protected SerializerToken lastSerializerToken; 240 247 /// <summary> 241 248 /// Formats the specified token. … … 245 252 protected override string Format(TypeToken token) { 246 253 lastTypeToken = token; 254 return ""; 255 } 256 257 protected override string Format(SerializerToken token) { 258 lastSerializerToken = token; 247 259 return ""; 248 260 } … … 255 267 new Dictionary<string, string> { 256 268 {"id", lastTypeToken.Id.ToString()}, 257 {"typeName", lastTypeToken.TypeName }, 258 {"serializer", lastTypeToken.Serializer }}); 269 {"typeName", lastTypeToken.TypeName }}); 259 270 } finally { 260 271 lastTypeToken = null; … … 262 273 } 263 274 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 264 288 /// <summary> 265 289 /// Formats the specified type cache. … … 267 291 /// <param name="typeCache">The type cache.</param> 268 292 /// <returns>An enumerable of formatted type cache tags.</returns> 269 public IEnumerable<string> Format( List<TypeMapping>typeCache) {293 public IEnumerable<string> Format(TypeCache typeCache) { 270 294 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 } 275 306 yield return CreateNodeEnd(XmlStringConstants.TYPECACHE); 276 307 } … … 342 373 writer.Flush(); 343 374 if (includeAssemblies) { 344 foreach (string name in serializer. RequiredFiles) {375 foreach (string name in serializer.GetRequiredFiles()) { 345 376 Uri uri = new Uri(name); 346 377 if (!uri.IsFile) { -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r6211 r6737 26 26 using System.IO.Compression; 27 27 using System.Xml; 28 using HeuristicLab.Persistence.Auxiliary; 28 29 using HeuristicLab.Persistence.Core; 29 30 using HeuristicLab.Persistence.Core.Tokens; … … 58 59 {XmlStringConstants.METAINFO, ParseMetaInfo}, 59 60 {XmlStringConstants.TYPE, ParseTypeInfo}, 61 {XmlStringConstants.SERIALIZER, ParseSerializer}, 60 62 }; 61 63 } … … 106 108 string serializer = reader.GetAttribute("serializer"); 107 109 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); 109 113 XmlReader inner = reader.ReadSubtree(); 110 114 inner.Read(); … … 124 128 string serializer = reader.GetAttribute("serializer"); 125 129 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); 127 133 yield return new BeginToken(name, typeId, id); 128 134 IEnumerator<ISerializationToken> iterator = GetEnumerator(); 129 135 while (iterator.MoveNext()) 130 136 yield return iterator.Current; 131 yield return new EndToken( name, typeId, id);137 yield return new EndToken(); 132 138 } 133 139 … … 151 157 152 158 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); 157 171 } 158 172 … … 162 176 /// <param name="reader">The reader.</param> 163 177 /// <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 { 167 181 XmlReader xmlReader = XmlReader.Create(reader); 168 182 while (xmlReader.Read()) { 169 183 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( 171 191 int.Parse(xmlReader.GetAttribute("id")), 172 xmlReader.GetAttribute("typeName"), 173 xmlReader.GetAttribute("serializer"))); 192 TypeLoader.Load(xmlReader.GetAttribute("serializer"))); 174 193 } 175 194 } … … 221 240 using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) { 222 241 XmlParser parser = new XmlParser(reader); 223 Deserializer deserializer = new Deserializer(new TypeMapping[] { });242 Deserializer deserializer = new Deserializer(new ReverseTypeCache()); 224 243 return deserializer.Deserialize(new AsyncBuffer<ISerializationToken>(parser)); 225 244 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/XmlStringConstants.cs
r5445 r6737 30 30 public const string TYPECACHE = "TYPECACHE"; 31 31 public const string TYPE = "TYPE"; 32 public const string SERIALIZER = "SERIALIZER"; 32 33 public const string METAINFO = "METAINFO"; 33 34 } -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj
r6702 r6737 126 126 <Compile Include="Auxiliary\ReflectionTools.cs" /> 127 127 <None Include="HeuristicLabPersistencePlugin.cs.frame" /> 128 <Compile Include="Core\CachedTypeSerializer.cs" /> 128 129 <Compile Include="Core\Configuration.cs" /> 130 <Compile Include="Core\ReverseTypeCache.cs" /> 129 131 <Compile Include="Core\PersistenceException.cs" /> 130 132 <Compile Include="Core\PrimitiveSerializerBase.cs" /> 131 133 <Compile Include="Core\FormatBase.cs" /> 132 134 <Compile Include="Auxiliary\TypeExtensions.cs" /> 135 <Compile Include="Core\Tokens\SerializerToken.cs" /> 133 136 <Compile Include="Core\Tokens\TypeToken.cs" /> 134 <Compile Include="Core\Type Mapping.cs" />137 <Compile Include="Core\TypeCache.cs" /> 135 138 <Compile Include="Auxiliary\TypeName.cs" /> 136 139 <Compile Include="Core\GeneratorBase.cs" /> -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Tests/UseCases.cs
r6228 r6737 1129 1129 Assert.AreEqual("ROOT", ((BeginToken)it.Current).Name); it.MoveNext(); 1130 1130 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); 1135 1137 var newTest = (OneWayTest)deserializer.Deserialize(new ISerializationToken[] { 1136 1138 new BeginToken("ROOT", 0, 0), 1137 1139 new PrimitiveToken("WriteOnly", 1, 1, new XmlString("<![CDATA[serial data]]>")), 1138 new EndToken( "ROOT", 0, 0)1140 new EndToken() 1139 1141 }); 1140 1142 Assert.AreEqual("serial data", newTest.value); … … 1168 1170 } 1169 1171 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 1170 1185 1171 1186
Note: See TracChangeset
for help on using the changeset viewer.