Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/17 13:26:49 (6 years ago)
Author:
jkarder
Message:

#2520: worked on new persistence

  • changed message definitions
  • updated transformers
  • worked on conversions
  • cleaned up
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  
    2525  public sealed class ComponentInfo {
    2626    public string Name { get; private set; }
     27    public string FullName { get; private set; }
    2728    public MemberInfo MemberInfo { get; private set; }
    2829    public StorableAttribute StorableAttribute { get; private set; }
     
    3031    public bool Writeable { get; private set; }
    3132
    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) {
    3334      Name = name;
     35      FullName = fullName;
    3436      MemberInfo = memberInfo;
    3537      StorableAttribute = storableAttribute;
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r15509 r15529  
    6060    private Dictionary<object, uint> object2BoxId;
    6161    private Dictionary<uint, object> boxId2Object;
    62     private Index<TypeBox> typeBoxes;
    63 
    6462    private Index<string> strings;
    6563
    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 
    7764    public uint BoxCount { get; private set; }
    78 
    7965
    8066    public Mapper() {
     
    8571      object2BoxId = new Dictionary<object, uint>(new MappingEqualityComparer());
    8672      boxId2Object = new Dictionary<uint, object>();
    87       typeBoxes = new Index<TypeBox>();
    88 
    8973      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>();
    10174
    10275      BoxCount = 0;
     
    164137      return o;
    165138    }
    166 
    167     #region Referenced Boxes
    168     #region TypeBoxes
    169     public uint GetTypeBoxId(TypeBox typeBox) { return typeBoxes.GetIndex(typeBox); }
    170     public TypeBox GetTypeBox(uint typeBoxId) { return typeBoxes.GetValue(typeBoxId); }
    171     #endregion
    172 
    173     #region BoolArrayBox
    174     public uint GetBoolArrayBoxId(BoolArrayBox boolArrayBox) { return boolArrayBoxes.GetIndex(boolArrayBox); }
    175     public BoolArrayBox GetBoolArrayBox(uint boolArrayBoxId) { return boolArrayBoxes.GetValue(boolArrayBoxId); }
    176     #endregion
    177 
    178     #region IntArrayBox
    179     public uint GetIntArrayBoxId(IntArrayBox intArrayBox) { return intArrayBoxes.GetIndex(intArrayBox); }
    180     public IntArrayBox GetIntArrayBox(uint intArrayBoxId) { return intArrayBoxes.GetValue(intArrayBoxId); }
    181     #endregion
    182 
    183     #region UnsignedIntArrayBox
    184     public uint GetUnsignedIntArrayBoxId(UnsignedIntArrayBox unsignedIntArrayBox) { return unsignedIntArrayBoxes.GetIndex(unsignedIntArrayBox); }
    185     public UnsignedIntArrayBox GetUnsignedIntArrayBox(uint unsignedIntArrayBoxId) { return unsignedIntArrayBoxes.GetValue(unsignedIntArrayBoxId); }
    186     #endregion
    187 
    188     #region LongArrayBox
    189     public uint GetLongArrayBoxId(LongArrayBox longArrayBox) { return longArrayBoxes.GetIndex(longArrayBox); }
    190     public LongArrayBox GetLongArrayBox(uint longArrayBoxId) { return longArrayBoxes.GetValue(longArrayBoxId); }
    191     #endregion
    192 
    193     #region UnsignedLongArrayBox
    194     public uint GetUnsignedLongArrayBoxId(UnsignedLongArrayBox unsignedLongArrayBox) { return unsignedLongArrayBoxes.GetIndex(unsignedLongArrayBox); }
    195     public UnsignedLongArrayBox GetUnsignedLongArrayBox(uint unsignedLongArrayBoxId) { return unsignedLongArrayBoxes.GetValue(unsignedLongArrayBoxId); }
    196     #endregion
    197 
    198     #region FloatArrayBox
    199     public uint GetFloatArrayBoxId(FloatArrayBox floatArrayBox) { return floatArrayBoxes.GetIndex(floatArrayBox); }
    200     public FloatArrayBox GetFloatArrayBox(uint floatArrayBoxId) { return floatArrayBoxes.GetValue(floatArrayBoxId); }
    201     #endregion
    202 
    203     #region DoubleArrayBox
    204     public uint GetDoubleArrayBoxId(DoubleArrayBox doubleArrayBox) { return doubleArrayBoxes.GetIndex(doubleArrayBox); }
    205     public DoubleArrayBox GetDoubleArrayBox(uint doubleArrayBoxId) { return doubleArrayBoxes.GetValue(doubleArrayBoxId); }
    206     #endregion
    207 
    208     #region DictionaryBox
    209     public uint GetDictionaryBoxId(DictionaryBox dictionaryBox) { return dictionaryBoxes.GetIndex(dictionaryBox); }
    210     public DictionaryBox GetDictionaryBox(uint dictionaryBoxId) { return dictionaryBoxes.GetValue(dictionaryBoxId); }
    211     #endregion
    212 
    213     #region StorableClassBox
    214     public uint GetStorableClassBoxId(StorableClassBox storableClassBox) { return storableClassBoxes.GetIndex(storableClassBox); }
    215     public StorableClassBox GetStorableClassBox(uint storableClassBoxId) { return storableClassBoxes.GetValue(storableClassBoxId); }
    216     #endregion
    217     #endregion
    218139    #endregion
    219140
     
    243164      bundle.TypeGuids.AddRange(mapper.types.GetValues().Select(x => ByteString.CopyFrom(StaticCache.GetGuid(x).ToByteArray())));
    244165      bundle.Boxes.AddRange(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
    245       bundle.TypeBoxes.AddRange(mapper.typeBoxes.GetValues());
    246 
    247166      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());
    259167
    260168      return bundle;
     
    265173      mapper.transformers = new Index<ITransformer>(bundle.TransformerGuids.Select(x => new Guid(x.ToByteArray())).Select(StaticCache.GetTransformer));
    266174      mapper.types = new Index<Type>(bundle.TypeGuids.Select(x => StaticCache.GetType(new Guid(x.ToByteArray()))));
    267 
    268175      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 
    271176      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);
    283177
    284178      return mapper.GetObject(bundle.RootBoxId);
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r15509 r15529  
    2525using System.Drawing;
    2626using System.Linq;
     27using System.Reflection;
    2728using HeuristicLab.PluginInfrastructure;
    2829
     
    3637    private readonly Dictionary<Type, Guid> type2Guid;
    3738    private readonly Dictionary<Type, TypeInfo> typeInfos;
     39    private readonly Dictionary<Guid, List<MethodInfo>> guid2ConversionMethods;
    3840
    3941    internal StaticCache() {
     
    4345      type2Guid = new Dictionary<Type, Guid>();
    4446      typeInfos = new Dictionary<Type, TypeInfo>();
     47      guid2ConversionMethods = new Dictionary<Guid, List<MethodInfo>>();
    4548
    4649      foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>())
     
    108111            if (StorableTypeAttribute.IsStorableType(t)) {
    109112              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              }
    110118            }
    111119          }
     
    149157      }
    150158    }
     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    }
    151167
    152168    // mainly for testing
     
    186202      }
    187203    }
     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    }
    188210  }
    189211}
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableConversionAttribute.cs

    r15034 r15529  
    2727  public sealed class StorableConversionAttribute : Attribute {
    2828    public Guid Guid { get; private set; }
    29     public uint SrcVersion { get; private set; }
     29    public uint Version { get; private set; }
    3030
    31     public StorableConversionAttribute(string guid, uint srcVersion) {
     31    public StorableConversionAttribute(string guid, uint version) {
    3232      this.Guid = new Guid(guid);
    33       this.SrcVersion = srcVersion;
     33      this.Version = version;
    3434    }
    3535
     
    4545    }
    4646    public static uint GetVersion(MethodInfo mi) {
    47       return GetStorableConversionAttribute(mi).SrcVersion;
     47      return GetStorableConversionAttribute(mi).Version;
    4848    }
    4949  }
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs

    r15034 r15529  
    5858      StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type);
    5959      if (StorableTypeAttribute != null) {
    60         // check constructors (
     60        string guidPrefix = StorableTypeAttribute.Guid.ToString().ToUpper();
     61        // check constructors
    6162        if (!type.IsValueType && !type.IsEnum && !type.IsInterface &&
    6263          GetStorableConstructor() == null && GetDefaultConstructor() == null)
    6364          throw new PersistenceException("No storable constructor or parameterless constructor found.");
    64 
    65         // traverse type hierarchy from base type to sub types
    66         Stack<Type> types = new Stack<Type>();
    67         while (type != null) {
    68           types.Push(type);
    69           type = type.BaseType;
    70         }
    7165
    7266        var fields = new List<ComponentInfo>();
     
    7468        var beforeSerializationHooks = new List<MethodInfo>();
    7569        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 fields
    81             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           }
    8970
    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
    9974
    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();
    10477
    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));
    11582          }
    11683        }
     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
    117115        Fields = fields;
    118116        Properties = properties;
     
    155153
    156154    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();
    161161    }
    162162
    163163    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);
    166165    }
    167166  }
Note: See TracChangeset for help on using the changeset viewer.