- Timestamp:
- 12/11/17 17:46:11 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r15020 r15509 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using Google.Proto colBuffers;25 using Google.Protobuf; 26 26 27 27 namespace HeuristicLab.Persistence { … … 43 43 } 44 44 45 private static StaticCache staticCache = null;45 private static StaticCache staticCache; 46 46 private static object locker = new object(); 47 47 public static StaticCache StaticCache { … … 56 56 private Index<ITransformer> transformers; 57 57 private Index<Type> types; 58 private Index<string> strings; 58 59 59 private Dictionary<uint, Box> boxId2Box; 60 60 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; 62 76 63 77 public uint BoxCount { get; private set; } 78 64 79 65 80 public Mapper() { 66 81 transformers = new Index<ITransformer>(); 67 82 types = new Index<Type>(); 68 strings = new Index<string>(); 83 69 84 boxId2Box = new Dictionary<uint, Box>(); 70 85 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>(); 72 101 73 102 BoxCount = 0; 74 103 } 75 104 105 #region Transformers 76 106 public uint GetTransformerId(ITransformer transformer) { 77 107 return transformers.GetIndex(transformer); 78 108 } 109 79 110 public ITransformer GetTransformer(uint transformerId) { 80 111 return transformers.GetValue(transformerId); 81 112 } 82 113 #endregion 114 115 #region Types 83 116 public uint GetTypeId(Type type) { 84 117 return types.GetIndex(type); 85 118 } 119 86 120 public Type GetType(uint typeId) { 87 121 return types.GetValue(typeId); 88 122 } 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 100 126 public uint GetBoxId(object o) { 101 127 uint boxId; … … 115 141 return boxId; 116 142 } 143 144 public Box GetBox(uint boxId) { 145 return boxId2Box[boxId]; 146 } 147 117 148 public object GetObject(uint boxId) { 118 149 object o; 119 if (boxId2 object.TryGetValue(boxId, out o)) return o;150 if (boxId2Object.TryGetValue(boxId, out o)) return o; 120 151 121 152 Box box; … … 127 158 var transformer = transformers.GetValue(box.TransformerId); 128 159 o = transformer.ToObject(box, this); 129 boxId2 object.Add(boxId, o);160 boxId2Object.Add(boxId, o); 130 161 transformer.FillFromBox(o, box, this); 131 162 } 163 132 164 return o; 133 165 } 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 134 228 135 229 public object CreateInstance(Type type) { … … 143 237 public static Bundle ToBundle(object o) { 144 238 var mapper = new Mapper(); 145 var bundle = Bundle.CreateBuilder(); 239 var bundle = new Bundle(); 240 146 241 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; 152 261 } 153 262 public static object ToObject(Bundle bundle) { 154 263 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 159 284 return mapper.GetObject(bundle.RootBoxId); 160 285 }
Note: See TracChangeset
for help on using the changeset viewer.