- Timestamp:
- 07/05/18 17:22:34 (6 years ago)
- Location:
- branches/PersistenceReintegration/HeuristicLab.Persistence/4.0
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Index.cs
r15509 r15986 59 59 } 60 60 61 public bool TryGetValue(uint index, out T value) { 62 return values.TryGetValue(index, out value); 63 } 64 61 65 public IEnumerable<T> GetValues() { 62 66 return values.Values; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r15529 r15986 93 93 public Type GetType(uint typeId) { 94 94 return types.GetValue(typeId); 95 } 96 97 public bool TryGetType(uint typeId, out Type type) { 98 return types.TryGetValue(typeId, out type); 95 99 } 96 100 #endregion … … 132 136 o = transformer.ToObject(box, this); 133 137 boxId2Object.Add(boxId, o); 134 transformer.FillFromBox(o, box, this);138 if (o != null) transformer.FillFromBox(o, box, this); 135 139 } 136 140 … … 176 180 mapper.strings = new Index<string>(bundle.Strings); 177 181 178 return mapper.GetObject(bundle.RootBoxId); 182 var root = mapper.GetObject(bundle.RootBoxId); 183 184 ExecuteAfterDeserializationHooks(mapper.boxId2Object.Values.ToArray()); 185 186 return root; 187 } 188 189 private static void ExecuteAfterDeserializationHooks(object[] objects) { 190 var emptyArgs = new object[0]; 191 192 foreach (var obj in objects) { 193 if (!StorableTypeAttribute.IsStorableType(obj.GetType())) continue; // TODO: performance 194 195 var typeStack = new Stack<Type>(); 196 for (var type = obj.GetType(); type != null; type = type.BaseType) { 197 typeStack.Push(type); 198 } 199 200 while (typeStack.Any()) { 201 var type = typeStack.Pop(); 202 if (!StorableTypeAttribute.IsStorableType(type)) continue; // object <- mytype (has hooks) or object <- valuetype <- mystruct 203 204 var typeInfo = staticCache.GetTypeInfo(type); 205 foreach (var hook in typeInfo.AfterDeserializationHooks) { 206 hook.Invoke(obj, emptyArgs); 207 } 208 } 209 } 179 210 } 180 211 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r15857 r15986 68 68 RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan)); 69 69 RegisterType(new Guid("ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69"), typeof(Font)); 70 RegisterType(new Guid("494C1352-A1C3-47A3-8754-A0B19B8F1567"), typeof(FontStyle)); 71 RegisterType(new Guid("41147B67-4C7A-4907-9F6C-509568395EDB"), typeof(GraphicsUnit)); 70 72 RegisterType(new Guid("E8348C94-9817-4164-9C98-377689F83F30"), typeof(Bitmap)); 71 73 RegisterType(new Guid("05551382-E894-4218-B860-FEE1D92CA07D"), typeof(Nullable<>)); … … 111 113 if (StorableTypeAttribute.IsStorableType(t)) { 112 114 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 }118 115 } 119 116 } … … 194 191 } 195 192 } 196 197 public IEnumerable<MethodInfo> GetStorableConversions(Guid guid) {198 List<MethodInfo> conversionMethods;199 if (guid2ConversionMethods.TryGetValue(guid, out conversionMethods)) return conversionMethods;200 return Enumerable.Empty<MethodInfo>();201 }202 203 public IEnumerable<MethodInfo> GetStorableConversions(IList<Tuple<Guid, uint>> typeChain) {204 foreach (var type in typeChain) {205 var conversionMethod = (from methodInfo in GetStorableConversions(type.Item1)206 let attrib = StorableConversionAttribute.GetStorableConversionAttribute(methodInfo)207 where attrib.Version == type.Item2 && (attrib.Dependencies == null ||208 attrib.Dependencies.All(x => typeChain.Any(y => y.Item1 == Guid.Parse(x.Key) && y.Item2 == x.Value)))209 select methodInfo).SingleOrDefault();210 if (conversionMethod != null)211 yield return conversionMethod;212 }213 }214 193 } 215 194 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableTypeAttribute.cs
r15034 r15986 37 37 38 38 public Guid Guid { get; private set; } 39 public uint Version { get; private set; }40 39 41 40 /// <summary> … … 43 42 /// </summary> 44 43 /// <param name="memberSelection">The storable class memberSelection.</param> 45 public StorableTypeAttribute(StorableMemberSelection memberSelection, string guid , uint version = 1) {44 public StorableTypeAttribute(StorableMemberSelection memberSelection, string guid) { 46 45 MemberSelection = memberSelection; 47 46 Guid = new Guid(guid); 48 Version = version;49 47 } 50 48 51 public StorableTypeAttribute(string guid , uint version = 1) {49 public StorableTypeAttribute(string guid) { 52 50 Guid = new Guid(guid); 53 Version = version;54 51 } 55 52 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15529 r15986 70 70 71 71 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) { 72 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 72 // TODO: improved performance for static fields 73 var fieldInfos = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 73 74 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields 74 75 … … 84 85 85 86 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 86 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 // TODO: improved performance for static properties 88 var propertyInfos = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 89 .Where(x => !x.GetIndexParameters().Any()); // exclude indexed properties 88 90 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/HeuristicLab.Persistence-4.0.csproj
r15509 r15986 65 65 <Compile Include="Core\StorableTypeAttribute.cs" /> 66 66 <Compile Include="Core\Transformer.cs" /> 67 <Compile Include="Core\StorableConversionAttribute.cs" />68 67 <Compile Include="Core\TransformerAttribute.cs" /> 69 68 <Compile Include="Core\TypeInfo.cs" /> -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto
r15529 r15986 17 17 18 18 uint32 type_id = 10; 19 uint32 type_version = 11; 20 repeated uint32 generic_type_box_ids = 12; 19 repeated uint32 generic_type_box_ids = 11; 21 20 22 21 bool bool = 20; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r15857 r15986 48 48 49 49 public override object ToObject(Box box, Mapper mapper) { 50 return Extract(box, (Type)mapper.GetObject(box.TypeBoxId), mapper); 50 var type = (Type)mapper.GetObject(box.TypeBoxId); 51 return type == null ? default(T) : Extract(box, type, mapper); 51 52 } 52 53 … … 80 81 box.TypeId = mapper.GetTypeId(type); 81 82 } 82 83 if (StorableTypeAttribute.IsStorableType(type))84 box.TypeVersion = StorableTypeAttribute.GetStorableTypeAttribute(type).Version;85 83 } 86 84 87 85 public override object ToObject(Box box, Mapper mapper) { 88 return Extract(box, mapper.GetType(box.TypeId), mapper); 86 Type type; 87 return mapper.TryGetType(box.TypeId, out type) ? Extract(box, type, mapper) : null; 89 88 } 90 89 91 90 private object Extract(Box box, Type type, Mapper mapper) { 92 91 if (type.IsGenericType) { 93 return type.MakeGenericType(box.GenericTypeBoxIds.Select(x => (Type)mapper.GetObject(x)).ToArray()); 92 var genericArgumentTypes = box.GenericTypeBoxIds.Select(x => (Type)mapper.GetObject(x)).ToArray(); 93 return genericArgumentTypes.Any(x => x == null) ? null : type.MakeGenericType(genericArgumentTypes); 94 94 } else if (type == typeof(Array)) { 95 return ((Type)mapper.GetObject(box.GenericTypeBoxIds[0])).MakeArrayType(); 95 var arrayType = (Type)mapper.GetObject(box.GenericTypeBoxIds[0]); 96 return arrayType?.MakeArrayType(); 96 97 } else { 97 98 return type; … … 526 527 var box = new Box { 527 528 TransformerId = mapper.GetTransformerId(this), 528 Type Id = mapper.GetStringId(o.GetType().AssemblyQualifiedName),529 UInt = mapper.GetStringId(Enum.Format(o.GetType(), o, "G")) ,529 TypeBoxId = mapper.GetBoxId(o.GetType()), 530 UInt = mapper.GetStringId(Enum.Format(o.GetType(), o, "G")) // TODO: introduce old names for enum values to enable refactoring 530 531 }; 531 532 return box; … … 533 534 534 535 public override object ToObject(Box box, Mapper mapper) { 535 return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(box.UInt)); 536 var type = (Type)mapper.GetObject(box.TypeBoxId); 537 return type == null ? null : Enum.Parse(type, mapper.GetString(box.UInt)); 536 538 } 537 539 } … … 784 786 protected override void Populate(Box box, object value, Mapper mapper) { 785 787 var kvps = box.KeyValuePairs; 786 var parentTypeVersions = box.UInts;787 788 var emptyArgs = new object[0]; 788 789 … … 815 816 } 816 817 817 if (type != originalType) {818 parentTypeVersions.AddRange(new[] { mapper.GetStringId(guid.ToString()), attribute.Version });819 }820 821 818 type = type.BaseType; 822 819 } … … 829 826 public override void FillFromBox(object obj, Box box, Mapper mapper) { 830 827 var kvps = box.KeyValuePairs; 831 var parentTypeVersions = box.UInts;832 828 var emptyArgs = new object[0]; 833 829 … … 840 836 841 837 var type = (Type)mapper.GetObject(box.TypeBoxId); 842 var typeBox = mapper.GetBox(box.TypeBoxId);843 838 var typeInfo = Mapper.StaticCache.GetTypeInfo(type); 844 845 var typeVersions = new List<Tuple<Guid, uint>>();846 typeVersions.Add(Tuple.Create(typeInfo.StorableTypeAttribute.Guid, typeBox.TypeVersion));847 for (int i = 0; i < parentTypeVersions.Count; i += 2)848 typeVersions.Add(Tuple.Create(Guid.Parse(mapper.GetString(parentTypeVersions[i])), parentTypeVersions[i + 1]));849 850 while (true) {851 var applicableConversion = Mapper.StaticCache.GetStorableConversions(typeVersions).FirstOrDefault();852 if (applicableConversion == null) break;853 854 Dictionary<string, object> newDict;855 List<Tuple<string, uint>> typeChain = null;856 857 var conversionParameters = applicableConversion.GetParameters();858 if (conversionParameters.Length == 1) {859 newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, new object[] { dict });860 } else if (conversionParameters.Length == 2) {861 var parameters = new object[] { dict, typeChain };862 newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, parameters);863 typeChain = (List<Tuple<string, uint>>)parameters[1];864 } else throw new PersistenceException("Conversion method has an invalid signature.");865 866 foreach (var kvp in newDict) dict[kvp.Key] = kvp.Value;867 868 var convertedType = StorableConversionAttribute.GetGuid(applicableConversion);869 var convertedVersion = StorableConversionAttribute.GetVersion(applicableConversion);870 871 var index = typeVersions.FindIndex(x => x.Item1 == convertedType);872 typeVersions.RemoveAt(index);873 typeVersions.Insert(index, Tuple.Create(convertedType, convertedVersion + 1));874 875 if (typeChain != null && typeChain.Any()) {876 typeVersions.RemoveRange(index + 1, typeVersions.Count - (index + 1));877 typeVersions.AddRange(typeChain.Select(x => Tuple.Create(Guid.Parse(x.Item1), x.Item2)));878 }879 }880 881 839 var typeStack = new Stack<Tuple<Type, TypeInfo>>(); 882 840 … … 905 863 } 906 864 907 // set all members as generated by conversion method chain865 // set all members 908 866 foreach (var kvp in dict) { 909 867 string key = kvp.Key; … … 940 898 // typeInfo.StorableTypeAttribute.Version, 941 899 // string.Join(", ", undefinedMembers.Select(x => x.Key)))); 942 943 foreach (var hook in typeInfo.AfterDeserializationHooks) {944 hook.Invoke(obj, emptyArgs);945 }946 900 } 947 901 }
Note: See TracChangeset
for help on using the changeset viewer.