Changeset 14537
- Timestamp:
- 01/03/17 18:21:40 (8 years ago)
- Location:
- branches/PersistenceOverhaul
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:ignore
-
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r13358 r14537 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.PluginInfrastructure;26 25 using Google.ProtocolBuffers; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;28 26 29 27 namespace HeuristicLab.Persistence { … … 122 120 bundle.RootBoxId = mapper.GetBoxId(o); 123 121 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))); 125 123 bundle.AddRangeStrings(mapper.strings.GetValues()); 126 124 bundle.AddRangeBoxes(mapper.boxes.GetValues()); … … 129 127 public static object ToObject(Bundle bundle) { 130 128 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))); 132 130 mapper.strings = new Index<string>(bundle.StringsList); 133 131 mapper.boxes = new Index<Box>(bundle.BoxesList); -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r13407 r14537 34 34 private Dictionary<Guid, ITransformer> guid2Transformer; 35 35 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; 38 38 private Dictionary<Type, TypeInfo> typeInfos; 39 39 private ExtensionRegistry extensionRegistry; … … 42 42 guid2Transformer = new Dictionary<Guid, ITransformer>(); 43 43 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>(); 46 46 typeInfos = new Dictionary<Type, TypeInfo>(); 47 47 extensionRegistry = ExtensionRegistry.CreateInstance(); 48 48 49 foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>() .OrderBy(x => x.Priority))49 foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>()) 50 50 RegisterTransformer(transformer); 51 51 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 84 146 foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { 85 147 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); 87 154 } 88 155 … … 97 164 RegisterExtension(BytesBox.Bytes); 98 165 RegisterExtension(BoolArrayBox.BoolArray); 166 RegisterExtension(ByteArrayBox.ByteArray); 99 167 RegisterExtension(IntArrayBox.IntArray); 100 168 RegisterExtension(LongArrayBox.LongArray); … … 108 176 RegisterExtension(IntMatrixBox.IntMatrix); 109 177 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 } 110 191 } 111 192 … … 116 197 } 117 198 } 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 } 122 212 } 123 213 } … … 134 224 return transformer2Guid[transformer]; 135 225 } 136 public Type GetType( Guidguid) {226 public Type GetType(string guid) { 137 227 return guid2Type[guid]; 138 228 } 139 public GuidGetGuid(Type type) {229 public string GetGuid(Type type) { 140 230 return type2Guid[type]; 141 231 } … … 144 234 TypeInfo typeInfo; 145 235 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)); 147 237 typeInfo = new TypeInfo(type, transformer); 148 238 typeInfos.Add(type, typeInfo); -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/HeuristicLab.Persistence-4.0.csproj
r13358 r14537 62 62 <Compile Include="Properties\AssemblyInfo.cs" /> 63 63 <Compile Include="Protos\PersistenceMessages.cs" /> 64 <Compile Include="Transformers\StorableClassTransformer.cs" /> 64 65 <Compile Include="Transformers\Transformers.cs" /> 65 66 </ItemGroup> -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto
r13375 r14537 15 15 extensions 100 to max; 16 16 } 17 17 18 18 19 // scalar boxes … … 79 80 repeated bool values = 1 [packed = true]; 80 81 } 82 message ByteArrayBox { 83 extend Box { 84 required ByteArrayBox byte_array = 121; 85 } 86 required bytes values = 1; 87 } 81 88 message IntArrayBox { 82 89 extend Box { 83 required IntArrayBox int_array = 12 1;90 required IntArrayBox int_array = 122; 84 91 } 85 92 repeated int32 values = 1 [packed = true]; … … 87 94 message LongArrayBox { 88 95 extend Box { 89 required LongArrayBox long_array = 12 2;96 required LongArrayBox long_array = 123; 90 97 } 91 98 repeated int64 values = 1 [packed = true]; … … 93 100 message UnsignedIntArrayBox { 94 101 extend Box { 95 required UnsignedIntArrayBox unsigned_int_array = 12 3;102 required UnsignedIntArrayBox unsigned_int_array = 124; 96 103 } 97 104 repeated uint32 values = 1 [packed = true]; 105 106 extensions 100 to max; 98 107 } 99 108 message UnsignedLongArrayBox { 100 109 extend Box { 101 required UnsignedLongArrayBox unsigned_long_array = 12 4;110 required UnsignedLongArrayBox unsigned_long_array = 125; 102 111 } 103 112 repeated uint64 values = 1 [packed = true]; … … 105 114 message FloatArrayBox { 106 115 extend Box { 107 required FloatArrayBox float_array = 12 5;116 required FloatArrayBox float_array = 126; 108 117 } 109 118 repeated float values = 1 [packed = true]; … … 111 120 message DoubleArrayBox { 112 121 extend Box { 113 required DoubleArrayBox double_array = 12 6;122 required DoubleArrayBox double_array = 127; 114 123 } 115 124 repeated double values = 1 [packed = true]; … … 117 126 message StringArrayBox { 118 127 extend Box { 119 required StringArrayBox string_array = 12 7;128 required StringArrayBox string_array = 128; 120 129 } 121 130 repeated string values = 1; … … 154 163 required uint32 comparer_id = 3; 155 164 } 165 166 message 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 175 message 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 24 24 using System.Linq; 25 25 using System.Reflection; 26 using HeuristicLab.Persistence.Data;27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 27 29 28 namespace HeuristicLab.Persistence { 30 29 [Transformer("78556C88-0FEE-4602-95C7-A469B2DDB468", 100)] 31 internal sealed class StorableClass Transformer : TransformerBase{30 internal sealed class StorableClassBoxTransformer : BoxTransformer<object> { 32 31 public override bool CanTransformType(Type type) { 33 32 return StorableClassAttribute.IsStorableClass(type); 34 33 } 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); 44 39 45 40 var emptyArgs = new object[0]; 46 41 foreach (var hook in typeInfo.BeforeSerializationHooks) { 47 hook.Invoke( o, emptyArgs);42 hook.Invoke(value, emptyArgs); 48 43 } 44 49 45 var components = new Dictionary<uint, uint>(); 50 46 foreach (var componentInfo in typeInfo.Fields) { 51 47 var field = (FieldInfo)componentInfo.MemberInfo; 52 var component = mapper.Get DataId(field.GetValue(o));48 var component = mapper.GetBoxId(field.GetValue(value)); 53 49 components.Add(mapper.GetStringId(componentInfo.Name), component); 54 50 } 51 55 52 foreach (var componentInfo in typeInfo.Properties.Where(x => x.Readable)) { 56 53 var property = (PropertyInfo)componentInfo.MemberInfo; 57 var component = mapper.Get DataId(property.GetValue(o, null));54 var component = mapper.GetBoxId(property.GetValue(value, null)); 58 55 components.Add(mapper.GetStringId(componentInfo.Name), component); 59 56 } 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()); 63 62 } 64 63 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); 69 67 var o = mapper.CreateInstance(type); 70 mapper.Cache(data, o);71 68 72 69 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]); 75 72 } 76 73 … … 78 75 var field = (FieldInfo)componentInfo.MemberInfo; 79 76 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); 82 79 } 80 83 81 foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) { 84 82 var property = (PropertyInfo)componentInfo.MemberInfo; 85 83 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); 88 86 } 87 89 88 var emptyArgs = new object[0]; 90 89 foreach (var hook in typeInfo.AfterDeserializationHooks) { 91 90 hook.Invoke(o, emptyArgs); 92 91 } 92 93 93 return o; 94 94 } -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r13407 r14537 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using System.Drawing; … … 158 159 159 160 #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 } 160 173 public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> { 161 174 protected override void Populate(Box.Builder box, T value, Mapper mapper) { … … 407 420 #endregion 408 421 409 #region Type410 [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 #endregion422 // #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 429 442 430 443 #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 431 464 [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)] 432 465 internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> { … … 435 468 } 436 469 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 449 470 [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 } 453 488 } 454 489 455 490 [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 } 459 508 } 460 509 461 510 [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 } 465 528 } 466 529 … … 507 570 } 508 571 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 { 511 619 public override bool CanTransformType(Type type) { 512 620 return type.IsArray; 513 621 } 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(); 520 769 } 521 770 } -
branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r13410 r14537 273 273 } 274 274 275 [StorableClass("FD953B0A-BDE6-41E6-91A8-CA3D90C91CDB")] 275 276 public class SimpleClass { 277 [Storable] 276 278 public double x { get; set; } 279 [Storable] 277 280 public int y { get; set; } 278 281 } … … 653 656 [TestCategory("Persistence4")] 654 657 [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")] 655 722 public void TestString() { 656 723 var test = new Func<object>(() => { return "Hello World!"; }); … … 669 736 [TestProperty("Time", "short")] 670 737 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); }); 672 739 ProtoBufSerializer serializer = new ProtoBufSerializer(); 673 740 serializer.Serialize(test(), tempFile); … … 791 858 [TestCategory("Persistence4")] 792 859 [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")] 793 912 public void TestIntValueArray() { 794 913 var test = new Func<IntValue[]>(() => { return new[] { new IntValue(41), new IntValue(22), new IntValue(13) }; }); … … 797 916 object o = serializer.Deserialize(tempFile); 798 917 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); 802 938 803 939 string msg = Profile(test); … … 809 945 #region Old persistence test methods 810 946 [TestMethod] 811 [TestCategory("Persistence ")]947 [TestCategory("Persistence4")] 812 948 [TestProperty("Time", "short")] 813 949 public void ComplexStorable() { … … 899 1035 900 1036 [TestMethod] 901 [TestCategory("Persistence ")]1037 [TestCategory("Persistence4")] 902 1038 [TestProperty("Time", "short")] 903 1039 public void SelfReferences() { … … 927 1063 928 1064 [TestMethod] 929 [TestCategory("Persistence ")]1065 [TestCategory("Persistence4")] 930 1066 [TestProperty("Time", "short")] 931 1067 public void ArrayCreation() { … … 957 1093 958 1094 [TestMethod] 959 [TestCategory("Persistence ")]1095 [TestCategory("Persistence4")] 960 1096 [TestProperty("Time", "short")] 961 1097 public void CustomSerializationProperty() { … … 974 1110 975 1111 [TestMethod] 976 [TestCategory("Persistence ")]1112 [TestCategory("Persistence4")] 977 1113 [TestProperty("Time", "short")] 978 1114 public void Primitives() { … … 987 1123 988 1124 [TestMethod] 989 [TestCategory("Persistence ")]1125 [TestCategory("Persistence4")] 990 1126 [TestProperty("Time", "short")] 991 1127 public void MultiDimensionalArray() { … … 1018 1154 1019 1155 [TestMethod] 1020 [TestCategory("Persistence ")]1156 [TestCategory("Persistence4")] 1021 1157 [TestProperty("Time", "short")] 1022 1158 public void NestedTypeTest() { … … 1033 1169 1034 1170 [TestMethod] 1035 [TestCategory("Persistence ")]1171 [TestCategory("Persistence4")] 1036 1172 [TestProperty("Time", "short")] 1037 1173 public void SimpleArray() { … … 1046 1182 1047 1183 [TestMethod] 1048 [TestCategory("Persistence ")]1184 [TestCategory("Persistence4")] 1049 1185 [TestProperty("Time", "short")] 1050 1186 public void PrimitiveRoot() { … … 1101 1237 1102 1238 [TestMethod] 1103 [TestCategory("Persistence ")]1239 [TestCategory("Persistence4")] 1104 1240 [TestProperty("Time", "short")] 1105 1241 public void Enums() { … … 1117 1253 1118 1254 [TestMethod] 1119 [TestCategory("Persistence ")]1255 [TestCategory("Persistence4")] 1120 1256 [TestProperty("Time", "short")] 1121 1257 public void TestAliasingWithOverriddenEquals() { … … 1135 1271 1136 1272 [TestMethod] 1137 [TestCategory("Persistence ")]1273 [TestCategory("Persistence4")] 1138 1274 [TestProperty("Time", "short")] 1139 1275 public void NonDefaultConstructorTest() { … … 1143 1279 serializer.Serialize(c, tempFile); 1144 1280 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")] 1152 1287 [TestProperty("Time", "short")] 1153 1288 public void TestSavingException() { … … 1159 1294 serializer.Serialize(s, tempFile); 1160 1295 Assert.Fail("Exception expected"); 1161 } 1162 catch (PersistenceException) { } 1296 } catch (PersistenceException) { } 1163 1297 List<int> newList = (List<int>)serializer.Deserialize(tempFile); 1164 1298 Assert.AreEqual(list[0], newList[0]); … … 1167 1301 1168 1302 [TestMethod] 1169 [TestCategory("Persistence ")]1303 [TestCategory("Persistence4")] 1170 1304 [TestProperty("Time", "short")] 1171 1305 public void TestTypeStringConversion() { … … 1179 1313 1180 1314 [TestMethod] 1181 [TestCategory("Persistence ")]1315 [TestCategory("Persistence4")] 1182 1316 [TestProperty("Time", "short")] 1183 1317 public void TestHexadecimalPublicKeyToken() { … … 1189 1323 1190 1324 [TestMethod] 1191 [TestCategory("Persistence ")]1325 [TestCategory("Persistence4")] 1192 1326 [TestProperty("Time", "short")] 1193 1327 public void InheritanceTest() { … … 1219 1353 1220 1354 [TestMethod] 1221 [TestCategory("Persistence ")]1355 [TestCategory("Persistence4")] 1222 1356 [TestProperty("Time", "short")] 1223 1357 public void InstantiateParentChainReference() { … … 1244 1378 1245 1379 [TestMethod] 1246 [TestCategory("Persistence ")]1380 [TestCategory("Persistence4")] 1247 1381 [TestProperty("Time", "short")] 1248 1382 public void StructTest() { … … 1255 1389 1256 1390 [TestMethod] 1257 [TestCategory("Persistence ")]1391 [TestCategory("Persistence4")] 1258 1392 [TestProperty("Time", "short")] 1259 1393 public void PointTest() { … … 1266 1400 1267 1401 [TestMethod] 1268 [TestCategory("Persistence ")]1402 [TestCategory("Persistence4")] 1269 1403 [TestProperty("Time", "short")] 1270 1404 public void NullableValueTypes() { … … 1280 1414 1281 1415 [TestMethod] 1282 [TestCategory("Persistence ")]1416 [TestCategory("Persistence4")] 1283 1417 [TestProperty("Time", "short")] 1284 1418 public void BitmapTest() { … … 1314 1448 1315 1449 [TestMethod] 1316 [TestCategory("Persistence ")]1450 [TestCategory("Persistence4")] 1317 1451 [TestProperty("Time", "short")] 1318 1452 public void HookTest() { … … 1347 1481 1348 1482 [TestMethod] 1349 [TestCategory("Persistence ")]1483 [TestCategory("Persistence4")] 1350 1484 [TestProperty("Time", "short")] 1351 1485 public void TestCustomConstructor() { … … 1368 1502 1369 1503 [TestMethod] 1370 [TestCategory("Persistence ")]1504 [TestCategory("Persistence4")] 1371 1505 [TestProperty("Time", "short")] 1372 1506 public void TestConstructorExceptionUnwrapping() { … … 1377 1511 ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)serializer.Deserialize(tempFile); 1378 1512 Assert.Fail("Exception expected"); 1379 } 1380 catch (PersistenceException pe) { 1513 } catch (PersistenceException pe) { 1381 1514 Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail"); 1382 1515 } … … 1384 1517 1385 1518 [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")] 1402 1520 [TestProperty("Time", "short")] 1403 1521 public void TestStreaming() { … … 1436 1554 1437 1555 [TestMethod] 1438 [TestCategory("Persistence ")]1556 [TestCategory("Persistence4")] 1439 1557 [TestProperty("Time", "short")] 1440 1558 public void TestLinkInheritance() { … … 1466 1584 1467 1585 [TestMethod] 1468 [TestCategory("Persistence ")]1586 [TestCategory("Persistence4")] 1469 1587 [TestProperty("Time", "short")] 1470 1588 public void TestStorableClassDiscoveryAllFields() { … … 1498 1616 1499 1617 [TestMethod] 1500 [TestCategory("Persistence ")]1618 [TestCategory("Persistence4")] 1501 1619 [TestProperty("Time", "short")] 1502 1620 public void TestStorableClassDiscoveryAllProperties() { … … 1531 1649 1532 1650 [TestMethod] 1533 [TestCategory("Persistence ")]1651 [TestCategory("Persistence4")] 1534 1652 [TestProperty("Time", "short")] 1535 1653 public void TestStorableClassDiscoveryAllFieldsAndAllProperties() { … … 1563 1681 1564 1682 [TestMethod] 1565 [TestCategory("Persistence ")]1683 [TestCategory("Persistence4")] 1566 1684 [TestProperty("Time", "short")] 1567 1685 public void TestStorableClassDiscoveryMarkedOnly() { … … 1577 1695 1578 1696 [TestMethod] 1579 [TestCategory("Persistence ")]1697 [TestCategory("Persistence4")] 1580 1698 [TestProperty("Time", "short")] 1581 1699 public void TestLineEndings() { … … 1594 1712 1595 1713 [TestMethod] 1596 [TestCategory("Persistence ")]1714 [TestCategory("Persistence4")] 1597 1715 [TestProperty("Time", "short")] 1598 1716 public void TestSpecialNumbers() { … … 1610 1728 1611 1729 [TestMethod] 1612 [TestCategory("Persistence ")]1730 [TestCategory("Persistence4")] 1613 1731 [TestProperty("Time", "short")] 1614 1732 public void TestStringSplit() { … … 1633 1751 1634 1752 [TestMethod] 1635 [TestCategory("Persistence ")]1753 [TestCategory("Persistence4")] 1636 1754 [TestProperty("Time", "short")] 1637 1755 public void TestHashSetSerializer() { … … 1654 1772 1655 1773 [TestMethod] 1656 [TestCategory("Persistence ")]1774 [TestCategory("Persistence4")] 1657 1775 [TestProperty("Time", "short")] 1658 1776 public void TestConcreteDictionarySerializer() { … … 1695 1813 1696 1814 [TestMethod] 1697 [TestCategory("Persistence ")]1815 [TestCategory("Persistence4")] 1698 1816 [TestProperty("Time", "short")] 1699 1817 public void TestReadOnlyFail() { … … 1702 1820 serializer.Serialize(new ReadOnlyFail(), tempFile); 1703 1821 Assert.Fail("Exception expected"); 1704 } 1705 catch (PersistenceException) { 1706 } 1707 catch { 1822 } catch (PersistenceException) { 1823 } catch { 1708 1824 Assert.Fail("PersistenceException expected"); 1709 1825 } … … 1720 1836 1721 1837 [TestMethod] 1722 [TestCategory("Persistence ")]1838 [TestCategory("Persistence4")] 1723 1839 [TestProperty("Time", "short")] 1724 1840 public void TestWriteOnlyFail() { … … 1727 1843 serializer.Serialize(new WriteOnlyFail(), tempFile); 1728 1844 Assert.Fail("Exception expected"); 1729 } 1730 catch (PersistenceException) { 1731 } 1732 catch { 1845 } catch (PersistenceException) { 1846 } catch { 1733 1847 Assert.Fail("PersistenceException expected."); 1734 1848 } … … 1751 1865 //TODO 1752 1866 /* [TestMethod] 1753 [TestCategory("Persistence ")]1867 [TestCategory("Persistence4")] 1754 1868 [TestProperty("Time", "short")] 1755 1869 public void TestTypeCacheExport() { … … 1769 1883 1770 1884 [TestMethod] 1771 [TestCategory("Persistence ")]1885 [TestCategory("Persistence4")] 1772 1886 [TestProperty("Time", "short")] 1773 1887 public void TupleTest() { … … 1777 1891 var t4 = Tuple.Create(Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6), Tuple.Create(8, 9, 10)); 1778 1892 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(); 1781 1895 Assert.AreEqual(tuple, newTuple); 1782 1896 } 1783 1897 1784 1898 [TestMethod] 1785 [TestCategory("Persistence ")]1899 [TestCategory("Persistence4")] 1786 1900 [TestProperty("Time", "short")] 1787 1901 public void FontTest() { … … 1802 1916 1803 1917 [TestMethod] 1804 [TestCategory("Persistence ")]1918 [TestCategory("Persistence4")] 1805 1919 [TestProperty("Time", "medium")] 1806 1920 public void ConcurrencyTest() { … … 1821 1935 1822 1936 [TestMethod] 1823 [TestCategory("Persistence ")]1937 [TestCategory("Persistence4")] 1824 1938 [TestProperty("Time", "medium")] 1825 1939 public void ConcurrentBitmapTest() { … … 1852 1966 1853 1967 [TestMethod] 1854 [TestCategory("Persistence ")]1968 [TestCategory("Persistence4")] 1855 1969 [TestProperty("Time", "short")] 1856 1970 public void TestSpecialCharacters() { … … 1863 1977 1864 1978 [TestMethod] 1865 [TestCategory("Persistence ")]1979 [TestCategory("Persistence4")] 1866 1980 [TestProperty("Time", "short")] 1867 1981 public void TestByteArray() { … … 1877 1991 1878 1992 [TestMethod] 1879 [TestCategory("Persistence ")]1993 [TestCategory("Persistence4")] 1880 1994 [TestProperty("Time", "short")] 1881 1995 public void TestOptionalNumberEnumerable() { … … 1888 2002 1889 2003 [TestMethod] 1890 [TestCategory("Persistence ")]2004 [TestCategory("Persistence4")] 1891 2005 [TestProperty("Time", "short")] 1892 2006 public void TestOptionalDateTimeEnumerable() { … … 1900 2014 1901 2015 [TestMethod] 1902 [TestCategory("Persistence ")]2016 [TestCategory("Persistence4")] 1903 2017 [TestProperty("Time", "short")] 1904 2018 public void TestStringEnumerable() { … … 1911 2025 1912 2026 [TestMethod] 1913 [TestCategory("Persistence ")]2027 [TestCategory("Persistence4")] 1914 2028 [TestProperty("Time", "short")] 1915 2029 public void TestUnicodeCharArray() { … … 1922 2036 1923 2037 [TestMethod] 1924 [TestCategory("Persistence ")]2038 [TestCategory("Persistence4")] 1925 2039 [TestProperty("Time", "short")] 1926 2040 public void TestUnicode() { … … 1933 2047 1934 2048 [TestMethod] 1935 [TestCategory("Persistence ")]2049 [TestCategory("Persistence4")] 1936 2050 [TestProperty("Time", "short")] 1937 2051 public void TestQueue() {
Note: See TracChangeset
for help on using the changeset viewer.