Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/05/17 17:36:50 (7 years ago)
Author:
jkarder
Message:

#2520: worked on persistence

File:
1 edited

Legend:

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

    r14537 r14549  
    2727namespace HeuristicLab.Persistence {
    2828  public sealed class Mapper {
     29    internal class MappingEqualityComparer : IEqualityComparer<object> {
     30      bool IEqualityComparer<object>.Equals(object x, object y) {
     31        if (x == null && y == null) return true;
     32        if (x == null ^ y == null) return false;
     33        if (x.GetType() != y.GetType()) return false;
     34
     35        var type = x.GetType();
     36        if (type.IsValueType || type == typeof(string)) return x.Equals(y);
     37        return object.ReferenceEquals(x, y);
     38      }
     39
     40      int IEqualityComparer<object>.GetHashCode(object obj) {
     41        return obj == null ? 0 : obj.GetHashCode();
     42      }
     43    }
     44
    2945    private static StaticCache staticCache = null;
    3046    private static object locker = new object();
     
    4157    private Index<Type> types;
    4258    private Index<string> strings;
    43     private Index<Box> boxes;
     59    private Dictionary<uint, Box> boxId2Box;
    4460    private Dictionary<object, uint> object2BoxId;
    4561    private Dictionary<uint, object> boxId2object;
    4662
    47     public long BoxCount { get; private set; }
     63    public uint BoxCount { get; private set; }
    4864
    4965    public Mapper() {
     
    5167      types = new Index<Type>();
    5268      strings = new Index<string>();
    53       boxes = new Index<Box>();
    54       object2BoxId = new Dictionary<object, uint>(new ReferenceEqualityComparer<object>());
     69      boxId2Box = new Dictionary<uint, Box>();
     70      object2BoxId = new Dictionary<object, uint>(new MappingEqualityComparer());
    5571      boxId2object = new Dictionary<uint, object>();
    5672
     
    8197    public uint GetBoxId(object o) {
    8298      uint boxId;
    83       if (object2BoxId.TryGetValue(o, out boxId)) return boxId;
    8499
    85100      if (o == null)
    86         boxId = boxes.GetIndex(null);
     101        boxId = 0;
    87102      else {
     103        if (object2BoxId.TryGetValue(o, out boxId)) return boxId;
    88104        var type = o.GetType();
    89105        var typeInfo = StaticCache.GetTypeInfo(type);
    90106        if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType());
    91         BoxCount++;
     107        boxId = ++BoxCount;
    92108        typeInfo.Used++;
    93         boxId = boxes.GetIndex(typeInfo.Transformer.ToBox(o, this));
     109        object2BoxId.Add(o, boxId);
     110        boxId2Box.Add(boxId, typeInfo.Transformer.ToBox(o, this));
    94111      }
    95       object2BoxId.Add(o, boxId);
    96112      return boxId;
    97113    }
     
    100116      if (boxId2object.TryGetValue(boxId, out o)) return o;
    101117
    102       var box = this.boxes.GetValue(boxId);
     118      Box box;
     119      boxId2Box.TryGetValue(boxId, out box);
     120
    103121      if (box == null)
    104122        o = null;
     
    106124        var transformer = transformers.GetValue(box.TransformerId);
    107125        o = transformer.ToObject(box, this);
     126        boxId2object.Add(boxId, o);
     127        transformer.FillFromBox(o, box, this);
    108128      }
    109       boxId2object.Add(boxId, o);
    110129      return o;
    111130    }
     
    122141      bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFromUtf8(x)));
    123142      bundle.AddRangeStrings(mapper.strings.GetValues());
    124       bundle.AddRangeBoxes(mapper.boxes.GetValues());
     143      bundle.AddRangeBoxes(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
    125144      return bundle.Build();
    126145    }
     
    129148      mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => StaticCache.GetType(x)));
    130149      mapper.strings = new Index<string>(bundle.StringsList);
    131       mapper.boxes = new Index<Box>(bundle.BoxesList);
     150      mapper.boxId2Box = bundle.BoxesList.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box);
    132151      mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetTransformer(x)));
    133152      return mapper.GetObject(bundle.RootBoxId);
Note: See TracChangeset for help on using the changeset viewer.