Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/11/17 17:46:11 (6 years ago)
Author:
jkarder
Message:

#2520: worked on new persistence

  • changed message definitions
  • updated transformers
  • cleaned up
File:
1 edited

Legend:

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

    r15020 r15509  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using Google.ProtocolBuffers;
     25using Google.Protobuf;
    2626
    2727namespace HeuristicLab.Persistence {
     
    4343    }
    4444
    45     private static StaticCache staticCache = null;
     45    private static StaticCache staticCache;
    4646    private static object locker = new object();
    4747    public static StaticCache StaticCache {
     
    5656    private Index<ITransformer> transformers;
    5757    private Index<Type> types;
    58     private Index<string> strings;
     58
    5959    private Dictionary<uint, Box> boxId2Box;
    6060    private Dictionary<object, uint> object2BoxId;
    61     private Dictionary<uint, object> boxId2object;
     61    private Dictionary<uint, object> boxId2Object;
     62    private Index<TypeBox> typeBoxes;
     63
     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;
    6276
    6377    public uint BoxCount { get; private set; }
     78
    6479
    6580    public Mapper() {
    6681      transformers = new Index<ITransformer>();
    6782      types = new Index<Type>();
    68       strings = new Index<string>();
     83
    6984      boxId2Box = new Dictionary<uint, Box>();
    7085      object2BoxId = new Dictionary<object, uint>(new MappingEqualityComparer());
    71       boxId2object = new Dictionary<uint, object>();
     86      boxId2Object = new Dictionary<uint, object>();
     87      typeBoxes = new Index<TypeBox>();
     88
     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>();
    72101
    73102      BoxCount = 0;
    74103    }
    75104
     105    #region Transformers
    76106    public uint GetTransformerId(ITransformer transformer) {
    77107      return transformers.GetIndex(transformer);
    78108    }
     109
    79110    public ITransformer GetTransformer(uint transformerId) {
    80111      return transformers.GetValue(transformerId);
    81112    }
    82 
     113    #endregion
     114
     115    #region Types
    83116    public uint GetTypeId(Type type) {
    84117      return types.GetIndex(type);
    85118    }
     119
    86120    public Type GetType(uint typeId) {
    87121      return types.GetValue(typeId);
    88122    }
    89 
    90     public uint GetStringId(string str) {
    91       return strings.GetIndex(str);
    92     }
    93     public string GetString(uint stringId) {
    94       return strings.GetValue(stringId);
    95     }
    96 
    97     public Box GetBox(uint boxId) {
    98       return boxId2Box[boxId];
    99     }
     123    #endregion
     124
     125    #region Boxes
    100126    public uint GetBoxId(object o) {
    101127      uint boxId;
     
    115141      return boxId;
    116142    }
     143
     144    public Box GetBox(uint boxId) {
     145      return boxId2Box[boxId];
     146    }
     147
    117148    public object GetObject(uint boxId) {
    118149      object o;
    119       if (boxId2object.TryGetValue(boxId, out o)) return o;
     150      if (boxId2Object.TryGetValue(boxId, out o)) return o;
    120151
    121152      Box box;
     
    127158        var transformer = transformers.GetValue(box.TransformerId);
    128159        o = transformer.ToObject(box, this);
    129         boxId2object.Add(boxId, o);
     160        boxId2Object.Add(boxId, o);
    130161        transformer.FillFromBox(o, box, this);
    131162      }
     163
    132164      return o;
    133165    }
     166
     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
    134228
    135229    public object CreateInstance(Type type) {
     
    143237    public static Bundle ToBundle(object o) {
    144238      var mapper = new Mapper();
    145       var bundle = Bundle.CreateBuilder();
     239      var bundle = new Bundle();
     240
    146241      bundle.RootBoxId = mapper.GetBoxId(o);
    147       bundle.AddRangeTransformerGuids(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
    148       bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => ByteString.CopyFrom(StaticCache.GetGuid(x).ToByteArray())));
    149       bundle.AddRangeStrings(mapper.strings.GetValues());
    150       bundle.AddRangeBoxes(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
    151       return bundle.Build();
     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;
    152261    }
    153262    public static object ToObject(Bundle bundle) {
    154263      var mapper = new Mapper();
    155       mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => StaticCache.GetType(new Guid(x.ToByteArray()))));
    156       mapper.strings = new Index<string>(bundle.StringsList);
    157       mapper.boxId2Box = bundle.BoxesList.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box);
    158       mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetTransformer(x)));
     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
    159284      return mapper.GetObject(bundle.RootBoxId);
    160285    }
Note: See TracChangeset for help on using the changeset viewer.