- Timestamp:
- 12/15/17 13:26:49 (6 years ago)
- Location:
- branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/ComponentInfo.cs
r14927 r15529 25 25 public sealed class ComponentInfo { 26 26 public string Name { get; private set; } 27 public string FullName { get; private set; } 27 28 public MemberInfo MemberInfo { get; private set; } 28 29 public StorableAttribute StorableAttribute { get; private set; } … … 30 31 public bool Writeable { get; private set; } 31 32 32 public ComponentInfo(string name, MemberInfo memberInfo, StorableAttribute storableAttribute, bool readable, bool writeable) {33 public ComponentInfo(string name, string fullName, MemberInfo memberInfo, StorableAttribute storableAttribute, bool readable, bool writeable) { 33 34 Name = name; 35 FullName = fullName; 34 36 MemberInfo = memberInfo; 35 37 StorableAttribute = storableAttribute; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r15509 r15529 60 60 private Dictionary<object, uint> object2BoxId; 61 61 private Dictionary<uint, object> boxId2Object; 62 private Index<TypeBox> typeBoxes;63 64 62 private Index<string> strings; 65 63 66 private Index<BoolArrayBox> boolArrayBoxes;67 private Index<IntArrayBox> intArrayBoxes;68 private Index<UnsignedIntArrayBox> unsignedIntArrayBoxes;69 private Index<LongArrayBox> longArrayBoxes;70 private Index<UnsignedLongArrayBox> unsignedLongArrayBoxes;71 private Index<FloatArrayBox> floatArrayBoxes;72 private Index<DoubleArrayBox> doubleArrayBoxes;73 74 private Index<DictionaryBox> dictionaryBoxes;75 private Index<StorableClassBox> storableClassBoxes;76 77 64 public uint BoxCount { get; private set; } 78 79 65 80 66 public Mapper() { … … 85 71 object2BoxId = new Dictionary<object, uint>(new MappingEqualityComparer()); 86 72 boxId2Object = new Dictionary<uint, object>(); 87 typeBoxes = new Index<TypeBox>();88 89 73 strings = new Index<string>(); 90 91 boolArrayBoxes = new Index<BoolArrayBox>();92 intArrayBoxes = new Index<IntArrayBox>();93 unsignedIntArrayBoxes = new Index<UnsignedIntArrayBox>();94 longArrayBoxes = new Index<LongArrayBox>();95 unsignedLongArrayBoxes = new Index<UnsignedLongArrayBox>();96 floatArrayBoxes = new Index<FloatArrayBox>();97 doubleArrayBoxes = new Index<DoubleArrayBox>();98 99 dictionaryBoxes = new Index<DictionaryBox>();100 storableClassBoxes = new Index<StorableClassBox>();101 74 102 75 BoxCount = 0; … … 164 137 return o; 165 138 } 166 167 #region Referenced Boxes168 #region TypeBoxes169 public uint GetTypeBoxId(TypeBox typeBox) { return typeBoxes.GetIndex(typeBox); }170 public TypeBox GetTypeBox(uint typeBoxId) { return typeBoxes.GetValue(typeBoxId); }171 #endregion172 173 #region BoolArrayBox174 public uint GetBoolArrayBoxId(BoolArrayBox boolArrayBox) { return boolArrayBoxes.GetIndex(boolArrayBox); }175 public BoolArrayBox GetBoolArrayBox(uint boolArrayBoxId) { return boolArrayBoxes.GetValue(boolArrayBoxId); }176 #endregion177 178 #region IntArrayBox179 public uint GetIntArrayBoxId(IntArrayBox intArrayBox) { return intArrayBoxes.GetIndex(intArrayBox); }180 public IntArrayBox GetIntArrayBox(uint intArrayBoxId) { return intArrayBoxes.GetValue(intArrayBoxId); }181 #endregion182 183 #region UnsignedIntArrayBox184 public uint GetUnsignedIntArrayBoxId(UnsignedIntArrayBox unsignedIntArrayBox) { return unsignedIntArrayBoxes.GetIndex(unsignedIntArrayBox); }185 public UnsignedIntArrayBox GetUnsignedIntArrayBox(uint unsignedIntArrayBoxId) { return unsignedIntArrayBoxes.GetValue(unsignedIntArrayBoxId); }186 #endregion187 188 #region LongArrayBox189 public uint GetLongArrayBoxId(LongArrayBox longArrayBox) { return longArrayBoxes.GetIndex(longArrayBox); }190 public LongArrayBox GetLongArrayBox(uint longArrayBoxId) { return longArrayBoxes.GetValue(longArrayBoxId); }191 #endregion192 193 #region UnsignedLongArrayBox194 public uint GetUnsignedLongArrayBoxId(UnsignedLongArrayBox unsignedLongArrayBox) { return unsignedLongArrayBoxes.GetIndex(unsignedLongArrayBox); }195 public UnsignedLongArrayBox GetUnsignedLongArrayBox(uint unsignedLongArrayBoxId) { return unsignedLongArrayBoxes.GetValue(unsignedLongArrayBoxId); }196 #endregion197 198 #region FloatArrayBox199 public uint GetFloatArrayBoxId(FloatArrayBox floatArrayBox) { return floatArrayBoxes.GetIndex(floatArrayBox); }200 public FloatArrayBox GetFloatArrayBox(uint floatArrayBoxId) { return floatArrayBoxes.GetValue(floatArrayBoxId); }201 #endregion202 203 #region DoubleArrayBox204 public uint GetDoubleArrayBoxId(DoubleArrayBox doubleArrayBox) { return doubleArrayBoxes.GetIndex(doubleArrayBox); }205 public DoubleArrayBox GetDoubleArrayBox(uint doubleArrayBoxId) { return doubleArrayBoxes.GetValue(doubleArrayBoxId); }206 #endregion207 208 #region DictionaryBox209 public uint GetDictionaryBoxId(DictionaryBox dictionaryBox) { return dictionaryBoxes.GetIndex(dictionaryBox); }210 public DictionaryBox GetDictionaryBox(uint dictionaryBoxId) { return dictionaryBoxes.GetValue(dictionaryBoxId); }211 #endregion212 213 #region StorableClassBox214 public uint GetStorableClassBoxId(StorableClassBox storableClassBox) { return storableClassBoxes.GetIndex(storableClassBox); }215 public StorableClassBox GetStorableClassBox(uint storableClassBoxId) { return storableClassBoxes.GetValue(storableClassBoxId); }216 #endregion217 #endregion218 139 #endregion 219 140 … … 243 164 bundle.TypeGuids.AddRange(mapper.types.GetValues().Select(x => ByteString.CopyFrom(StaticCache.GetGuid(x).ToByteArray()))); 244 165 bundle.Boxes.AddRange(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value)); 245 bundle.TypeBoxes.AddRange(mapper.typeBoxes.GetValues());246 247 166 bundle.Strings.AddRange(mapper.strings.GetValues()); 248 249 bundle.BoolArrayBoxes.AddRange(mapper.boolArrayBoxes.GetValues());250 bundle.IntArrayBoxes.AddRange(mapper.intArrayBoxes.GetValues());251 bundle.UnsignedIntArrayBoxes.AddRange(mapper.unsignedIntArrayBoxes.GetValues());252 bundle.LongArrayBoxes.AddRange(mapper.longArrayBoxes.GetValues());253 bundle.UnsignedLongArrayBoxes.AddRange(mapper.unsignedLongArrayBoxes.GetValues());254 bundle.FloatArrayBoxes.AddRange(mapper.floatArrayBoxes.GetValues());255 bundle.DoubleArrayBoxes.AddRange(mapper.doubleArrayBoxes.GetValues());256 257 bundle.DictionaryBoxes.AddRange(mapper.dictionaryBoxes.GetValues());258 bundle.StorableClassBoxes.AddRange(mapper.storableClassBoxes.GetValues());259 167 260 168 return bundle; … … 265 173 mapper.transformers = new Index<ITransformer>(bundle.TransformerGuids.Select(x => new Guid(x.ToByteArray())).Select(StaticCache.GetTransformer)); 266 174 mapper.types = new Index<Type>(bundle.TypeGuids.Select(x => StaticCache.GetType(new Guid(x.ToByteArray())))); 267 268 175 mapper.boxId2Box = bundle.Boxes.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box); 269 mapper.typeBoxes = new Index<TypeBox>(bundle.TypeBoxes);270 271 176 mapper.strings = new Index<string>(bundle.Strings); 272 273 mapper.boolArrayBoxes = new Index<BoolArrayBox>(bundle.BoolArrayBoxes);274 mapper.intArrayBoxes = new Index<IntArrayBox>(bundle.IntArrayBoxes);275 mapper.unsignedIntArrayBoxes = new Index<UnsignedIntArrayBox>(bundle.UnsignedIntArrayBoxes);276 mapper.longArrayBoxes = new Index<LongArrayBox>(bundle.LongArrayBoxes);277 mapper.unsignedLongArrayBoxes = new Index<UnsignedLongArrayBox>(bundle.UnsignedLongArrayBoxes);278 mapper.floatArrayBoxes = new Index<FloatArrayBox>(bundle.FloatArrayBoxes);279 mapper.doubleArrayBoxes = new Index<DoubleArrayBox>(bundle.DoubleArrayBoxes);280 281 mapper.dictionaryBoxes = new Index<DictionaryBox>(bundle.DictionaryBoxes);282 mapper.storableClassBoxes = new Index<StorableClassBox>(bundle.StorableClassBoxes);283 177 284 178 return mapper.GetObject(bundle.RootBoxId); -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r15509 r15529 25 25 using System.Drawing; 26 26 using System.Linq; 27 using System.Reflection; 27 28 using HeuristicLab.PluginInfrastructure; 28 29 … … 36 37 private readonly Dictionary<Type, Guid> type2Guid; 37 38 private readonly Dictionary<Type, TypeInfo> typeInfos; 39 private readonly Dictionary<Guid, List<MethodInfo>> guid2ConversionMethods; 38 40 39 41 internal StaticCache() { … … 43 45 type2Guid = new Dictionary<Type, Guid>(); 44 46 typeInfos = new Dictionary<Type, TypeInfo>(); 47 guid2ConversionMethods = new Dictionary<Guid, List<MethodInfo>>(); 45 48 46 49 foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>()) … … 108 111 if (StorableTypeAttribute.IsStorableType(t)) { 109 112 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(t).Guid, t); 113 } 114 foreach (var mi in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)) { 115 if (StorableConversionAttribute.IsStorableConversionMethod(mi)) { 116 RegisterStorableConversion(StorableConversionAttribute.GetStorableConversionAttribute(mi).Guid, mi); 117 } 110 118 } 111 119 } … … 149 157 } 150 158 } 159 public void RegisterStorableConversion(Guid guid, MethodInfo methodInfo) { 160 lock (locker) { 161 List<MethodInfo> conversionMethods; 162 if (!guid2ConversionMethods.TryGetValue(guid, out conversionMethods)) 163 guid2ConversionMethods.Add(guid, conversionMethods = new List<MethodInfo>()); 164 conversionMethods.Add(methodInfo); 165 } 166 } 151 167 152 168 // mainly for testing … … 186 202 } 187 203 } 204 205 public IEnumerable<MethodInfo> GetStorableConversions(Guid guid) { 206 List<MethodInfo> conversionMethods; 207 if (guid2ConversionMethods.TryGetValue(guid, out conversionMethods)) return conversionMethods; 208 return Enumerable.Empty<MethodInfo>(); 209 } 188 210 } 189 211 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableConversionAttribute.cs
r15034 r15529 27 27 public sealed class StorableConversionAttribute : Attribute { 28 28 public Guid Guid { get; private set; } 29 public uint SrcVersion { get; private set; }29 public uint Version { get; private set; } 30 30 31 public StorableConversionAttribute(string guid, uint srcVersion) {31 public StorableConversionAttribute(string guid, uint version) { 32 32 this.Guid = new Guid(guid); 33 this. SrcVersion = srcVersion;33 this.Version = version; 34 34 } 35 35 … … 45 45 } 46 46 public static uint GetVersion(MethodInfo mi) { 47 return GetStorableConversionAttribute(mi). SrcVersion;47 return GetStorableConversionAttribute(mi).Version; 48 48 } 49 49 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15034 r15529 58 58 StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type); 59 59 if (StorableTypeAttribute != null) { 60 // check constructors ( 60 string guidPrefix = StorableTypeAttribute.Guid.ToString().ToUpper(); 61 // check constructors 61 62 if (!type.IsValueType && !type.IsEnum && !type.IsInterface && 62 63 GetStorableConstructor() == null && GetDefaultConstructor() == null) 63 64 throw new PersistenceException("No storable constructor or parameterless constructor found."); 64 65 // traverse type hierarchy from base type to sub types66 Stack<Type> types = new Stack<Type>();67 while (type != null) {68 types.Push(type);69 type = type.BaseType;70 }71 65 72 66 var fields = new List<ComponentInfo>(); … … 74 68 var beforeSerializationHooks = new List<MethodInfo>(); 75 69 var afterDeserializationHooks = new List<MethodInfo>(); 76 while (types.Count > 0) {77 type = types.Pop();78 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) {79 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic)80 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields81 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly)82 fieldInfos = fieldInfos.Where(x => StorableAttribute.IsStorable(x)).ToArray();83 foreach (var field in fieldInfos) {84 var attrib = StorableAttribute.GetStorableAttribute(field);85 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? field.Name : attrib.Name;86 fields.Add(new ComponentInfo(type.Name + '.' + name, field, attrib, true, true));87 }88 }89 70 90 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 91 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic). 92 Where(x => x.GetIndexParameters().Length == 0); // exclude indexed properties 93 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 94 propertyInfos = propertyInfos.Where(x => StorableAttribute.IsStorable(x)).ToArray(); 95 foreach (var property in propertyInfos) { 96 var attrib = StorableAttribute.GetStorableAttribute(property); 97 if ((!property.CanRead || !property.CanWrite) && (attrib == null || !attrib.AllowOneWay)) 98 throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization."); 71 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) { 72 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 73 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields 99 74 100 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name; 101 properties.Add(new ComponentInfo(type.Name + '.' + name, property, attrib, property.CanRead, property.CanWrite)); 102 } 103 } 75 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 76 fieldInfos = fieldInfos.Where(StorableAttribute.IsStorable).ToArray(); 104 77 105 var methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic). 106 Where(x => StorableHookAttribute.IsStorableHook(x)). 107 Where(x => (x.ReturnType == typeof(void)) && (x.GetParameters().Length == 0)); 108 foreach (var method in methodInfos) { 109 foreach (var attrib in StorableHookAttribute.GetStorableHookAttributes(method)) { 110 if (attrib.HookType == HookType.BeforeSerialization) 111 beforeSerializationHooks.Add(method); 112 if (attrib.HookType == HookType.AfterDeserialization) 113 afterDeserializationHooks.Add(method); 114 } 78 foreach (var field in fieldInfos) { 79 var attrib = StorableAttribute.GetStorableAttribute(field); 80 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? field.Name : attrib.Name; 81 fields.Add(new ComponentInfo(name, guidPrefix + "." + name, field, attrib, true, true)); 115 82 } 116 83 } 84 85 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 86 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 .Where(x => !x.GetIndexParameters().Any()); // exclude indexed properties 88 89 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 90 propertyInfos = propertyInfos.Where(StorableAttribute.IsStorable).ToArray(); 91 92 foreach (var property in propertyInfos) { 93 var attrib = StorableAttribute.GetStorableAttribute(property); 94 if ((!property.CanRead || !property.CanWrite) && (attrib == null || !attrib.AllowOneWay)) 95 throw new PersistenceException("Properties must be readable and writable or have one way serialization explicitly enabled."); 96 97 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name; 98 properties.Add(new ComponentInfo(name, guidPrefix + "." + name, property, attrib, property.CanRead, property.CanWrite)); 99 } 100 } 101 102 var methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 103 .Where(StorableHookAttribute.IsStorableHook) 104 .Where(x => x.ReturnType == typeof(void) && !x.GetParameters().Any()); 105 106 foreach (var method in methodInfos) { 107 foreach (var attrib in StorableHookAttribute.GetStorableHookAttributes(method)) { 108 if (attrib.HookType == HookType.BeforeSerialization) 109 beforeSerializationHooks.Add(method); 110 if (attrib.HookType == HookType.AfterDeserialization) 111 afterDeserializationHooks.Add(method); 112 } 113 } 114 117 115 Fields = fields; 118 116 Properties = properties; … … 155 153 156 154 private ConstructorInfo GetStorableConstructor() { 157 return Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 158 .Where(x => StorableConstructorAttribute.IsStorableConstructor(x)) 159 .Where(x => (x.GetParameters().Length == 1) && (x.GetParameters()[0].ParameterType == typeof(StorableConstructorFlag))) 160 .FirstOrDefault(); 155 return (from ctor in Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 156 let parameters = ctor.GetParameters() 157 where StorableConstructorAttribute.IsStorableConstructor(ctor) 158 && parameters.Length == 1 159 && parameters[0].ParameterType == typeof(StorableConstructorFlag) 160 select ctor).FirstOrDefault(); 161 161 } 162 162 163 163 private ConstructorInfo GetDefaultConstructor() { 164 return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 165 null, Type.EmptyTypes, null); 164 return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); 166 165 } 167 166 }
Note: See TracChangeset
for help on using the changeset viewer.