Changeset 13367


Ignore:
Timestamp:
11/24/15 16:59:57 (4 years ago)
Author:
swagner
Message:

#2520: Worked on new persistence implementation

Location:
branches/PersistenceOverhaul/HeuristicLab.Persistence
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableClassAttribute.cs

    r13347 r13367  
    4949    }
    5050
     51    public StorableClassAttribute(string guid) {
     52      Guid = new Guid(guid);
     53      Released = false;
     54    }
     55
    5156    /// <summary>
    5257    /// Initializes a new instance of the <see cref="StorableClassAttribute"/> class.
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/ProtoBufSerializer.cs

    r13347 r13367  
    2929
    3030    protected override Bundle DeserializeBundle(Stream stream) {
    31       return Bundle.ParseFrom(stream);
     31      return Bundle.ParseFrom(stream, Mapper.StaticCache.GetExtensionRegistry());
    3232    }
    3333  }
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r13358 r13367  
    3636    private Dictionary<Type, Guid> type2Guid;
    3737    private Dictionary<Type, TypeInfo> typeInfos;
     38    private ExtensionRegistry extensionRegistry;
    3839
    3940    internal StaticCache() {
     
    4748        type2Guid = new Dictionary<Type, Guid>();
    4849        typeInfos = new Dictionary<Type, TypeInfo>();
     50        extensionRegistry = ExtensionRegistry.CreateInstance();
    4951
    50         foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Order)) {
     52        foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Priority)) {
    5153          guid2Transformer.Add(transformer.Guid, transformer);
    5254          transformer2Guid.Add(transformer, transformer.Guid);
     
    7173            RegisterType(StorableClassAttribute.GetStorableClassAttribute(t).Guid, t);
    7274        }
     75
     76        RegisterExtension(BoolBox.Bool);
     77        RegisterExtension(IntBox.Int);
     78        RegisterExtension(LongBox.Long);
     79        RegisterExtension(UnsignedIntBox.UnsignedInt);
     80        RegisterExtension(UnsignedLongBox.UnsignedLong);
     81        RegisterExtension(FloatBox.Float);
     82        RegisterExtension(DoubleBox.Double);
     83        RegisterExtension(StringBox.String);
     84        RegisterExtension(BytesBox.Bytes);
     85        RegisterExtension(BoolArrayBox.BoolArray);
     86        RegisterExtension(IntArrayBox.IntArray);
     87        RegisterExtension(MatrixBox.Matrix);
     88        RegisterExtension(BoolMatrixBox.BoolMatrix);
     89        RegisterExtension(IntMatrixBox.IntMatrix);
     90        RegisterExtension(DictionaryBox.Dictionary);
    7391      }
    7492    }
     
    7997        type2Guid.Add(type, guid);
    8098      }
     99    }
     100    public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
     101      extensionRegistry.Add(extension);
    81102    }
    82103
     
    104125      }
    105126    }
     127    public ExtensionRegistry GetExtensionRegistry() {
     128      return extensionRegistry;
     129    }
    106130  }
    107131}
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Transformer.cs

    r13358 r13367  
    2525  public abstract class Transformer : ITransformer {
    2626    public Guid Guid { get; private set; }
    27     public uint Id { get; private set; }
    28     public uint Order { get; private set; }
     27    public uint Priority { get; private set; }
    2928
    3029    protected Transformer() {
    3130      Guid = TransformerAttribute.GetGuid(this.GetType());
    32       Order = TransformerAttribute.GetOrder(this.GetType());
    33       Id = 0;
    34     }
    35 
    36     public void Initialize(uint id) {
    37       Id = id;
     31      Priority = TransformerAttribute.GetPriority(this.GetType());
    3832    }
    3933
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/TransformerAttribute.cs

    r13347 r13367  
    2626  public sealed class TransformerAttribute : Attribute {
    2727    public Guid Guid { get; private set; }
    28     public uint Order { get; private set; }
     28    public uint Priority { get; private set; }
    2929
    30     public TransformerAttribute(string guid, uint order) {
     30    public TransformerAttribute(string guid, uint priority) {
    3131      Guid = new Guid(guid);
    32       Order = order;
     32      Priority = priority;
    3333    }
    3434
     
    4242      return GetTransformerAttribute(type).Guid;
    4343    }
    44     public static uint GetOrder(Type type) {
    45       return GetTransformerAttribute(type).Order;
     44    public static uint GetPriority(Type type) {
     45      return GetTransformerAttribute(type).Priority;
    4646    }
    4747  }
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/ITransformer.cs

    r13347 r13367  
    2525  public interface ITransformer {
    2626    Guid Guid { get; }
    27     uint Id { get; }
    28     uint Order { get; }
    29 
    30     void Initialize(uint id);
     27    uint Priority { get; }
    3128
    3229    bool CanTransformType(Type type);
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto

    r13347 r13367  
    1616}
    1717
    18 // value boxes
     18// scalar boxes
    1919message BoolBox {
    2020  extend Box {
     
    7373
    7474// array boxes
    75 message ArrayBox {
     75message BoolArrayBox {
    7676  extend Box {
    77     required ArrayBox array = 120;
    78   }
    79   optional uint32 element_type_id = 1;
    80 
    81   extensions 100 to max;
    82 }
    83 message BoolArrayBox {
    84   extend ArrayBox {
    85     required BoolArrayBox bool_array = 100;
     77    required BoolArrayBox bool_array = 120;
    8678  }
    8779  repeated bool values = 1 [packed = true];
    8880}
    8981message IntArrayBox {
    90   extend ArrayBox {
    91     required IntArrayBox int_array = 101;
     82  extend Box {
     83    required IntArrayBox int_array = 121;
    9284  }
    9385  repeated int32 values = 1 [packed = true];
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r13358 r13367  
    2020#endregion
    2121
     22using Google.ProtocolBuffers;
    2223using System;
     24using System.Collections.Generic;
    2325using System.Drawing;
     26using System.Linq;
    2427
    2528namespace HeuristicLab.Persistence {
     
    4346  }
    4447
    45   #region Scalar Value Boxes
     48  #region Scalar Box Transformers
    4649  public abstract class BoolBoxTransformer<T> : BoxTransformer<T> {
    4750    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     
    6871    protected abstract T ToValueType(int value, Type type, Mapper mapper);
    6972  }
     73  public abstract class LongBoxTransformer<T> : BoxTransformer<T> {
     74    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     75      var b = LongBox.CreateBuilder();
     76      b.Value = ToBoxType(value, mapper);
     77      box.SetExtension<LongBox>(LongBox.Long, b.Build());
     78    }
     79    protected override T Extract(Box box, Type type, Mapper mapper) {
     80      return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);
     81    }
     82    protected abstract long ToBoxType(T value, Mapper mapper);
     83    protected abstract T ToValueType(long value, Type type, Mapper mapper);
     84  }
    7085  public abstract class UnsignedIntBoxTransformer<T> : BoxTransformer<T> {
    7186    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     
    8095    protected abstract T ToValueType(uint value, Type type, Mapper mapper);
    8196  }
    82   public abstract class LongBoxTransformer<T> : BoxTransformer<T> {
    83     protected override void Populate(Box.Builder box, T value, Mapper mapper) {
    84       var b = LongBox.CreateBuilder();
    85       b.Value = ToBoxType(value, mapper);
    86       box.SetExtension<LongBox>(LongBox.Long, b.Build());
    87     }
    88     protected override T Extract(Box box, Type type, Mapper mapper) {
    89       return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);
    90     }
    91     protected abstract long ToBoxType(T value, Mapper mapper);
    92     protected abstract T ToValueType(long value, Type type, Mapper mapper);
    93   }
    9497  public abstract class UnsignedLongBoxTransformer<T> : BoxTransformer<T> {
    9598    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     
    139142    protected abstract string ToBoxType(T value, Mapper mapper);
    140143    protected abstract T ToValueType(string value, Type type, Mapper mapper);
     144  }
     145  public abstract class BytesBoxTransformer<T> : BoxTransformer<T> {
     146    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     147      var b = BytesBox.CreateBuilder();
     148      b.Value = ByteString.CopyFrom(ToBoxType(value, mapper));
     149      box.SetExtension<BytesBox>(BytesBox.Bytes, b.Build());
     150    }
     151    protected override T Extract(Box box, Type type, Mapper mapper) {
     152      return ToValueType(box.GetExtension(BytesBox.Bytes).Value.ToByteArray(), type, mapper);
     153    }
     154    protected abstract byte[] ToBoxType(T value, Mapper mapper);
     155    protected abstract T ToValueType(byte[] value, Type type, Mapper mapper);
     156  }
     157  #endregion
     158
     159  #region Array Box Transformers
     160  public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> {
     161    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     162      var b = BoolArrayBox.CreateBuilder();
     163      b.AddRangeValues(ToBoxType(value, mapper));
     164      box.SetExtension<BoolArrayBox>(BoolArrayBox.BoolArray, b.Build());
     165    }
     166    protected override T Extract(Box box, Type type, Mapper mapper) {
     167      return ToValueType(box.GetExtension(BoolArrayBox.BoolArray).ValuesList, type, mapper);
     168    }
     169    protected abstract IEnumerable<bool> ToBoxType(T value, Mapper mapper);
     170    protected abstract T ToValueType(IEnumerable<bool> value, Type type, Mapper mapper);
     171  }
     172  public abstract class IntArrayBoxTransformer<T> : BoxTransformer<T> {
     173    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     174      var b = IntArrayBox.CreateBuilder();
     175      b.AddRangeValues(ToBoxType(value, mapper));
     176      box.SetExtension<IntArrayBox>(IntArrayBox.IntArray, b.Build());
     177    }
     178    protected override T Extract(Box box, Type type, Mapper mapper) {
     179      return ToValueType(box.GetExtension(IntArrayBox.IntArray).ValuesList, type, mapper);
     180    }
     181    protected abstract IEnumerable<int> ToBoxType(T value, Mapper mapper);
     182    protected abstract T ToValueType(IEnumerable<int> value, Type type, Mapper mapper);
    141183  }
    142184  #endregion
     
    226268    protected override TimeSpan ToValueType(long value, Type type, Mapper mapper) { return new TimeSpan(value); }
    227269  }
     270  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]
     271  internal sealed class ColorTransformers : IntBoxTransformer<Color> {
     272    protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
     273    protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
     274  }
    228275  #endregion
    229276
     
    251298  #endregion
    252299
    253   #region Color
    254   [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]
    255   internal sealed class ColorTransformers : IntBoxTransformer<Color> {
    256     protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
    257     protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
    258   }
    259   #endregion
    260 
    261300  #region Type
    262301  [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 218)]
     
    269308  }
    270309  #endregion
     310
     311  #region Primitive Array Types
     312  [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)]
     313  internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> {
     314    protected override IEnumerable<bool> ToBoxType(bool[] value, Mapper mapper) { return value; }
     315    protected override bool[] ToValueType(IEnumerable<bool> value, Type type, Mapper mapper) { return value.ToArray(); }
     316  }
     317  [Transformer("5F6DC3BC-4433-4AE9-A636-4BD126F7DACD", 306)]
     318  internal sealed class IntArrayTransformer : IntArrayBoxTransformer<int[]> {
     319    protected override IEnumerable<int> ToBoxType(int[] value, Mapper mapper) { return value; }
     320    protected override int[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.ToArray(); }
     321  }
     322  #endregion
    271323}
Note: See TracChangeset for help on using the changeset viewer.