Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14537


Ignore:
Timestamp:
01/03/17 18:21:40 (8 years ago)
Author:
jkarder
Message:

#2520: worked on persistence

Location:
branches/PersistenceOverhaul
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul

    • Property svn:ignore
      •  

        old new  
        2424protoc.exe
        2525obj
         26.vs
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r13358 r14537  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.PluginInfrastructure;
    2625using Google.ProtocolBuffers;
    27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2826
    2927namespace HeuristicLab.Persistence {
     
    122120      bundle.RootBoxId = mapper.GetBoxId(o);
    123121      bundle.AddRangeTransformerGuids(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
    124       bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFrom(x.ToByteArray())));
     122      bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFromUtf8(x)));
    125123      bundle.AddRangeStrings(mapper.strings.GetValues());
    126124      bundle.AddRangeBoxes(mapper.boxes.GetValues());
     
    129127    public static object ToObject(Bundle bundle) {
    130128      var mapper = new Mapper();
    131       mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetType(x)));
     129      mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => StaticCache.GetType(x)));
    132130      mapper.strings = new Index<string>(bundle.StringsList);
    133131      mapper.boxes = new Index<Box>(bundle.BoxesList);
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r13407 r14537  
    3434    private Dictionary<Guid, ITransformer> guid2Transformer;
    3535    private Dictionary<ITransformer, Guid> transformer2Guid;
    36     private Dictionary<Guid, Type> guid2Type;
    37     private Dictionary<Type, Guid> type2Guid;
     36    private Dictionary<string, Type> guid2Type;
     37    private Dictionary<Type, string> type2Guid;
    3838    private Dictionary<Type, TypeInfo> typeInfos;
    3939    private ExtensionRegistry extensionRegistry;
     
    4242      guid2Transformer = new Dictionary<Guid, ITransformer>();
    4343      transformer2Guid = new Dictionary<ITransformer, Guid>();
    44       guid2Type = new Dictionary<Guid, Type>();
    45       type2Guid = new Dictionary<Type, Guid>();
     44      guid2Type = new Dictionary<string, Type>();
     45      type2Guid = new Dictionary<Type, string>();
    4646      typeInfos = new Dictionary<Type, TypeInfo>();
    4747      extensionRegistry = ExtensionRegistry.CreateInstance();
    4848
    49       foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Priority))
     49      foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>())
    5050        RegisterTransformer(transformer);
    5151
    52       RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
    53       RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
    54       RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
    55       RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
    56       RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
    57       RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
    58       RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
    59       RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
    60       RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
    61       RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
    62       RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
    63       RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
    64       RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
    65       RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
    66       RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
    67       RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
    68       RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
    69       RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
    70 
    71       RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
    72       RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
    73       RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
    74       RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
    75       RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
    76       RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
    77       RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
    78       RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
    79       RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
    80       RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
    81       RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
    82       RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
    83       RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
     52
     53      // ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69
     54      // E8348C94-9817-4164-9C98-377689F83F30
     55      // 05551382-E894-4218-B860-FEE1D92CA07D
     56      // 4CC0D44E-65B2-4DF1-A333-30F058CB78CA
     57      // 5D451A64-EBD9-411F-A07E-232250B33784
     58      // EEBD2D26-56CC-45EA-900A-DD29B9F598CE
     59      // B8EEEFA7-8576-42DD-937C-AFBB66297B56
     60      // 4C3C8E0A-7DCF-4814-B798-AECB86E1E4E3
     61      // EAA4FF91-B90C-47CB-AC0F-66AB66D41186
     62      // EB51B342-826F-4004-B2C6-F2A03A8C35C9
     63      // 14138614-45DB-4A0D-9E59-AE9373241D6B
     64
     65
     66      //       RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
     67      //       RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
     68      //       RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
     69      //       RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
     70      //       RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
     71      //       RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
     72      //       RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
     73      //       RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
     74      //       RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
     75      //       RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
     76      //       RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
     77      //       RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
     78      //       RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
     79      //       RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
     80      //       RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
     81      //       RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
     82      //       RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
     83      //       RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
     84      //
     85      //       RegisterType(new Guid("9CF55419-439B-4A90-B2ED-8C7F7768EB61"), typeof(Array));
     86      //       RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
     87      //       RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
     88      //       RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
     89      //       RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
     90      //       RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
     91      //       RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
     92      //       RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
     93      //       RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
     94      //       RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
     95      //       RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
     96      //       RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
     97      //       RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
     98      //       RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
     99      //       RegisterType(new Guid("DB2A785C-5803-42C7-A01F-E1FD845A83A1"), typeof(List<>));
     100      //       RegisterType(new Guid("EB98A564-BEDC-458A-9E9B-4BF3CDAFB9BE"), typeof(Stack<>));
     101      //       RegisterType(new Guid("D5E3118B-957A-43B5-A740-1BBAA0EAB666"), typeof(System.Collections.Stack));
     102      //       RegisterType(new Guid("7E2F3277-7216-4295-A498-9ACA43527D5B"), typeof(HashSet<>));
     103      //       RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>));
     104
     105
     106      RegisterTypeAndFullName(typeof(bool));
     107      RegisterTypeAndFullName(typeof(byte));
     108      RegisterTypeAndFullName(typeof(sbyte));
     109      RegisterTypeAndFullName(typeof(short));
     110      RegisterTypeAndFullName(typeof(ushort));
     111      RegisterTypeAndFullName(typeof(char));
     112      RegisterTypeAndFullName(typeof(int));
     113      RegisterTypeAndFullName(typeof(uint));
     114      RegisterTypeAndFullName(typeof(long));
     115      RegisterTypeAndFullName(typeof(ulong));
     116      RegisterTypeAndFullName(typeof(float));
     117      RegisterTypeAndFullName(typeof(double));
     118      RegisterTypeAndFullName(typeof(DateTime));
     119      RegisterTypeAndFullName(typeof(TimeSpan));
     120      RegisterTypeAndFullName(typeof(Color));
     121      RegisterTypeAndFullName(typeof(Point));
     122      RegisterTypeAndFullName(typeof(KeyValuePair<,>));
     123      RegisterTypeAndFullName(typeof(string));
     124
     125      RegisterTypeAndFullName(typeof(Array));
     126      RegisterTypeAndFullName(typeof(bool[]));
     127      RegisterTypeAndFullName(typeof(byte[]));
     128      RegisterTypeAndFullName(typeof(sbyte[]));
     129      RegisterTypeAndFullName(typeof(short[]));
     130      RegisterTypeAndFullName(typeof(ushort[]));
     131      RegisterTypeAndFullName(typeof(char[]));
     132      RegisterTypeAndFullName(typeof(int[]));
     133      RegisterTypeAndFullName(typeof(uint[]));
     134      RegisterTypeAndFullName(typeof(long[]));
     135      RegisterTypeAndFullName(typeof(ulong[]));
     136      RegisterTypeAndFullName(typeof(float[]));
     137      RegisterTypeAndFullName(typeof(double[]));
     138      RegisterTypeAndFullName(typeof(string[]));
     139      RegisterTypeAndFullName(typeof(List<>));
     140      RegisterTypeAndFullName(typeof(Stack<>));
     141      RegisterTypeAndFullName(typeof(System.Collections.Stack));
     142      RegisterTypeAndFullName(typeof(HashSet<>));
     143      RegisterTypeAndFullName(typeof(Dictionary<,>));
     144
     145
    84146      foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
    85147        foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x)))
    86           RegisterType(StorableClassAttribute.GetStorableClassAttribute(t).Guid, t);
     148          RegisterTypeAndFullName(t);
     149      }
     150
     151      var registeredTypes = type2Guid.Keys.ToArray();
     152      foreach (var t in registeredTypes) {
     153        AddBaseTypesRec(t);
    87154      }
    88155
     
    97164      RegisterExtension(BytesBox.Bytes);
    98165      RegisterExtension(BoolArrayBox.BoolArray);
     166      RegisterExtension(ByteArrayBox.ByteArray);
    99167      RegisterExtension(IntArrayBox.IntArray);
    100168      RegisterExtension(LongArrayBox.LongArray);
     
    108176      RegisterExtension(IntMatrixBox.IntMatrix);
    109177      RegisterExtension(DictionaryBox.Dictionary);
     178      RegisterExtension(StorableClassBox.StorableClass);
     179      RegisterExtension(TypeBox.Type);
     180    }
     181
     182    private void AddBaseTypesRec(Type t) {
     183      foreach (var interfaceType in t.GetInterfaces()) {
     184        RegisterTypeAndFullName(interfaceType);
     185        AddBaseTypesRec(interfaceType);
     186      }
     187      if (t.BaseType != null) {
     188        RegisterTypeAndFullName(t.BaseType);
     189        AddBaseTypesRec(t.BaseType);
     190      }
    110191    }
    111192
     
    116197      }
    117198    }
    118     public void RegisterType(Guid guid, Type type) {
    119       lock (locker) {
    120         guid2Type.Add(guid, type);
    121         type2Guid.Add(type, guid);
     199    //    public void RegisterType(Guid guid, Type type) {
     200    //      lock (locker) {
     201    //        guid2Type.Add(guid, type);
     202    //        type2Guid.Add(type, guid);
     203    //      }
     204    //    }
     205    public void RegisterTypeAndFullName(Type type) {
     206      lock (locker) {
     207        var key = type.AssemblyQualifiedName ?? type.Name;
     208        if (!guid2Type.ContainsKey(key)) {
     209          guid2Type.Add(key, type);
     210          type2Guid.Add(type, key);
     211        }
    122212      }
    123213    }
     
    134224      return transformer2Guid[transformer];
    135225    }
    136     public Type GetType(Guid guid) {
     226    public Type GetType(string guid) {
    137227      return guid2Type[guid];
    138228    }
    139     public Guid GetGuid(Type type) {
     229    public string GetGuid(Type type) {
    140230      return type2Guid[type];
    141231    }
     
    144234        TypeInfo typeInfo;
    145235        if (!typeInfos.TryGetValue(type, out typeInfo)) {
    146           var transformer = guid2Transformer.Values.First(x => x.CanTransformType(type));
     236          var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type));
    147237          typeInfo = new TypeInfo(type, transformer);
    148238          typeInfos.Add(type, typeInfo);
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/HeuristicLab.Persistence-4.0.csproj

    r13358 r14537  
    6262    <Compile Include="Properties\AssemblyInfo.cs" />
    6363    <Compile Include="Protos\PersistenceMessages.cs" />
     64    <Compile Include="Transformers\StorableClassTransformer.cs" />
    6465    <Compile Include="Transformers\Transformers.cs" />
    6566  </ItemGroup>
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto

    r13375 r14537  
    1515  extensions 100 to max;
    1616}
     17
    1718
    1819// scalar boxes
     
    7980  repeated bool values = 1 [packed = true];
    8081}
     82message ByteArrayBox {
     83  extend Box {
     84    required ByteArrayBox byte_array = 121;
     85  }
     86  required bytes values = 1;
     87}
    8188message IntArrayBox {
    8289  extend Box {
    83     required IntArrayBox int_array = 121;
     90    required IntArrayBox int_array = 122;
    8491  }
    8592  repeated int32 values = 1 [packed = true];
     
    8794message LongArrayBox {
    8895  extend Box {
    89     required LongArrayBox long_array = 122;
     96    required LongArrayBox long_array = 123;
    9097  }
    9198  repeated int64 values = 1 [packed = true];
     
    93100message UnsignedIntArrayBox {
    94101  extend Box {
    95     required UnsignedIntArrayBox unsigned_int_array = 123;
     102    required UnsignedIntArrayBox unsigned_int_array = 124;
    96103  }
    97104  repeated uint32 values = 1 [packed = true];
     105
     106  extensions 100 to max;
    98107}
    99108message UnsignedLongArrayBox {
    100109  extend Box {
    101     required UnsignedLongArrayBox unsigned_long_array = 124;
     110    required UnsignedLongArrayBox unsigned_long_array = 125;
    102111  }
    103112  repeated uint64 values = 1 [packed = true];
     
    105114message FloatArrayBox {
    106115  extend Box {
    107     required FloatArrayBox float_array = 125;
     116    required FloatArrayBox float_array = 126;
    108117  }
    109118  repeated float values = 1 [packed = true];
     
    111120message DoubleArrayBox {
    112121  extend Box {
    113     required DoubleArrayBox double_array = 126;
     122    required DoubleArrayBox double_array = 127;
    114123  }
    115124  repeated double values = 1 [packed = true];
     
    117126message StringArrayBox {
    118127  extend Box {
    119     required StringArrayBox string_array = 127;
     128    required StringArrayBox string_array = 128;
    120129  }
    121130  repeated string values = 1;
     
    154163  required uint32 comparer_id = 3;
    155164}
     165
     166message StorableClassBox {
     167  extend Box {
     168    required StorableClassBox storable_class = 200;
     169  }
     170  repeated uint32 key_ids = 1 [packed = true];
     171  repeated uint32 value_ids = 2 [packed = true];
     172}
     173
     174
     175message TypeBox {
     176  extend Box {
     177    required TypeBox type = 250;
     178  }
     179  repeated uint32 generic_type_ids = 1 [packed = true];
     180}
     181
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/StorableClassTransformer.cs

    r13326 r14537  
    2424using System.Linq;
    2525using System.Reflection;
    26 using HeuristicLab.Persistence.Data;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
    2928namespace HeuristicLab.Persistence {
    3029  [Transformer("78556C88-0FEE-4602-95C7-A469B2DDB468", 100)]
    31   internal sealed class StorableClassTransformer : TransformerBase {
     30  internal sealed class StorableClassBoxTransformer : BoxTransformer<object> {
    3231    public override bool CanTransformType(Type type) {
    3332      return StorableClassAttribute.IsStorableClass(type);
    3433    }
    35     public override PersistenceData ToData(object o, PersistenceMapper mapper) {
    36       var type = o.GetType();
    37       var typeInfo = mapper.GetTypeInfo(type);
    38       var attribute = typeInfo.StorableClassAttribute;
    39       var data = new CompositeData(Id);
    40       mapper.Cache(o, data);
    41       data.TypeId = mapper.GetTypeId(type);
    42       // data.GuidId = attribute.Guid;
    43       // data.Version = attribute.Version;
     34
     35    protected override void Populate(Box.Builder box, object value, Mapper mapper) {
     36      var b = StorableClassBox.CreateBuilder();
     37      var type = value.GetType();
     38      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
    4439
    4540      var emptyArgs = new object[0];
    4641      foreach (var hook in typeInfo.BeforeSerializationHooks) {
    47         hook.Invoke(o, emptyArgs);
     42        hook.Invoke(value, emptyArgs);
    4843      }
     44
    4945      var components = new Dictionary<uint, uint>();
    5046      foreach (var componentInfo in typeInfo.Fields) {
    5147        var field = (FieldInfo)componentInfo.MemberInfo;
    52         var component = mapper.GetDataId(field.GetValue(o));
     48        var component = mapper.GetBoxId(field.GetValue(value));
    5349        components.Add(mapper.GetStringId(componentInfo.Name), component);
    5450      }
     51
    5552      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Readable)) {
    5653        var property = (PropertyInfo)componentInfo.MemberInfo;
    57         var component = mapper.GetDataId(property.GetValue(o, null));
     54        var component = mapper.GetBoxId(property.GetValue(value, null));
    5855        components.Add(mapper.GetStringId(componentInfo.Name), component);
    5956      }
    60       data.ComponentNameIds = components.Keys.ToArray();
    61       data.ComponentIds = components.Values.ToArray();
    62       return data;
     57
     58      b.AddRangeKeyIds(components.Keys);
     59      b.AddRangeValueIds(components.Values);
     60
     61      box.SetExtension(StorableClassBox.StorableClass, b.Build());
    6362    }
    6463
    65     public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
    66       var composite = (CompositeData)data;
    67       var type = mapper.GetType(composite.TypeId);
    68       var typeInfo = mapper.GetTypeInfo(type);
     64    protected override object Extract(Box box, Type type, Mapper mapper) {
     65      var data = box.GetExtension(StorableClassBox.StorableClass);
     66      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
    6967      var o = mapper.CreateInstance(type);
    70       mapper.Cache(data, o);
    7168
    7269      var components = new Dictionary<uint, uint>();
    73       for (int i = 0; i < composite.ComponentNameIds.Length; i++) {
    74         components.Add(composite.ComponentNameIds[i], composite.ComponentIds[i]);
     70      for (int i = 0; i < data.KeyIdsList.Count; i++) {
     71        components.Add(data.KeyIdsList[i], data.ValueIdsList[i]);
    7572      }
    7673
     
    7875        var field = (FieldInfo)componentInfo.MemberInfo;
    7976        uint componentId;
    80         components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
    81         field.SetValue(o, componentId != 0 ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue);
     77        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
     78        field.SetValue(o, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue);
    8279      }
     80
    8381      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) {
    8482        var property = (PropertyInfo)componentInfo.MemberInfo;
    8583        uint componentId;
    86         components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
    87         property.SetValue(o, componentId != 0 ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue, null);
     84        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
     85        property.SetValue(o, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue, null);
    8886      }
     87
    8988      var emptyArgs = new object[0];
    9089      foreach (var hook in typeInfo.AfterDeserializationHooks) {
    9190        hook.Invoke(o, emptyArgs);
    9291      }
     92
    9393      return o;
    9494    }
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r13407 r14537  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Drawing;
     
    158159
    159160  #region Array Box Transformers
     161  public abstract class ByteArrayBoxTransformer<T> : BoxTransformer<T> {
     162    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     163      var b = ByteArrayBox.CreateBuilder();
     164      b.SetValues(ToByteString(value));
     165      box.SetExtension(ByteArrayBox.ByteArray, b.Build());
     166    }
     167    protected override T Extract(Box box, Type type, Mapper mapper) {
     168      return FromByteString(box.GetExtension(ByteArrayBox.ByteArray).Values);
     169    }
     170    protected abstract ByteString ToByteString(T value);
     171    protected abstract T FromByteString(ByteString byteString);
     172  }
    160173  public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> {
    161174    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     
    407420  #endregion
    408421
    409   #region Type
    410   [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
    411   internal sealed class TypeTransformer : Transformer {
    412     public override bool CanTransformType(Type type) {
    413       return typeof(Type).IsAssignableFrom(type);
    414     }
    415     public override Box ToBox(object o, Mapper mapper) {
    416       var boxBuilder = Box.CreateBuilder();
    417 
    418       boxBuilder.TransformerId = mapper.GetTransformerId(this);
    419       boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
    420 
    421       return boxBuilder.Build();
    422     }
    423 
    424     public override object ToObject(Box box, Mapper mapper) {
    425       return Type.GetType(mapper.GetString(box.TypeId));
    426     }
    427   }
    428   #endregion
     422  //   #region Type
     423  //   [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
     424  //   internal sealed class TypeTransformer : Transformer {
     425  //     public override bool CanTransformType(Type type) {
     426  //       return typeof(Type).IsAssignableFrom(type);
     427  //     }
     428  //     public override Box ToBox(object o, Mapper mapper) {
     429  //       var boxBuilder = Box.CreateBuilder();
     430  //
     431  //       boxBuilder.TransformerId = mapper.GetTransformerId(this);
     432  //       boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
     433  //
     434  //       return boxBuilder.Build();
     435  //     }
     436  //
     437  //     public override object ToObject(Box box, Mapper mapper) {
     438  //       return Type.GetType(mapper.GetString(box.TypeId));
     439  //     }
     440  //   }
     441  //   #endregion
    429442
    430443  #region Array Types
     444  [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
     445  internal sealed class ByteArrayTransformer : ByteArrayBoxTransformer<byte[]> {
     446    protected override ByteString ToByteString(byte[] value) {
     447      return ByteString.CopyFrom(value);
     448    }
     449    protected override byte[] FromByteString(ByteString byteString) {
     450      return byteString.ToArray();
     451    }
     452  }
     453
     454  [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
     455  internal sealed class SByteArrayTransformer : ByteArrayBoxTransformer<sbyte[]> {
     456    protected override ByteString ToByteString(sbyte[] value) {
     457      return ByteString.CopyFrom(value.Select(b => (byte)b).ToArray());
     458    }
     459    protected override sbyte[] FromByteString(ByteString byteString) {
     460      return byteString.Select(b => (sbyte)b).ToArray();
     461    }
     462  }
     463
    431464  [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)]
    432465  internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> {
     
    435468  }
    436469
    437   [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
    438   internal sealed class ByteArrayTransformer : UnsignedIntArrayBoxTransformer<byte[]> {
    439     protected override IEnumerable<uint> ToBoxType(byte[] value, Mapper mapper) { return value.Cast<uint>(); }
    440     protected override byte[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<byte>().ToArray(); }
    441   }
    442 
    443   [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
    444   internal sealed class SByteArrayTransformer : IntArrayBoxTransformer<sbyte[]> {
    445     protected override IEnumerable<int> ToBoxType(sbyte[] value, Mapper mapper) { return value.Cast<int>(); }
    446     protected override sbyte[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.Cast<sbyte>().ToArray(); }
    447   }
    448 
    449470  [Transformer("2811FDD4-6800-4CBA-86D7-9071ED5775ED", 303)]
    450   internal sealed class ShortArrayTransformer : IntArrayBoxTransformer<short[]> {
    451     protected override IEnumerable<int> ToBoxType(short[] value, Mapper mapper) { return value.Cast<int>(); }
    452     protected override short[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.Cast<short>().ToArray(); }
     471  internal sealed class ShortArrayTransformer : ByteArrayBoxTransformer<short[]> {
     472    protected override ByteString ToByteString(short[] value) {
     473      var res = new byte[value.Length * 2];
     474      for (int i = 0; i < value.Length; i++) {
     475        var bytes = BitConverter.GetBytes(value[i]);
     476        Array.Copy(bytes, 0, res, i * 2, 2);
     477      }
     478      return ByteString.CopyFrom(res);
     479    }
     480    protected override short[] FromByteString(ByteString byteString) {
     481      var bytes = byteString.ToArray();
     482      var res = new short[bytes.Length / 2];
     483      for (int i = 0; i < bytes.Length; i += 2) {
     484        res[i / 2] = BitConverter.ToInt16(bytes, i);
     485      }
     486      return res;
     487    }
    453488  }
    454489
    455490  [Transformer("1AAC2625-356C-40BC-8CB4-15CB3D047EB8", 304)]
    456   internal sealed class UShortArrayTransformer : UnsignedIntArrayBoxTransformer<ushort[]> {
    457     protected override IEnumerable<uint> ToBoxType(ushort[] value, Mapper mapper) { return value.Cast<uint>(); }
    458     protected override ushort[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<ushort>().ToArray(); }
     491  internal sealed class UShortArrayTransformer : ByteArrayBoxTransformer<ushort[]> {
     492    protected override ByteString ToByteString(ushort[] value) {
     493      var res = new byte[value.Length * 2];
     494      for (int i = 0; i < value.Length; i++) {
     495        var bytes = BitConverter.GetBytes(value[i]);
     496        Array.Copy(bytes, 0, res, i * 2, 2);
     497      }
     498      return ByteString.CopyFrom(res);
     499    }
     500    protected override ushort[] FromByteString(ByteString byteString) {
     501      var bytes = byteString.ToArray();
     502      var res = new ushort[bytes.Length / 2];
     503      for (int i = 0; i < bytes.Length; i += 2) {
     504        res[i / 2] = BitConverter.ToUInt16(bytes, i);
     505      }
     506      return res;
     507    }
    459508  }
    460509
    461510  [Transformer("12F19098-5D49-4C23-8897-69087F1C146D", 305)]
    462   internal sealed class CharArrayTransformer : UnsignedIntArrayBoxTransformer<char[]> {
    463     protected override IEnumerable<uint> ToBoxType(char[] value, Mapper mapper) { return value.Cast<uint>(); }
    464     protected override char[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<char>().ToArray(); }
     511  internal sealed class CharArrayTransformer : ByteArrayBoxTransformer<char[]> {
     512    protected override ByteString ToByteString(char[] value) {
     513      var res = new byte[value.Length * 2];
     514      for (int i = 0; i < value.Length; i++) {
     515        var bytes = BitConverter.GetBytes(value[i]);
     516        Array.Copy(bytes, 0, res, i * 2, 2);
     517      }
     518      return ByteString.CopyFrom(res);
     519    }
     520    protected override char[] FromByteString(ByteString byteString) {
     521      var bytes = byteString.ToArray();
     522      var res = new char[bytes.Length / 2];
     523      for (int i = 0; i < bytes.Length; i += 2) {
     524        res[i / 2] = BitConverter.ToChar(bytes, i);
     525      }
     526      return res;
     527    }
    465528  }
    466529
     
    507570  }
    508571
    509   [Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)]
    510   internal sealed class GenericArrayTransformer : UnsignedIntArrayBoxTransformer<object> {
     572  //[Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)]
     573  //internal sealed class GenericArrayTransformer : UnsignedIntArrayBoxTransformer<object> {
     574  //  public override bool CanTransformType(Type type) {
     575  //    return type.IsArray;
     576  //  }
     577  //  protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
     578  //    var arr = (object[])value;
     579  //    return arr.Select(x => mapper.GetBoxId(x));
     580  //  }
     581  //  protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
     582  //    return value.Select(x => mapper.GetObject(x)).ToArray();
     583  //  }
     584  //}
     585
     586  //   internal sealed class QueueTransformer : Transformer {
     587  //     public override bool CanTransformType(Type type) {
     588  //       return type == typeof(System.Collections.Queue) || type == typeof(Queue<>);
     589  //     }
     590  //     public override Box ToBox(object o, Mapper mapper) {
     591  //       var box = Box.CreateBuilder();
     592  //       box.TransformerId = mapper.GetTransformerId(this);
     593  //       Populate(box, o, mapper);
     594  //       return box.Build();
     595  //     }
     596  //     public override object ToObject(Box box, Mapper mapper) {
     597  //       return Extract(box, mapper.GetType(box.TypeId), mapper);
     598  //     }
     599  //     private void Populate(Box.Builder box, object value, Mapper mapper) {
     600  //       var elements = (System.Collections.IEnumerable)value;
     601  //       if (value.GetType().IsGenericType) {
     602  //         var elementType = value.GetType().GetGenericTypeDefinition().GetGenericArguments().First();
     603  //         box.TypeId = mapper.GetTypeId(elementType);
     604  //       }
     605  //
     606  //       var b = UnsignedIntArrayBox.CreateBuilder();
     607  //       foreach (var elem in elements) {
     608  //         b.AddValues(mapper.GetBoxId(elem));
     609  //       }
     610  //
     611  //       box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
     612  //     }
     613  //     private object Extract(Box box, Type type, Mapper mapper) {
     614  //     }
     615  //   }
     616
     617  [Transformer("B01ADF0A-ACAA-444E-9F82-0C7C2AF1F490", 2000)]
     618  internal sealed class ArrayTransformer : Transformer {
    511619    public override bool CanTransformType(Type type) {
    512620      return type.IsArray;
    513621    }
    514     protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
    515       var arr = (object[])value;
    516       return arr.Select(x => mapper.GetBoxId(x));
    517     }
    518     protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
    519       return value.Select(x => mapper.GetObject(x)).ToArray();
     622    public override Box ToBox(object o, Mapper mapper) {
     623      var box = Box.CreateBuilder();
     624      box.TransformerId = mapper.GetTransformerId(this);
     625      Populate(box, o, mapper);
     626      return box.Build();
     627    }
     628    public override object ToObject(Box box, Mapper mapper) {
     629      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
     630    }
     631    private void Populate(Box.Builder box, object value, Mapper mapper) {
     632      var array = (object[])value;
     633      var elementType = value.GetType().GetElementType();
     634
     635      var b = UnsignedIntArrayBox.CreateBuilder();
     636      b.AddRangeValues(array.Select(x => mapper.GetBoxId(x)));
     637
     638      box.TypeId = mapper.GetBoxId(elementType);
     639      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
     640    }
     641    private object Extract(Box box, Type type, Mapper mapper) {
     642      var b = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
     643
     644      var array = (object[])Array.CreateInstance((Type)mapper.GetObject(box.TypeId), b.ValuesCount);
     645
     646      for (int i = 0; i < array.Length; i++) {
     647        array[i] = mapper.GetObject(b.GetValues(i));
     648      }
     649
     650      return array;
     651    }
     652  }
     653
     654  [Transformer("11B822C9-46A0-4B65-AE4A-D12F63DDE9F6", 1)]
     655  internal sealed class TypeTransformer : Transformer {
     656    public override bool CanTransformType(Type type) {
     657      return typeof(Type).IsAssignableFrom(type);
     658    }
     659
     660    public override Box ToBox(object o, Mapper mapper) {
     661      var box = Box.CreateBuilder();
     662      box.TransformerId = mapper.GetTransformerId(this);
     663      Populate(box, o, mapper);
     664      return box.Build();
     665    }
     666
     667    private void Populate(Box.Builder box, object o, Mapper mapper) {
     668      var type = (Type)o;
     669
     670      var b = TypeBox.CreateBuilder();
     671      if (type.IsGenericType) {
     672        box.TypeId = mapper.GetTypeId(type.GetGenericTypeDefinition());
     673        b.AddRangeGenericTypeIds(type.GetGenericArguments().Select(t => mapper.GetBoxId(t)));
     674      } else if (type.IsArray) {
     675        box.TypeId = mapper.GetTypeId(typeof(Array));
     676        b.AddGenericTypeIds(mapper.GetBoxId(type.GetElementType()));
     677      } else {
     678        box.TypeId = mapper.GetTypeId(type);
     679      }
     680      box.SetExtension(TypeBox.Type, b.Build());
     681    }
     682
     683    public override object ToObject(Box box, Mapper mapper) {
     684      return Extract(box, mapper.GetType(box.TypeId), mapper);
     685    }
     686
     687    private object Extract(Box box, Type type, Mapper mapper) {
     688      var b = box.GetExtension(TypeBox.Type);
     689      if (b.GenericTypeIdsCount > 0) {
     690        if (typeof(Array).IsAssignableFrom(type)) {
     691          return ((Type)mapper.GetObject(b.GetGenericTypeIds(0))).MakeArrayType();
     692        } else {
     693          return type.MakeGenericType(b.GenericTypeIdsList.Select(id => (Type)mapper.GetObject(id)).ToArray());
     694        }
     695      } else {
     696        return mapper.GetType(box.TypeId);
     697      }
     698    }
     699  }
     700
     701  [Transformer("4FA5EAAF-ECC7-4A9C-84E7-6583DA96F3B9", 1000)]
     702  internal sealed class EnumerableTransformer : Transformer {
     703    public override bool CanTransformType(Type type) {
     704      return (type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition()) ||
     705             type == typeof(System.Collections.Stack);
     706    }
     707    public override Box ToBox(object o, Mapper mapper) {
     708      var box = Box.CreateBuilder();
     709      box.TransformerId = mapper.GetTransformerId(this);
     710      Populate(box, o, mapper);
     711      return box.Build();
     712    }
     713    public override object ToObject(Box box, Mapper mapper) {
     714      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
     715    }
     716    private void Populate(Box.Builder box, object value, Mapper mapper) {
     717      var enumerableBox = UnsignedIntArrayBox.CreateBuilder();
     718      box.TypeId = mapper.GetBoxId(value.GetType());
     719      enumerableBox.AddRangeValues(((IEnumerable)value).OfType<object>().Select(mapper.GetBoxId));
     720
     721      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, enumerableBox.Build());
     722    }
     723    private object Extract(Box box, Type type, Mapper mapper) {
     724      var enumerableBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
     725      if ((type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition())) {
     726        var elemType = type.GenericTypeArguments[0];
     727        var stack = Activator.CreateInstance(type);
     728        var addMeth = type.GetMethod("Push");
     729        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject).Reverse())
     730          addMeth.Invoke(stack, new[] { e });
     731        return stack;
     732      } else if (typeof(Stack).IsAssignableFrom(type)) {
     733        var l = new System.Collections.ArrayList();
     734        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject)) {
     735          l.Add(e);
     736        }
     737        l.Reverse();
     738        return Activator.CreateInstance(type, l);
     739      } else {
     740        throw new NotSupportedException();
     741      }
     742    }
     743  }
     744
     745  [Transformer("C47A62F5-F113-4A43-A8EE-CF817EC799A2", 500)]
     746  internal sealed class DictionaryTransformer : Transformer {
     747    public override bool CanTransformType(Type type) {
     748      return type.IsGenericType && typeof(IDictionary<,>).IsAssignableFrom(type);
     749    }
     750    public override Box ToBox(object o, Mapper mapper) {
     751      var box = Box.CreateBuilder();
     752      box.TransformerId = mapper.GetTransformerId(this);
     753      Populate(box, o, mapper);
     754      return box.Build();
     755    }
     756    public override object ToObject(Box box, Mapper mapper) {
     757      return Extract(box, mapper.GetType(box.TypeId), mapper);
     758    }
     759    private void Populate(Box.Builder box, object value, Mapper mapper) {
     760      var dictionaryBox = DictionaryBox.CreateBuilder();
     761      foreach (DictionaryEntry item in (IDictionary)value) {
     762        dictionaryBox.AddKeyIds(mapper.GetBoxId(item.Key));
     763        dictionaryBox.AddValueIds(mapper.GetBoxId(item.Value));
     764      }
     765      // TODO: comparer
     766    }
     767    private object Extract(Box box, Type type, Mapper mapper) {
     768      throw new NotImplementedException();
    520769    }
    521770  }
  • branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r13410 r14537  
    273273  }
    274274
     275  [StorableClass("FD953B0A-BDE6-41E6-91A8-CA3D90C91CDB")]
    275276  public class SimpleClass {
     277    [Storable]
    276278    public double x { get; set; }
     279    [Storable]
    277280    public int y { get; set; }
    278281  }
     
    653656    [TestCategory("Persistence4")]
    654657    [TestProperty("Time", "short")]
     658    public void TestSBytes() {
     659      var test = new Func<sbyte[]>(() => { return new sbyte[] { 3, 1 }; });
     660      ProtoBufSerializer serializer = new ProtoBufSerializer();
     661      serializer.Serialize(test(), tempFile);
     662      object o = serializer.Deserialize(tempFile);
     663      sbyte[] result = (sbyte[])o;
     664      Assert.AreEqual(test()[0], result[0]);
     665      Assert.AreEqual(test()[1], result[1]);
     666
     667      string msg = Profile(test);
     668      Console.WriteLine(msg);
     669    }
     670
     671    [TestMethod]
     672    [TestCategory("Persistence4")]
     673    [TestProperty("Time", "short")]
     674    public void TestChars() {
     675      var test = new Func<char[]>(() => { return new char[] { 'a', 'b' }; });
     676      ProtoBufSerializer serializer = new ProtoBufSerializer();
     677      serializer.Serialize(test(), tempFile);
     678      object o = serializer.Deserialize(tempFile);
     679      char[] result = (char[])o;
     680      Assert.AreEqual(test()[0], result[0]);
     681      Assert.AreEqual(test()[1], result[1]);
     682
     683      string msg = Profile(test);
     684      Console.WriteLine(msg);
     685    }
     686
     687    [TestMethod]
     688    [TestCategory("Persistence4")]
     689    [TestProperty("Time", "short")]
     690    public void TestShorts() {
     691      var test = new Func<short[]>(() => { return new short[] { 3, 1 }; });
     692      ProtoBufSerializer serializer = new ProtoBufSerializer();
     693      serializer.Serialize(test(), tempFile);
     694      object o = serializer.Deserialize(tempFile);
     695      short[] result = (short[])o;
     696      Assert.AreEqual(test()[0], result[0]);
     697      Assert.AreEqual(test()[1], result[1]);
     698
     699      string msg = Profile(test);
     700      Console.WriteLine(msg);
     701    }
     702
     703    [TestMethod]
     704    [TestCategory("Persistence4")]
     705    [TestProperty("Time", "short")]
     706    public void TestUShorts() {
     707      var test = new Func<ushort[]>(() => { return new ushort[] { 3, 1 }; });
     708      ProtoBufSerializer serializer = new ProtoBufSerializer();
     709      serializer.Serialize(test(), tempFile);
     710      object o = serializer.Deserialize(tempFile);
     711      ushort[] result = (ushort[])o;
     712      Assert.AreEqual(test()[0], result[0]);
     713      Assert.AreEqual(test()[1], result[1]);
     714
     715      string msg = Profile(test);
     716      Console.WriteLine(msg);
     717    }
     718
     719    [TestMethod]
     720    [TestCategory("Persistence4")]
     721    [TestProperty("Time", "short")]
    655722    public void TestString() {
    656723      var test = new Func<object>(() => { return "Hello World!"; });
     
    669736    [TestProperty("Time", "short")]
    670737    public void TestColor() {
    671       var test = new Func<object>(() => { return Color.DeepSkyBlue; });
     738      var test = new Func<object>(() => { return Color.FromArgb(12, 34, 56, 78); });
    672739      ProtoBufSerializer serializer = new ProtoBufSerializer();
    673740      serializer.Serialize(test(), tempFile);
     
    791858    [TestCategory("Persistence4")]
    792859    [TestProperty("Time", "short")]
     860    public void TestStack() {
     861      var test = new Func<Stack>(() => {
     862        return new Stack(new int[] { 1, 2, 3 });
     863      });
     864      ProtoBufSerializer serializer = new ProtoBufSerializer();
     865      serializer.Serialize(test(), tempFile);
     866      object o = serializer.Deserialize(tempFile);
     867      Stack result = (Stack)o;
     868      var actualStack = test();
     869      Assert.AreEqual(actualStack.Pop(), result.Pop());
     870      Assert.AreEqual(actualStack.Pop(), result.Pop());
     871      Assert.AreEqual(actualStack.Pop(), result.Pop());
     872
     873      string msg = Profile(test);
     874      Console.WriteLine(msg);
     875    }
     876
     877    [TestMethod]
     878    [TestCategory("Persistence4")]
     879    [TestProperty("Time", "short")]
     880    public void TestArrayOfStack() {
     881      var test = new Func<object[]>(() => {
     882        return new object[] {
     883          new Stack(new int[] { 1, 2, 3 }),
     884          new Stack<int>(new int[] { 1, 2, 3 }),
     885      };
     886      });
     887      ProtoBufSerializer serializer = new ProtoBufSerializer();
     888      serializer.Serialize(test(), tempFile);
     889      object o = serializer.Deserialize(tempFile);
     890      var result = (object[])o;
     891      var firstStack = (Stack)result[0];
     892      var secondStack = (Stack<int>)result[1];
     893      var actual = test();
     894      var actualFirst = (Stack)actual[0];
     895      var actualSecond = (Stack<int>)actual[1];
     896
     897      Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
     898      Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
     899      Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
     900      Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
     901      Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
     902      Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
     903
     904      string msg = Profile(test);
     905      Console.WriteLine(msg);
     906    }
     907
     908
     909    [TestMethod]
     910    [TestCategory("Persistence4")]
     911    [TestProperty("Time", "short")]
    793912    public void TestIntValueArray() {
    794913      var test = new Func<IntValue[]>(() => { return new[] { new IntValue(41), new IntValue(22), new IntValue(13) }; });
     
    797916      object o = serializer.Deserialize(tempFile);
    798917      IntValue[] result = (IntValue[])o;
    799       Assert.AreEqual(test()[0], result[0]);
    800       Assert.AreEqual(test()[1], result[1]);
    801       Assert.AreEqual(test()[2], result[2]);
     918      Assert.AreEqual(test()[0].Value, result[0].Value);
     919      Assert.AreEqual(test()[1].Value, result[1].Value);
     920      Assert.AreEqual(test()[2].Value, result[2].Value);
     921
     922      string msg = Profile(test);
     923      Console.WriteLine(msg);
     924    }
     925
     926    [TestMethod]
     927    [TestCategory("Persistence4")]
     928    [TestProperty("Time", "short")]
     929    public void TestIntValueArrayArray() {
     930      var test = new Func<IntValue[][]>(() => { return new IntValue[][] { new IntValue[] { new IntValue(41), new IntValue(22), new IntValue(13) } }; });
     931      ProtoBufSerializer serializer = new ProtoBufSerializer();
     932      serializer.Serialize(test(), tempFile);
     933      object o = serializer.Deserialize(tempFile);
     934      IntValue[][] result = (IntValue[][])o;
     935      Assert.AreEqual(test()[0][0].Value, result[0][0].Value);
     936      Assert.AreEqual(test()[0][1].Value, result[0][1].Value);
     937      Assert.AreEqual(test()[0][2].Value, result[0][2].Value);
    802938
    803939      string msg = Profile(test);
     
    809945    #region Old persistence test methods
    810946    [TestMethod]
    811     [TestCategory("Persistence")]
     947    [TestCategory("Persistence4")]
    812948    [TestProperty("Time", "short")]
    813949    public void ComplexStorable() {
     
    8991035
    9001036    [TestMethod]
    901     [TestCategory("Persistence")]
     1037    [TestCategory("Persistence4")]
    9021038    [TestProperty("Time", "short")]
    9031039    public void SelfReferences() {
     
    9271063
    9281064    [TestMethod]
    929     [TestCategory("Persistence")]
     1065    [TestCategory("Persistence4")]
    9301066    [TestProperty("Time", "short")]
    9311067    public void ArrayCreation() {
     
    9571093
    9581094    [TestMethod]
    959     [TestCategory("Persistence")]
     1095    [TestCategory("Persistence4")]
    9601096    [TestProperty("Time", "short")]
    9611097    public void CustomSerializationProperty() {
     
    9741110
    9751111    [TestMethod]
    976     [TestCategory("Persistence")]
     1112    [TestCategory("Persistence4")]
    9771113    [TestProperty("Time", "short")]
    9781114    public void Primitives() {
     
    9871123
    9881124    [TestMethod]
    989     [TestCategory("Persistence")]
     1125    [TestCategory("Persistence4")]
    9901126    [TestProperty("Time", "short")]
    9911127    public void MultiDimensionalArray() {
     
    10181154
    10191155    [TestMethod]
    1020     [TestCategory("Persistence")]
     1156    [TestCategory("Persistence4")]
    10211157    [TestProperty("Time", "short")]
    10221158    public void NestedTypeTest() {
     
    10331169
    10341170    [TestMethod]
    1035     [TestCategory("Persistence")]
     1171    [TestCategory("Persistence4")]
    10361172    [TestProperty("Time", "short")]
    10371173    public void SimpleArray() {
     
    10461182
    10471183    [TestMethod]
    1048     [TestCategory("Persistence")]
     1184    [TestCategory("Persistence4")]
    10491185    [TestProperty("Time", "short")]
    10501186    public void PrimitiveRoot() {
     
    11011237
    11021238    [TestMethod]
    1103     [TestCategory("Persistence")]
     1239    [TestCategory("Persistence4")]
    11041240    [TestProperty("Time", "short")]
    11051241    public void Enums() {
     
    11171253
    11181254    [TestMethod]
    1119     [TestCategory("Persistence")]
     1255    [TestCategory("Persistence4")]
    11201256    [TestProperty("Time", "short")]
    11211257    public void TestAliasingWithOverriddenEquals() {
     
    11351271
    11361272    [TestMethod]
    1137     [TestCategory("Persistence")]
     1273    [TestCategory("Persistence4")]
    11381274    [TestProperty("Time", "short")]
    11391275    public void NonDefaultConstructorTest() {
     
    11431279        serializer.Serialize(c, tempFile);
    11441280        Assert.Fail("Exception not thrown");
    1145       }
    1146       catch (PersistenceException) {
    1147       }
    1148     }
    1149 
    1150     [TestMethod]
    1151     [TestCategory("Persistence")]
     1281      } catch (PersistenceException) {
     1282      }
     1283    }
     1284
     1285    [TestMethod]
     1286    [TestCategory("Persistence4")]
    11521287    [TestProperty("Time", "short")]
    11531288    public void TestSavingException() {
     
    11591294        serializer.Serialize(s, tempFile);
    11601295        Assert.Fail("Exception expected");
    1161       }
    1162       catch (PersistenceException) { }
     1296      } catch (PersistenceException) { }
    11631297      List<int> newList = (List<int>)serializer.Deserialize(tempFile);
    11641298      Assert.AreEqual(list[0], newList[0]);
     
    11671301
    11681302    [TestMethod]
    1169     [TestCategory("Persistence")]
     1303    [TestCategory("Persistence4")]
    11701304    [TestProperty("Time", "short")]
    11711305    public void TestTypeStringConversion() {
     
    11791313
    11801314    [TestMethod]
    1181     [TestCategory("Persistence")]
     1315    [TestCategory("Persistence4")]
    11821316    [TestProperty("Time", "short")]
    11831317    public void TestHexadecimalPublicKeyToken() {
     
    11891323
    11901324    [TestMethod]
    1191     [TestCategory("Persistence")]
     1325    [TestCategory("Persistence4")]
    11921326    [TestProperty("Time", "short")]
    11931327    public void InheritanceTest() {
     
    12191353
    12201354    [TestMethod]
    1221     [TestCategory("Persistence")]
     1355    [TestCategory("Persistence4")]
    12221356    [TestProperty("Time", "short")]
    12231357    public void InstantiateParentChainReference() {
     
    12441378
    12451379    [TestMethod]
    1246     [TestCategory("Persistence")]
     1380    [TestCategory("Persistence4")]
    12471381    [TestProperty("Time", "short")]
    12481382    public void StructTest() {
     
    12551389
    12561390    [TestMethod]
    1257     [TestCategory("Persistence")]
     1391    [TestCategory("Persistence4")]
    12581392    [TestProperty("Time", "short")]
    12591393    public void PointTest() {
     
    12661400
    12671401    [TestMethod]
    1268     [TestCategory("Persistence")]
     1402    [TestCategory("Persistence4")]
    12691403    [TestProperty("Time", "short")]
    12701404    public void NullableValueTypes() {
     
    12801414
    12811415    [TestMethod]
    1282     [TestCategory("Persistence")]
     1416    [TestCategory("Persistence4")]
    12831417    [TestProperty("Time", "short")]
    12841418    public void BitmapTest() {
     
    13141448
    13151449    [TestMethod]
    1316     [TestCategory("Persistence")]
     1450    [TestCategory("Persistence4")]
    13171451    [TestProperty("Time", "short")]
    13181452    public void HookTest() {
     
    13471481
    13481482    [TestMethod]
    1349     [TestCategory("Persistence")]
     1483    [TestCategory("Persistence4")]
    13501484    [TestProperty("Time", "short")]
    13511485    public void TestCustomConstructor() {
     
    13681502
    13691503    [TestMethod]
    1370     [TestCategory("Persistence")]
     1504    [TestCategory("Persistence4")]
    13711505    [TestProperty("Time", "short")]
    13721506    public void TestConstructorExceptionUnwrapping() {
     
    13771511        ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)serializer.Deserialize(tempFile);
    13781512        Assert.Fail("Exception expected");
    1379       }
    1380       catch (PersistenceException pe) {
     1513      } catch (PersistenceException pe) {
    13811514        Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail");
    13821515      }
     
    13841517
    13851518    [TestMethod]
    1386     [TestCategory("Persistence")]
    1387     [TestProperty("Time", "short")]
    1388     public void TestRejectionJustifications() {
    1389       ProtoBufSerializer serializer = new ProtoBufSerializer();
    1390       NonSerializable ns = new NonSerializable();
    1391       try {
    1392         serializer.Serialize(ns, tempFile);
    1393         Assert.Fail("PersistenceException expected");
    1394       }
    1395       catch (PersistenceException x) {
    1396         Assert.IsTrue(x.Message.Contains(new StorableSerializer().JustifyRejection(typeof(NonSerializable))));
    1397       }
    1398     }
    1399 
    1400     [TestMethod]
    1401     [TestCategory("Persistence")]
     1519    [TestCategory("Persistence4")]
    14021520    [TestProperty("Time", "short")]
    14031521    public void TestStreaming() {
     
    14361554
    14371555    [TestMethod]
    1438     [TestCategory("Persistence")]
     1556    [TestCategory("Persistence4")]
    14391557    [TestProperty("Time", "short")]
    14401558    public void TestLinkInheritance() {
     
    14661584
    14671585    [TestMethod]
    1468     [TestCategory("Persistence")]
     1586    [TestCategory("Persistence4")]
    14691587    [TestProperty("Time", "short")]
    14701588    public void TestStorableClassDiscoveryAllFields() {
     
    14981616
    14991617    [TestMethod]
    1500     [TestCategory("Persistence")]
     1618    [TestCategory("Persistence4")]
    15011619    [TestProperty("Time", "short")]
    15021620    public void TestStorableClassDiscoveryAllProperties() {
     
    15311649
    15321650    [TestMethod]
    1533     [TestCategory("Persistence")]
     1651    [TestCategory("Persistence4")]
    15341652    [TestProperty("Time", "short")]
    15351653    public void TestStorableClassDiscoveryAllFieldsAndAllProperties() {
     
    15631681
    15641682    [TestMethod]
    1565     [TestCategory("Persistence")]
     1683    [TestCategory("Persistence4")]
    15661684    [TestProperty("Time", "short")]
    15671685    public void TestStorableClassDiscoveryMarkedOnly() {
     
    15771695
    15781696    [TestMethod]
    1579     [TestCategory("Persistence")]
     1697    [TestCategory("Persistence4")]
    15801698    [TestProperty("Time", "short")]
    15811699    public void TestLineEndings() {
     
    15941712
    15951713    [TestMethod]
    1596     [TestCategory("Persistence")]
     1714    [TestCategory("Persistence4")]
    15971715    [TestProperty("Time", "short")]
    15981716    public void TestSpecialNumbers() {
     
    16101728
    16111729    [TestMethod]
    1612     [TestCategory("Persistence")]
     1730    [TestCategory("Persistence4")]
    16131731    [TestProperty("Time", "short")]
    16141732    public void TestStringSplit() {
     
    16331751
    16341752    [TestMethod]
    1635     [TestCategory("Persistence")]
     1753    [TestCategory("Persistence4")]
    16361754    [TestProperty("Time", "short")]
    16371755    public void TestHashSetSerializer() {
     
    16541772
    16551773    [TestMethod]
    1656     [TestCategory("Persistence")]
     1774    [TestCategory("Persistence4")]
    16571775    [TestProperty("Time", "short")]
    16581776    public void TestConcreteDictionarySerializer() {
     
    16951813
    16961814    [TestMethod]
    1697     [TestCategory("Persistence")]
     1815    [TestCategory("Persistence4")]
    16981816    [TestProperty("Time", "short")]
    16991817    public void TestReadOnlyFail() {
     
    17021820        serializer.Serialize(new ReadOnlyFail(), tempFile);
    17031821        Assert.Fail("Exception expected");
    1704       }
    1705       catch (PersistenceException) {
    1706       }
    1707       catch {
     1822      } catch (PersistenceException) {
     1823      } catch {
    17081824        Assert.Fail("PersistenceException expected");
    17091825      }
     
    17201836
    17211837    [TestMethod]
    1722     [TestCategory("Persistence")]
     1838    [TestCategory("Persistence4")]
    17231839    [TestProperty("Time", "short")]
    17241840    public void TestWriteOnlyFail() {
     
    17271843        serializer.Serialize(new WriteOnlyFail(), tempFile);
    17281844        Assert.Fail("Exception expected");
    1729       }
    1730       catch (PersistenceException) {
    1731       }
    1732       catch {
     1845      } catch (PersistenceException) {
     1846      } catch {
    17331847        Assert.Fail("PersistenceException expected.");
    17341848      }
     
    17511865    //TODO
    17521866    /* [TestMethod]
    1753      [TestCategory("Persistence")]
     1867     [TestCategory("Persistence4")]
    17541868     [TestProperty("Time", "short")]
    17551869     public void TestTypeCacheExport() {
     
    17691883
    17701884    [TestMethod]
    1771     [TestCategory("Persistence")]
     1885    [TestCategory("Persistence4")]
    17721886    [TestProperty("Time", "short")]
    17731887    public void TupleTest() {
     
    17771891      var t4 = Tuple.Create(Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6), Tuple.Create(8, 9, 10));
    17781892      var tuple = Tuple.Create(t1, t2, t3, t4);
    1779       XmlGenerator.Serialize(tuple, tempFile);
    1780       var newTuple = XmlParser.Deserialize<Tuple<Tuple<int>, Tuple<char, string>, Tuple<double, float, int>, Tuple<Tuple<int, int, int>, Tuple<int, int, int>, Tuple<int, int, int>>>>(tempFile);
     1893      SerializeNew(tuple);
     1894      var newTuple = DeserializeNew();
    17811895      Assert.AreEqual(tuple, newTuple);
    17821896    }
    17831897
    17841898    [TestMethod]
    1785     [TestCategory("Persistence")]
     1899    [TestCategory("Persistence4")]
    17861900    [TestProperty("Time", "short")]
    17871901    public void FontTest() {
     
    18021916
    18031917    [TestMethod]
    1804     [TestCategory("Persistence")]
     1918    [TestCategory("Persistence4")]
    18051919    [TestProperty("Time", "medium")]
    18061920    public void ConcurrencyTest() {
     
    18211935
    18221936    [TestMethod]
    1823     [TestCategory("Persistence")]
     1937    [TestCategory("Persistence4")]
    18241938    [TestProperty("Time", "medium")]
    18251939    public void ConcurrentBitmapTest() {
     
    18521966
    18531967    [TestMethod]
    1854     [TestCategory("Persistence")]
     1968    [TestCategory("Persistence4")]
    18551969    [TestProperty("Time", "short")]
    18561970    public void TestSpecialCharacters() {
     
    18631977
    18641978    [TestMethod]
    1865     [TestCategory("Persistence")]
     1979    [TestCategory("Persistence4")]
    18661980    [TestProperty("Time", "short")]
    18671981    public void TestByteArray() {
     
    18771991
    18781992    [TestMethod]
    1879     [TestCategory("Persistence")]
     1993    [TestCategory("Persistence4")]
    18801994    [TestProperty("Time", "short")]
    18811995    public void TestOptionalNumberEnumerable() {
     
    18882002
    18892003    [TestMethod]
    1890     [TestCategory("Persistence")]
     2004    [TestCategory("Persistence4")]
    18912005    [TestProperty("Time", "short")]
    18922006    public void TestOptionalDateTimeEnumerable() {
     
    19002014
    19012015    [TestMethod]
    1902     [TestCategory("Persistence")]
     2016    [TestCategory("Persistence4")]
    19032017    [TestProperty("Time", "short")]
    19042018    public void TestStringEnumerable() {
     
    19112025
    19122026    [TestMethod]
    1913     [TestCategory("Persistence")]
     2027    [TestCategory("Persistence4")]
    19142028    [TestProperty("Time", "short")]
    19152029    public void TestUnicodeCharArray() {
     
    19222036
    19232037    [TestMethod]
    1924     [TestCategory("Persistence")]
     2038    [TestCategory("Persistence4")]
    19252039    [TestProperty("Time", "short")]
    19262040    public void TestUnicode() {
     
    19332047
    19342048    [TestMethod]
    1935     [TestCategory("Persistence")]
     2049    [TestCategory("Persistence4")]
    19362050    [TestProperty("Time", "short")]
    19372051    public void TestQueue() {
Note: See TracChangeset for help on using the changeset viewer.