- Timestamp:
- 01/05/17 17:36:50 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r14537 r14549 27 27 namespace HeuristicLab.Persistence { 28 28 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 29 45 private static StaticCache staticCache = null; 30 46 private static object locker = new object(); … … 41 57 private Index<Type> types; 42 58 private Index<string> strings; 43 private Index<Box> boxes;59 private Dictionary<uint, Box> boxId2Box; 44 60 private Dictionary<object, uint> object2BoxId; 45 61 private Dictionary<uint, object> boxId2object; 46 62 47 public longBoxCount { get; private set; }63 public uint BoxCount { get; private set; } 48 64 49 65 public Mapper() { … … 51 67 types = new Index<Type>(); 52 68 strings = new Index<string>(); 53 box es = 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()); 55 71 boxId2object = new Dictionary<uint, object>(); 56 72 … … 81 97 public uint GetBoxId(object o) { 82 98 uint boxId; 83 if (object2BoxId.TryGetValue(o, out boxId)) return boxId;84 99 85 100 if (o == null) 86 boxId = boxes.GetIndex(null);101 boxId = 0; 87 102 else { 103 if (object2BoxId.TryGetValue(o, out boxId)) return boxId; 88 104 var type = o.GetType(); 89 105 var typeInfo = StaticCache.GetTypeInfo(type); 90 106 if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType()); 91 BoxCount++;107 boxId = ++BoxCount; 92 108 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)); 94 111 } 95 object2BoxId.Add(o, boxId);96 112 return boxId; 97 113 } … … 100 116 if (boxId2object.TryGetValue(boxId, out o)) return o; 101 117 102 var box = this.boxes.GetValue(boxId); 118 Box box; 119 boxId2Box.TryGetValue(boxId, out box); 120 103 121 if (box == null) 104 122 o = null; … … 106 124 var transformer = transformers.GetValue(box.TransformerId); 107 125 o = transformer.ToObject(box, this); 126 boxId2object.Add(boxId, o); 127 transformer.FillFromBox(o, box, this); 108 128 } 109 boxId2object.Add(boxId, o);110 129 return o; 111 130 } … … 122 141 bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFromUtf8(x))); 123 142 bundle.AddRangeStrings(mapper.strings.GetValues()); 124 bundle.AddRangeBoxes(mapper.box es.GetValues());143 bundle.AddRangeBoxes(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value)); 125 144 return bundle.Build(); 126 145 } … … 129 148 mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => StaticCache.GetType(x))); 130 149 mapper.strings = new Index<string>(bundle.StringsList); 131 mapper.box es = 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); 132 151 mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetTransformer(x))); 133 152 return mapper.GetObject(bundle.RootBoxId);
Note: See TracChangeset
for help on using the changeset viewer.