Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs @ 15509

Last change on this file since 15509 was 15509, checked in by jkarder, 6 years ago

#2520: worked on new persistence

  • changed message definitions
  • updated transformers
  • cleaned up
File size: 11.4 KB
RevLine 
[13326]1#region License Information
2/* HeuristicLab
[13347]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13326]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.Generic;
24using System.Linq;
[15509]25using Google.Protobuf;
[13326]26
27namespace HeuristicLab.Persistence {
[13347]28  public sealed class Mapper {
[14549]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
[15509]45    private static StaticCache staticCache;
[13358]46    private static object locker = new object();
47    public static StaticCache StaticCache {
48      get {
49        lock (locker) {
50          if (staticCache == null) staticCache = new StaticCache();
51          return staticCache;
52        }
53      }
54    }
55
[13347]56    private Index<ITransformer> transformers;
57    private Index<Type> types;
[15509]58
[14549]59    private Dictionary<uint, Box> boxId2Box;
[13358]60    private Dictionary<object, uint> object2BoxId;
[15509]61    private Dictionary<uint, object> boxId2Object;
62    private Index<TypeBox> typeBoxes;
[13326]63
[15509]64    private Index<string> strings;
65
66    private Index<BoolArrayBox> boolArrayBoxes;
67    private Index<IntArrayBox> intArrayBoxes;
68    private Index<UnsignedIntArrayBox> unsignedIntArrayBoxes;
69    private Index<LongArrayBox> longArrayBoxes;
70    private Index<UnsignedLongArrayBox> unsignedLongArrayBoxes;
71    private Index<FloatArrayBox> floatArrayBoxes;
72    private Index<DoubleArrayBox> doubleArrayBoxes;
73
74    private Index<DictionaryBox> dictionaryBoxes;
75    private Index<StorableClassBox> storableClassBoxes;
76
[14549]77    public uint BoxCount { get; private set; }
[13326]78
[15509]79
[13347]80    public Mapper() {
81      transformers = new Index<ITransformer>();
82      types = new Index<Type>();
[15509]83
[14549]84      boxId2Box = new Dictionary<uint, Box>();
85      object2BoxId = new Dictionary<object, uint>(new MappingEqualityComparer());
[15509]86      boxId2Object = new Dictionary<uint, object>();
87      typeBoxes = new Index<TypeBox>();
[13326]88
[15509]89      strings = new Index<string>();
90
91      boolArrayBoxes = new Index<BoolArrayBox>();
92      intArrayBoxes = new Index<IntArrayBox>();
93      unsignedIntArrayBoxes = new Index<UnsignedIntArrayBox>();
94      longArrayBoxes = new Index<LongArrayBox>();
95      unsignedLongArrayBoxes = new Index<UnsignedLongArrayBox>();
96      floatArrayBoxes = new Index<FloatArrayBox>();
97      doubleArrayBoxes = new Index<DoubleArrayBox>();
98
99      dictionaryBoxes = new Index<DictionaryBox>();
100      storableClassBoxes = new Index<StorableClassBox>();
101
[13347]102      BoxCount = 0;
[13326]103    }
104
[15509]105    #region Transformers
[13358]106    public uint GetTransformerId(ITransformer transformer) {
107      return transformers.GetIndex(transformer);
[13326]108    }
[15509]109
[13358]110    public ITransformer GetTransformer(uint transformerId) {
111      return transformers.GetValue(transformerId);
[13326]112    }
[15509]113    #endregion
[13326]114
[15509]115    #region Types
[13347]116    public uint GetTypeId(Type type) {
[13326]117      return types.GetIndex(type);
118    }
[15509]119
[13347]120    public Type GetType(uint typeId) {
[13326]121      return types.GetValue(typeId);
122    }
[15509]123    #endregion
[13326]124
[15509]125    #region Boxes
[13347]126    public uint GetBoxId(object o) {
127      uint boxId;
128
129      if (o == null)
[14549]130        boxId = 0;
[13347]131      else {
[14549]132        if (object2BoxId.TryGetValue(o, out boxId)) return boxId;
[13347]133        var type = o.GetType();
[13358]134        var typeInfo = StaticCache.GetTypeInfo(type);
[13347]135        if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType());
[14549]136        boxId = ++BoxCount;
[13347]137        typeInfo.Used++;
[14549]138        object2BoxId.Add(o, boxId);
139        boxId2Box.Add(boxId, typeInfo.Transformer.ToBox(o, this));
[13347]140      }
141      return boxId;
[13326]142    }
[15509]143
144    public Box GetBox(uint boxId) {
145      return boxId2Box[boxId];
146    }
147
[13347]148    public object GetObject(uint boxId) {
149      object o;
[15509]150      if (boxId2Object.TryGetValue(boxId, out o)) return o;
[13326]151
[14549]152      Box box;
153      boxId2Box.TryGetValue(boxId, out box);
154
[13347]155      if (box == null)
156        o = null;
157      else {
158        var transformer = transformers.GetValue(box.TransformerId);
159        o = transformer.ToObject(box, this);
[15509]160        boxId2Object.Add(boxId, o);
[14549]161        transformer.FillFromBox(o, box, this);
[13347]162      }
[15509]163
[13347]164      return o;
[13326]165    }
166
[15509]167    #region Referenced Boxes
168    #region TypeBoxes
169    public uint GetTypeBoxId(TypeBox typeBox) { return typeBoxes.GetIndex(typeBox); }
170    public TypeBox GetTypeBox(uint typeBoxId) { return typeBoxes.GetValue(typeBoxId); }
171    #endregion
172
173    #region BoolArrayBox
174    public uint GetBoolArrayBoxId(BoolArrayBox boolArrayBox) { return boolArrayBoxes.GetIndex(boolArrayBox); }
175    public BoolArrayBox GetBoolArrayBox(uint boolArrayBoxId) { return boolArrayBoxes.GetValue(boolArrayBoxId); }
176    #endregion
177
178    #region IntArrayBox
179    public uint GetIntArrayBoxId(IntArrayBox intArrayBox) { return intArrayBoxes.GetIndex(intArrayBox); }
180    public IntArrayBox GetIntArrayBox(uint intArrayBoxId) { return intArrayBoxes.GetValue(intArrayBoxId); }
181    #endregion
182
183    #region UnsignedIntArrayBox
184    public uint GetUnsignedIntArrayBoxId(UnsignedIntArrayBox unsignedIntArrayBox) { return unsignedIntArrayBoxes.GetIndex(unsignedIntArrayBox); }
185    public UnsignedIntArrayBox GetUnsignedIntArrayBox(uint unsignedIntArrayBoxId) { return unsignedIntArrayBoxes.GetValue(unsignedIntArrayBoxId); }
186    #endregion
187
188    #region LongArrayBox
189    public uint GetLongArrayBoxId(LongArrayBox longArrayBox) { return longArrayBoxes.GetIndex(longArrayBox); }
190    public LongArrayBox GetLongArrayBox(uint longArrayBoxId) { return longArrayBoxes.GetValue(longArrayBoxId); }
191    #endregion
192
193    #region UnsignedLongArrayBox
194    public uint GetUnsignedLongArrayBoxId(UnsignedLongArrayBox unsignedLongArrayBox) { return unsignedLongArrayBoxes.GetIndex(unsignedLongArrayBox); }
195    public UnsignedLongArrayBox GetUnsignedLongArrayBox(uint unsignedLongArrayBoxId) { return unsignedLongArrayBoxes.GetValue(unsignedLongArrayBoxId); }
196    #endregion
197
198    #region FloatArrayBox
199    public uint GetFloatArrayBoxId(FloatArrayBox floatArrayBox) { return floatArrayBoxes.GetIndex(floatArrayBox); }
200    public FloatArrayBox GetFloatArrayBox(uint floatArrayBoxId) { return floatArrayBoxes.GetValue(floatArrayBoxId); }
201    #endregion
202
203    #region DoubleArrayBox
204    public uint GetDoubleArrayBoxId(DoubleArrayBox doubleArrayBox) { return doubleArrayBoxes.GetIndex(doubleArrayBox); }
205    public DoubleArrayBox GetDoubleArrayBox(uint doubleArrayBoxId) { return doubleArrayBoxes.GetValue(doubleArrayBoxId); }
206    #endregion
207
208    #region DictionaryBox
209    public uint GetDictionaryBoxId(DictionaryBox dictionaryBox) { return dictionaryBoxes.GetIndex(dictionaryBox); }
210    public DictionaryBox GetDictionaryBox(uint dictionaryBoxId) { return dictionaryBoxes.GetValue(dictionaryBoxId); }
211    #endregion
212
213    #region StorableClassBox
214    public uint GetStorableClassBoxId(StorableClassBox storableClassBox) { return storableClassBoxes.GetIndex(storableClassBox); }
215    public StorableClassBox GetStorableClassBox(uint storableClassBoxId) { return storableClassBoxes.GetValue(storableClassBoxId); }
216    #endregion
217    #endregion
218    #endregion
219
220    #region Strings
221    public uint GetStringId(string str) {
222      return strings.GetIndex(str);
223    }
224    public string GetString(uint stringId) {
225      return strings.GetValue(stringId);
226    }
227    #endregion
228
[13326]229    public object CreateInstance(Type type) {
[15020]230      try {
231        return StaticCache.GetTypeInfo(type).GetConstructor()();
232      } catch (Exception e) {
233        throw new PersistenceException("Deserialization failed.", e);
234      }
[13326]235    }
236
[13347]237    public static Bundle ToBundle(object o) {
238      var mapper = new Mapper();
[15509]239      var bundle = new Bundle();
240
[13347]241      bundle.RootBoxId = mapper.GetBoxId(o);
[15509]242      bundle.TransformerGuids.AddRange(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
243      bundle.TypeGuids.AddRange(mapper.types.GetValues().Select(x => ByteString.CopyFrom(StaticCache.GetGuid(x).ToByteArray())));
244      bundle.Boxes.AddRange(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
245      bundle.TypeBoxes.AddRange(mapper.typeBoxes.GetValues());
246
247      bundle.Strings.AddRange(mapper.strings.GetValues());
248
249      bundle.BoolArrayBoxes.AddRange(mapper.boolArrayBoxes.GetValues());
250      bundle.IntArrayBoxes.AddRange(mapper.intArrayBoxes.GetValues());
251      bundle.UnsignedIntArrayBoxes.AddRange(mapper.unsignedIntArrayBoxes.GetValues());
252      bundle.LongArrayBoxes.AddRange(mapper.longArrayBoxes.GetValues());
253      bundle.UnsignedLongArrayBoxes.AddRange(mapper.unsignedLongArrayBoxes.GetValues());
254      bundle.FloatArrayBoxes.AddRange(mapper.floatArrayBoxes.GetValues());
255      bundle.DoubleArrayBoxes.AddRange(mapper.doubleArrayBoxes.GetValues());
256
257      bundle.DictionaryBoxes.AddRange(mapper.dictionaryBoxes.GetValues());
258      bundle.StorableClassBoxes.AddRange(mapper.storableClassBoxes.GetValues());
259
260      return bundle;
[13326]261    }
[13347]262    public static object ToObject(Bundle bundle) {
263      var mapper = new Mapper();
[15509]264
265      mapper.transformers = new Index<ITransformer>(bundle.TransformerGuids.Select(x => new Guid(x.ToByteArray())).Select(StaticCache.GetTransformer));
266      mapper.types = new Index<Type>(bundle.TypeGuids.Select(x => StaticCache.GetType(new Guid(x.ToByteArray()))));
267
268      mapper.boxId2Box = bundle.Boxes.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box);
269      mapper.typeBoxes = new Index<TypeBox>(bundle.TypeBoxes);
270
271      mapper.strings = new Index<string>(bundle.Strings);
272
273      mapper.boolArrayBoxes = new Index<BoolArrayBox>(bundle.BoolArrayBoxes);
274      mapper.intArrayBoxes = new Index<IntArrayBox>(bundle.IntArrayBoxes);
275      mapper.unsignedIntArrayBoxes = new Index<UnsignedIntArrayBox>(bundle.UnsignedIntArrayBoxes);
276      mapper.longArrayBoxes = new Index<LongArrayBox>(bundle.LongArrayBoxes);
277      mapper.unsignedLongArrayBoxes = new Index<UnsignedLongArrayBox>(bundle.UnsignedLongArrayBoxes);
278      mapper.floatArrayBoxes = new Index<FloatArrayBox>(bundle.FloatArrayBoxes);
279      mapper.doubleArrayBoxes = new Index<DoubleArrayBox>(bundle.DoubleArrayBoxes);
280
281      mapper.dictionaryBoxes = new Index<DictionaryBox>(bundle.DictionaryBoxes);
282      mapper.storableClassBoxes = new Index<StorableClassBox>(bundle.StorableClassBoxes);
283
[13347]284      return mapper.GetObject(bundle.RootBoxId);
[13326]285    }
286  }
287}
Note: See TracBrowser for help on using the repository browser.