[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[13347] | 25 | using Google.ProtocolBuffers;
|
---|
[13326] | 26 |
|
---|
| 27 | namespace HeuristicLab.Persistence {
|
---|
[13347] | 28 | public sealed class Mapper {
|
---|
[13358] | 29 | private static StaticCache staticCache = null;
|
---|
| 30 | private static object locker = new object();
|
---|
| 31 | public static StaticCache StaticCache {
|
---|
| 32 | get {
|
---|
| 33 | lock (locker) {
|
---|
| 34 | if (staticCache == null) staticCache = new StaticCache();
|
---|
| 35 | return staticCache;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[13347] | 40 | private Index<ITransformer> transformers;
|
---|
| 41 | private Index<Type> types;
|
---|
[13326] | 42 | private Index<string> strings;
|
---|
[13347] | 43 | private Index<Box> boxes;
|
---|
[13358] | 44 | private Dictionary<object, uint> object2BoxId;
|
---|
| 45 | private Dictionary<uint, object> boxId2object;
|
---|
[13326] | 46 |
|
---|
[13347] | 47 | public long BoxCount { get; private set; }
|
---|
[13326] | 48 |
|
---|
[13347] | 49 | public Mapper() {
|
---|
| 50 | transformers = new Index<ITransformer>();
|
---|
| 51 | types = new Index<Type>();
|
---|
[13326] | 52 | strings = new Index<string>();
|
---|
[13347] | 53 | boxes = new Index<Box>();
|
---|
[13358] | 54 | object2BoxId = new Dictionary<object, uint>(new ReferenceEqualityComparer<object>());
|
---|
| 55 | boxId2object = new Dictionary<uint, object>();
|
---|
[13326] | 56 |
|
---|
[13347] | 57 | BoxCount = 0;
|
---|
[13326] | 58 | }
|
---|
| 59 |
|
---|
[13358] | 60 | public uint GetTransformerId(ITransformer transformer) {
|
---|
| 61 | return transformers.GetIndex(transformer);
|
---|
[13326] | 62 | }
|
---|
[13358] | 63 | public ITransformer GetTransformer(uint transformerId) {
|
---|
| 64 | return transformers.GetValue(transformerId);
|
---|
[13326] | 65 | }
|
---|
| 66 |
|
---|
[13347] | 67 | public uint GetTypeId(Type type) {
|
---|
[13326] | 68 | return types.GetIndex(type);
|
---|
| 69 | }
|
---|
[13347] | 70 | public Type GetType(uint typeId) {
|
---|
[13326] | 71 | return types.GetValue(typeId);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public uint GetStringId(string str) {
|
---|
| 75 | return strings.GetIndex(str);
|
---|
| 76 | }
|
---|
| 77 | public string GetString(uint stringId) {
|
---|
| 78 | return strings.GetValue(stringId);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[13347] | 81 | public uint GetBoxId(object o) {
|
---|
| 82 | uint boxId;
|
---|
[13358] | 83 | if (object2BoxId.TryGetValue(o, out boxId)) return boxId;
|
---|
[13347] | 84 |
|
---|
| 85 | if (o == null)
|
---|
| 86 | boxId = boxes.GetIndex(null);
|
---|
| 87 | else {
|
---|
| 88 | var type = o.GetType();
|
---|
[13358] | 89 | var typeInfo = StaticCache.GetTypeInfo(type);
|
---|
[13347] | 90 | if (typeInfo.Transformer == null) throw new ArgumentException("Cannot serialize object of type " + o.GetType());
|
---|
| 91 | BoxCount++;
|
---|
| 92 | typeInfo.Used++;
|
---|
| 93 | boxId = boxes.GetIndex(typeInfo.Transformer.ToBox(o, this));
|
---|
| 94 | }
|
---|
[13358] | 95 | object2BoxId.Add(o, boxId);
|
---|
[13347] | 96 | return boxId;
|
---|
[13326] | 97 | }
|
---|
[13347] | 98 | public object GetObject(uint boxId) {
|
---|
| 99 | object o;
|
---|
[13358] | 100 | if (boxId2object.TryGetValue(boxId, out o)) return o;
|
---|
[13326] | 101 |
|
---|
[13347] | 102 | var box = this.boxes.GetValue(boxId);
|
---|
| 103 | if (box == null)
|
---|
| 104 | o = null;
|
---|
| 105 | else {
|
---|
| 106 | var transformer = transformers.GetValue(box.TransformerId);
|
---|
| 107 | o = transformer.ToObject(box, this);
|
---|
| 108 | }
|
---|
[13358] | 109 | boxId2object.Add(boxId, o);
|
---|
[13347] | 110 | return o;
|
---|
[13326] | 111 | }
|
---|
| 112 |
|
---|
| 113 | public object CreateInstance(Type type) {
|
---|
[13358] | 114 | return StaticCache.GetTypeInfo(type).GetConstructor()();
|
---|
[13326] | 115 | }
|
---|
| 116 |
|
---|
[13347] | 117 | public static Bundle ToBundle(object o) {
|
---|
| 118 | var mapper = new Mapper();
|
---|
| 119 | var bundle = Bundle.CreateBuilder();
|
---|
| 120 | bundle.RootBoxId = mapper.GetBoxId(o);
|
---|
| 121 | bundle.AddRangeTransformerGuids(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
|
---|
[14537] | 122 | bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFromUtf8(x)));
|
---|
[13347] | 123 | bundle.AddRangeStrings(mapper.strings.GetValues());
|
---|
| 124 | bundle.AddRangeBoxes(mapper.boxes.GetValues());
|
---|
| 125 | return bundle.Build();
|
---|
[13326] | 126 | }
|
---|
[13347] | 127 | public static object ToObject(Bundle bundle) {
|
---|
| 128 | var mapper = new Mapper();
|
---|
[14537] | 129 | mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => StaticCache.GetType(x)));
|
---|
[13347] | 130 | mapper.strings = new Index<string>(bundle.StringsList);
|
---|
| 131 | mapper.boxes = new Index<Box>(bundle.BoxesList);
|
---|
[13358] | 132 | mapper.transformers = new Index<ITransformer>(bundle.TransformerGuidsList.Select(x => new Guid(x.ToByteArray())).Select(x => StaticCache.GetTransformer(x)));
|
---|
[13347] | 133 | return mapper.GetObject(bundle.RootBoxId);
|
---|
[13326] | 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|