- Timestamp:
- 09/12/11 10:12:50 (13 years ago)
- Location:
- branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core
- Files:
-
- 4 added
- 1 deleted
- 4 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 }
Note: See TracChangeset
for help on using the changeset viewer.