Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/05/18 17:22:34 (6 years ago)
Author:
jkarder
Message:

#2520: worked on new persistence

  • removed type version
  • removed conversions
  • execute AfterDeserialization hooks after all objects have been populated
  • worked on transformers
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  
    5959    }
    6060
     61    public bool TryGetValue(uint index, out T value) {
     62      return values.TryGetValue(index, out value);
     63    }
     64
    6165    public IEnumerable<T> GetValues() {
    6266      return values.Values;
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r15529 r15986  
    9393    public Type GetType(uint typeId) {
    9494      return types.GetValue(typeId);
     95    }
     96
     97    public bool TryGetType(uint typeId, out Type type) {
     98      return types.TryGetValue(typeId, out type);
    9599    }
    96100    #endregion
     
    132136        o = transformer.ToObject(box, this);
    133137        boxId2Object.Add(boxId, o);
    134         transformer.FillFromBox(o, box, this);
     138        if (o != null) transformer.FillFromBox(o, box, this);
    135139      }
    136140
     
    176180      mapper.strings = new Index<string>(bundle.Strings);
    177181
    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      }
    179210    }
    180211  }
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r15857 r15986  
    6868      RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
    6969      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));
    7072      RegisterType(new Guid("E8348C94-9817-4164-9C98-377689F83F30"), typeof(Bitmap));
    7173      RegisterType(new Guid("05551382-E894-4218-B860-FEE1D92CA07D"), typeof(Nullable<>));
     
    111113            if (StorableTypeAttribute.IsStorableType(t)) {
    112114              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               }
    118115            }
    119116          }
     
    194191      }
    195192    }
    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     }
    214193  }
    215194}
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableTypeAttribute.cs

    r15034 r15986  
    3737
    3838    public Guid Guid { get; private set; }
    39     public uint Version { get; private set; }
    4039
    4140    /// <summary>
     
    4342    /// </summary>
    4443    /// <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) {
    4645      MemberSelection = memberSelection;
    4746      Guid = new Guid(guid);
    48       Version = version;
    4947    }
    5048
    51     public StorableTypeAttribute(string guid, uint version = 1) {
     49    public StorableTypeAttribute(string guid) {
    5250      Guid = new Guid(guid);
    53       Version = version;
    5451    }
    5552
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs

    r15529 r15986  
    7070
    7171        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)
    7374                               .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields
    7475
     
    8485
    8586        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)
    8789                                  .Where(x => !x.GetIndexParameters().Any());  // exclude indexed properties
    8890
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/HeuristicLab.Persistence-4.0.csproj

    r15509 r15986  
    6565    <Compile Include="Core\StorableTypeAttribute.cs" />
    6666    <Compile Include="Core\Transformer.cs" />
    67     <Compile Include="Core\StorableConversionAttribute.cs" />
    6867    <Compile Include="Core\TransformerAttribute.cs" />
    6968    <Compile Include="Core\TypeInfo.cs" />
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto

    r15529 r15986  
    1717
    1818  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;
    2120
    2221  bool bool = 20;
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r15857 r15986  
    4848
    4949    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);
    5152    }
    5253
     
    8081        box.TypeId = mapper.GetTypeId(type);
    8182      }
    82 
    83       if (StorableTypeAttribute.IsStorableType(type))
    84         box.TypeVersion = StorableTypeAttribute.GetStorableTypeAttribute(type).Version;
    8583    }
    8684
    8785    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;
    8988    }
    9089
    9190    private object Extract(Box box, Type type, Mapper mapper) {
    9291      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);
    9494      } 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();
    9697      } else {
    9798        return type;
     
    526527        var box = new Box {
    527528          TransformerId = mapper.GetTransformerId(this),
    528           TypeId = 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
    530531        };
    531532        return box;
     
    533534
    534535      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));
    536538      }
    537539    }
     
    784786    protected override void Populate(Box box, object value, Mapper mapper) {
    785787      var kvps = box.KeyValuePairs;
    786       var parentTypeVersions = box.UInts;
    787788      var emptyArgs = new object[0];
    788789
     
    815816        }
    816817
    817         if (type != originalType) {
    818           parentTypeVersions.AddRange(new[] { mapper.GetStringId(guid.ToString()), attribute.Version });
    819         }
    820 
    821818        type = type.BaseType;
    822819      }
     
    829826    public override void FillFromBox(object obj, Box box, Mapper mapper) {
    830827      var kvps = box.KeyValuePairs;
    831       var parentTypeVersions = box.UInts;
    832828      var emptyArgs = new object[0];
    833829
     
    840836
    841837      var type = (Type)mapper.GetObject(box.TypeBoxId);
    842       var typeBox = mapper.GetBox(box.TypeBoxId);
    843838      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 
    881839      var typeStack = new Stack<Tuple<Type, TypeInfo>>();
    882840
     
    905863        }
    906864
    907         // set all members as generated by conversion method chain
     865        // set all members
    908866        foreach (var kvp in dict) {
    909867          string key = kvp.Key;
     
    940898        //    typeInfo.StorableTypeAttribute.Version,
    941899        //    string.Join(", ", undefinedMembers.Select(x => x.Key))));
    942 
    943         foreach (var hook in typeInfo.AfterDeserializationHooks) {
    944           hook.Invoke(obj, emptyArgs);
    945         }
    946900      }
    947901    }
Note: See TracChangeset for help on using the changeset viewer.