- Timestamp:
- 11/24/15 15:22:21 (9 years ago)
- Location:
- branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r13347 r13358 29 29 namespace HeuristicLab.Persistence { 30 30 public sealed class Mapper { 31 private static StaticCache staticCache = null; 32 private static object locker = new object(); 33 public static StaticCache StaticCache { 34 get { 35 lock (locker) { 36 if (staticCache == null) staticCache = new StaticCache(); 37 return staticCache; 38 } 39 } 40 } 41 31 42 private Index<ITransformer> transformers; 32 private Dictionary<Type, TypeInfo> typeInfos;33 43 private Index<Type> types; 34 44 private Index<string> strings; 35 45 private Index<Box> boxes; 36 private Dictionary<object, uint> boxCache;37 private Dictionary<uint, object> objectCache;46 private Dictionary<object, uint> object2BoxId; 47 private Dictionary<uint, object> boxId2object; 38 48 39 49 public long BoxCount { get; private set; } … … 41 51 public Mapper() { 42 52 transformers = new Index<ITransformer>(); 43 typeInfos = new Dictionary<Type, TypeInfo>();44 53 types = new Index<Type>(); 45 54 strings = new Index<string>(); 46 55 boxes = new Index<Box>(); 47 boxCache= new Dictionary<object, uint>(new ReferenceEqualityComparer<object>());48 objectCache= new Dictionary<uint, object>();56 object2BoxId = new Dictionary<object, uint>(new ReferenceEqualityComparer<object>()); 57 boxId2object = new Dictionary<uint, object>(); 49 58 50 59 BoxCount = 0; 51 52 foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Order)) {53 transformer.Initialize(transformers.GetIndex(transformer));54 }55 60 } 56 61 57 public TypeInfo GetTypeInfo(Type type) { 58 TypeInfo typeInfo; 59 if (!typeInfos.TryGetValue(type, out typeInfo)) { 60 var transformer = transformers.GetValues().Where(x => x.CanTransformType(type)).FirstOrDefault(); 61 typeInfo = new TypeInfo(type, transformer); 62 typeInfos.Add(type, typeInfo); 63 } 64 return typeInfo; 62 public uint GetTransformerId(ITransformer transformer) { 63 return transformers.GetIndex(transformer); 65 64 } 66 public IEnumerable<TypeInfo> GetTypeInfos() { 67 return typeInfos.Values; 68 } 69 public IEnumerable<Type> GetTypes() { 70 return typeInfos.Values.Select(x => x.Type); 65 public ITransformer GetTransformer(uint transformerId) { 66 return transformers.GetValue(transformerId); 71 67 } 72 68 … … 87 83 public uint GetBoxId(object o) { 88 84 uint boxId; 89 if ( boxCache.TryGetValue(o, out boxId)) return boxId;85 if (object2BoxId.TryGetValue(o, out boxId)) return boxId; 90 86 91 87 if (o == null) … … 93 89 else { 94 90 var type = o.GetType(); 95 var typeInfo = GetTypeInfo(type);91 var typeInfo = StaticCache.GetTypeInfo(type); 96 92 if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType()); 97 93 BoxCount++; … … 99 95 boxId = boxes.GetIndex(typeInfo.Transformer.ToBox(o, this)); 100 96 } 101 boxCache.Add(o, boxId);97 object2BoxId.Add(o, boxId); 102 98 return boxId; 103 99 } 104 100 public object GetObject(uint boxId) { 105 101 object o; 106 if ( objectCache.TryGetValue(boxId, out o)) return o;102 if (boxId2object.TryGetValue(boxId, out o)) return o; 107 103 108 104 var box = this.boxes.GetValue(boxId); … … 113 109 o = transformer.ToObject(box, this); 114 110 } 115 objectCache.Add(boxId, o);111 boxId2object.Add(boxId, o); 116 112 return o; 117 113 } 118 114 119 115 public object CreateInstance(Type type) { 120 return GetTypeInfo(type).GetConstructor()();116 return StaticCache.GetTypeInfo(type).GetConstructor()(); 121 117 } 122 118 … … 126 122 bundle.RootBoxId = mapper.GetBoxId(o); 127 123 bundle.AddRangeTransformerGuids(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray()))); 128 bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => mapper.typeInfos[x].StorableClassAttribute.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));124 bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFrom(x.ToByteArray()))); 129 125 bundle.AddRangeStrings(mapper.strings.GetValues()); 130 126 bundle.AddRangeBoxes(mapper.boxes.GetValues()); … … 132 128 } 133 129 public static object ToObject(Bundle bundle) { 134 var types = new Dictionary<Guid, Type>();135 foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {136 foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x)))137 types.Add(StorableClassAttribute.GetStorableClassAttribute(t).Guid, t);138 }139 140 130 var mapper = new Mapper(); 141 mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => types[x]));131 mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetType(x))); 142 132 mapper.strings = new Index<string>(bundle.StringsList); 143 133 mapper.boxes = new Index<Box>(bundle.BoxesList); 144 mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => mapper.transformers.GetValues().First(y => y.Guid == x))); 145 foreach (var type in mapper.types.GetValues()) 146 mapper.typeInfos.Add(type, new TypeInfo(type)); 147 134 mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetTransformer(x))); 148 135 return mapper.GetObject(bundle.RootBoxId); 149 136 } -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Transformer.cs
r13347 r13358 23 23 24 24 namespace HeuristicLab.Persistence { 25 internalabstract class Transformer : ITransformer {25 public abstract class Transformer : ITransformer { 26 26 public Guid Guid { get; private set; } 27 27 public uint Id { get; private set; }
Note: See TracChangeset
for help on using the changeset viewer.