Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/15 15:22:21 (9 years ago)
Author:
swagner
Message:

#2520: Worked on new persistence implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r13347 r13358  
    2929namespace HeuristicLab.Persistence {
    3030  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
    3142    private Index<ITransformer> transformers;
    32     private Dictionary<Type, TypeInfo> typeInfos;
    3343    private Index<Type> types;
    3444    private Index<string> strings;
    3545    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;
    3848
    3949    public long BoxCount { get; private set; }
     
    4151    public Mapper() {
    4252      transformers = new Index<ITransformer>();
    43       typeInfos = new Dictionary<Type, TypeInfo>();
    4453      types = new Index<Type>();
    4554      strings = new Index<string>();
    4655      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>();
    4958
    5059      BoxCount = 0;
    51 
    52       foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Order)) {
    53         transformer.Initialize(transformers.GetIndex(transformer));
    54       }
    5560    }
    5661
    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);
    6564    }
    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);
    7167    }
    7268
     
    8783    public uint GetBoxId(object o) {
    8884      uint boxId;
    89       if (boxCache.TryGetValue(o, out boxId)) return boxId;
     85      if (object2BoxId.TryGetValue(o, out boxId)) return boxId;
    9086
    9187      if (o == null)
     
    9389      else {
    9490        var type = o.GetType();
    95         var typeInfo = GetTypeInfo(type);
     91        var typeInfo = StaticCache.GetTypeInfo(type);
    9692        if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType());
    9793        BoxCount++;
     
    9995        boxId = boxes.GetIndex(typeInfo.Transformer.ToBox(o, this));
    10096      }
    101       boxCache.Add(o, boxId);
     97      object2BoxId.Add(o, boxId);
    10298      return boxId;
    10399    }
    104100    public object GetObject(uint boxId) {
    105101      object o;
    106       if (objectCache.TryGetValue(boxId, out o)) return o;
     102      if (boxId2object.TryGetValue(boxId, out o)) return o;
    107103
    108104      var box = this.boxes.GetValue(boxId);
     
    113109        o = transformer.ToObject(box, this);
    114110      }
    115       objectCache.Add(boxId, o);
     111      boxId2object.Add(boxId, o);
    116112      return o;
    117113    }
    118114
    119115    public object CreateInstance(Type type) {
    120       return GetTypeInfo(type).GetConstructor()();
     116      return StaticCache.GetTypeInfo(type).GetConstructor()();
    121117    }
    122118
     
    126122      bundle.RootBoxId = mapper.GetBoxId(o);
    127123      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())));
    129125      bundle.AddRangeStrings(mapper.strings.GetValues());
    130126      bundle.AddRangeBoxes(mapper.boxes.GetValues());
     
    132128    }
    133129    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 
    140130      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)));
    142132      mapper.strings = new Index<string>(bundle.StringsList);
    143133      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)));
    148135      return mapper.GetObject(bundle.RootBoxId);
    149136    }
Note: See TracChangeset for help on using the changeset viewer.