Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs @ 14549

Last change on this file since 14549 was 14549, checked in by jkarder, 7 years ago

#2520: worked on persistence

File size: 12.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using Google.ProtocolBuffers;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Persistence {
32  public sealed class StaticCache {
33    private static object locker = new object();
34
35    private Dictionary<Guid, ITransformer> guid2Transformer;
36    private Dictionary<ITransformer, Guid> transformer2Guid;
37    private Dictionary<string, Type> guid2Type;
38    private Dictionary<Type, string> type2Guid;
39    private Dictionary<Type, TypeInfo> typeInfos;
40    private ExtensionRegistry extensionRegistry;
41
42    internal StaticCache() {
43      guid2Transformer = new Dictionary<Guid, ITransformer>();
44      transformer2Guid = new Dictionary<ITransformer, Guid>();
45      guid2Type = new Dictionary<string, Type>();
46      type2Guid = new Dictionary<Type, string>();
47      typeInfos = new Dictionary<Type, TypeInfo>();
48      extensionRegistry = ExtensionRegistry.CreateInstance();
49
50      foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>())
51        RegisterTransformer(transformer);
52
53
54      // ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69
55      // E8348C94-9817-4164-9C98-377689F83F30
56      // 05551382-E894-4218-B860-FEE1D92CA07D
57      // 4CC0D44E-65B2-4DF1-A333-30F058CB78CA
58      // 5D451A64-EBD9-411F-A07E-232250B33784
59      // EEBD2D26-56CC-45EA-900A-DD29B9F598CE
60      // B8EEEFA7-8576-42DD-937C-AFBB66297B56
61      // 4C3C8E0A-7DCF-4814-B798-AECB86E1E4E3
62      // EAA4FF91-B90C-47CB-AC0F-66AB66D41186
63      // EB51B342-826F-4004-B2C6-F2A03A8C35C9
64      // 14138614-45DB-4A0D-9E59-AE9373241D6B
65
66
67      //       RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
68      //       RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
69      //       RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
70      //       RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
71      //       RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
72      //       RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
73      //       RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
74      //       RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
75      //       RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
76      //       RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
77      //       RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
78      //       RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
79      //       RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
80      //       RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
81      //       RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
82      //       RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
83      //       RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
84      //       RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
85      //
86      //       RegisterType(new Guid("9CF55419-439B-4A90-B2ED-8C7F7768EB61"), typeof(Array));
87      //       RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
88      //       RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
89      //       RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
90      //       RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
91      //       RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
92      //       RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
93      //       RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
94      //       RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
95      //       RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
96      //       RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
97      //       RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
98      //       RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
99      //       RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
100      //       RegisterType(new Guid("DB2A785C-5803-42C7-A01F-E1FD845A83A1"), typeof(List<>));
101      //       RegisterType(new Guid("EB98A564-BEDC-458A-9E9B-4BF3CDAFB9BE"), typeof(Stack<>));
102      //       RegisterType(new Guid("D5E3118B-957A-43B5-A740-1BBAA0EAB666"), typeof(System.Collections.Stack));
103      //       RegisterType(new Guid("7E2F3277-7216-4295-A498-9ACA43527D5B"), typeof(HashSet<>));
104      //       RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>));
105
106      RegisterTypeAndFullName(typeof(object));
107      RegisterTypeAndFullName(typeof(bool));
108      RegisterTypeAndFullName(typeof(byte));
109      RegisterTypeAndFullName(typeof(sbyte));
110      RegisterTypeAndFullName(typeof(short));
111      RegisterTypeAndFullName(typeof(ushort));
112      RegisterTypeAndFullName(typeof(char));
113      RegisterTypeAndFullName(typeof(int));
114      RegisterTypeAndFullName(typeof(uint));
115      RegisterTypeAndFullName(typeof(long));
116      RegisterTypeAndFullName(typeof(ulong));
117      RegisterTypeAndFullName(typeof(float));
118      RegisterTypeAndFullName(typeof(double));
119      RegisterTypeAndFullName(typeof(decimal));
120      RegisterTypeAndFullName(typeof(DateTime));
121      RegisterTypeAndFullName(typeof(TimeSpan));
122      RegisterTypeAndFullName(typeof(Font));
123      RegisterTypeAndFullName(typeof(Color));
124      RegisterTypeAndFullName(typeof(Bitmap));
125      RegisterTypeAndFullName(typeof(Point));
126      RegisterTypeAndFullName(typeof(KeyValuePair<,>));
127      RegisterTypeAndFullName(typeof(Tuple<>));
128      RegisterTypeAndFullName(typeof(Tuple<,>));
129      RegisterTypeAndFullName(typeof(Tuple<,,>));
130      RegisterTypeAndFullName(typeof(Tuple<,,,>));
131      RegisterTypeAndFullName(typeof(Tuple<,,,,>));
132      RegisterTypeAndFullName(typeof(Tuple<,,,,,>));
133      RegisterTypeAndFullName(typeof(Tuple<,,,,,,>));
134      RegisterTypeAndFullName(typeof(Tuple<,,,,,,,>));
135      RegisterTypeAndFullName(typeof(Nullable<>));
136      RegisterTypeAndFullName(typeof(string));
137
138      RegisterTypeAndFullName(typeof(Array));
139      RegisterTypeAndFullName(typeof(bool[]));
140      RegisterTypeAndFullName(typeof(byte[]));
141      RegisterTypeAndFullName(typeof(sbyte[]));
142      RegisterTypeAndFullName(typeof(short[]));
143      RegisterTypeAndFullName(typeof(ushort[]));
144      RegisterTypeAndFullName(typeof(char[]));
145      RegisterTypeAndFullName(typeof(int[]));
146      RegisterTypeAndFullName(typeof(uint[]));
147      RegisterTypeAndFullName(typeof(long[]));
148      RegisterTypeAndFullName(typeof(ulong[]));
149      RegisterTypeAndFullName(typeof(float[]));
150      RegisterTypeAndFullName(typeof(double[]));
151      RegisterTypeAndFullName(typeof(string[]));
152      RegisterTypeAndFullName(typeof(List<>));
153      RegisterTypeAndFullName(typeof(ArrayList));
154      RegisterTypeAndFullName(typeof(HashSet<>));
155      RegisterTypeAndFullName(typeof(Stack<>));
156      RegisterTypeAndFullName(typeof(Stack));
157      RegisterTypeAndFullName(typeof(Queue<>));
158      RegisterTypeAndFullName(typeof(Queue));
159      RegisterTypeAndFullName(typeof(Dictionary<,>));
160
161      foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
162        foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x) || x.IsValueType && !x.IsPrimitive && !x.IsEnum && x.IsSealed))
163          RegisterTypeAndFullName(t);
164      }
165
166      var registeredTypes = type2Guid.Keys.ToArray();
167      foreach (var t in registeredTypes) {
168        AddBaseTypesRec(t);
169      }
170
171      RegisterExtension(BoolBox.Bool);
172      RegisterExtension(IntBox.Int);
173      RegisterExtension(LongBox.Long);
174      RegisterExtension(UnsignedIntBox.UnsignedInt);
175      RegisterExtension(UnsignedLongBox.UnsignedLong);
176      RegisterExtension(FloatBox.Float);
177      RegisterExtension(DoubleBox.Double);
178      RegisterExtension(StringBox.String);
179      RegisterExtension(BytesBox.Bytes);
180      RegisterExtension(BoolArrayBox.BoolArray);
181      RegisterExtension(ByteArrayBox.ByteArray);
182      RegisterExtension(IntArrayBox.IntArray);
183      RegisterExtension(LongArrayBox.LongArray);
184      RegisterExtension(UnsignedIntArrayBox.UnsignedIntArray);
185      RegisterExtension(UnsignedLongArrayBox.UnsignedLongArray);
186      RegisterExtension(FloatArrayBox.FloatArray);
187      RegisterExtension(DoubleArrayBox.DoubleArray);
188      RegisterExtension(StringArrayBox.StringArray);
189      RegisterExtension(MatrixBox.Matrix);
190      RegisterExtension(BoolMatrixBox.BoolMatrix);
191      RegisterExtension(IntMatrixBox.IntMatrix);
192      RegisterExtension(DictionaryBox.Dictionary);
193      RegisterExtension(StorableClassBox.StorableClass);
194      RegisterExtension(TypeBox.Type);
195    }
196
197    private void AddBaseTypesRec(Type t) {
198      foreach (var interfaceType in t.GetInterfaces()) {
199        RegisterTypeAndFullName(interfaceType);
200        AddBaseTypesRec(interfaceType);
201      }
202      if (t.BaseType != null) {
203        RegisterTypeAndFullName(t.BaseType);
204        AddBaseTypesRec(t.BaseType);
205      }
206    }
207
208    public void RegisterTransformer(ITransformer transformer) {
209      lock (locker) {
210        guid2Transformer.Add(transformer.Guid, transformer);
211        transformer2Guid.Add(transformer, transformer.Guid);
212      }
213    }
214    //    public void RegisterType(Guid guid, Type type) {
215    //      lock (locker) {
216    //        guid2Type.Add(guid, type);
217    //        type2Guid.Add(type, guid);
218    //      }
219    //    }
220    public void RegisterTypeAndFullName(Type type) {
221      lock (locker) {
222        var key = type.AssemblyQualifiedName ?? type.Name;
223        if (!guid2Type.ContainsKey(key)) {
224          guid2Type.Add(key, type);
225          type2Guid.Add(type, key);
226        }
227      }
228    }
229    public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
230      lock (locker) {
231        extensionRegistry.Add(extension);
232      }
233    }
234
235    public ITransformer GetTransformer(Guid guid) {
236      return guid2Transformer[guid];
237    }
238    public Guid GetGuid(ITransformer transformer) {
239      return transformer2Guid[transformer];
240    }
241    public Type GetType(string guid) {
242      return guid2Type[guid];
243    }
244    public string GetGuid(Type type) {
245      return type2Guid[type];
246    }
247    public TypeInfo GetTypeInfo(Type type) {
248      lock (locker) {
249        TypeInfo typeInfo;
250        if (!typeInfos.TryGetValue(type, out typeInfo)) {
251          var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type));
252          typeInfo = new TypeInfo(type, transformer);
253          typeInfos.Add(type, typeInfo);
254        }
255        return typeInfo;
256      }
257    }
258    public ExtensionRegistry GetExtensionRegistry() {
259      return extensionRegistry;
260    }
261  }
262}
Note: See TracBrowser for help on using the repository browser.