Ignore:
Timestamp:
01/03/17 18:21:40 (6 years ago)
Author:
jkarder
Message:

#2520: worked on persistence

Location:
branches/PersistenceOverhaul
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul

    • Property svn:ignore
      •  

        old new  
        2424protoc.exe
        2525obj
         26.vs
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r13407 r14537  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Drawing;
     
    158159
    159160  #region Array Box Transformers
     161  public abstract class ByteArrayBoxTransformer<T> : BoxTransformer<T> {
     162    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     163      var b = ByteArrayBox.CreateBuilder();
     164      b.SetValues(ToByteString(value));
     165      box.SetExtension(ByteArrayBox.ByteArray, b.Build());
     166    }
     167    protected override T Extract(Box box, Type type, Mapper mapper) {
     168      return FromByteString(box.GetExtension(ByteArrayBox.ByteArray).Values);
     169    }
     170    protected abstract ByteString ToByteString(T value);
     171    protected abstract T FromByteString(ByteString byteString);
     172  }
    160173  public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> {
    161174    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     
    407420  #endregion
    408421
    409   #region Type
    410   [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
    411   internal sealed class TypeTransformer : Transformer {
    412     public override bool CanTransformType(Type type) {
    413       return typeof(Type).IsAssignableFrom(type);
    414     }
    415     public override Box ToBox(object o, Mapper mapper) {
    416       var boxBuilder = Box.CreateBuilder();
    417 
    418       boxBuilder.TransformerId = mapper.GetTransformerId(this);
    419       boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
    420 
    421       return boxBuilder.Build();
    422     }
    423 
    424     public override object ToObject(Box box, Mapper mapper) {
    425       return Type.GetType(mapper.GetString(box.TypeId));
    426     }
    427   }
    428   #endregion
     422  //   #region Type
     423  //   [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
     424  //   internal sealed class TypeTransformer : Transformer {
     425  //     public override bool CanTransformType(Type type) {
     426  //       return typeof(Type).IsAssignableFrom(type);
     427  //     }
     428  //     public override Box ToBox(object o, Mapper mapper) {
     429  //       var boxBuilder = Box.CreateBuilder();
     430  //
     431  //       boxBuilder.TransformerId = mapper.GetTransformerId(this);
     432  //       boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
     433  //
     434  //       return boxBuilder.Build();
     435  //     }
     436  //
     437  //     public override object ToObject(Box box, Mapper mapper) {
     438  //       return Type.GetType(mapper.GetString(box.TypeId));
     439  //     }
     440  //   }
     441  //   #endregion
    429442
    430443  #region Array Types
     444  [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
     445  internal sealed class ByteArrayTransformer : ByteArrayBoxTransformer<byte[]> {
     446    protected override ByteString ToByteString(byte[] value) {
     447      return ByteString.CopyFrom(value);
     448    }
     449    protected override byte[] FromByteString(ByteString byteString) {
     450      return byteString.ToArray();
     451    }
     452  }
     453
     454  [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
     455  internal sealed class SByteArrayTransformer : ByteArrayBoxTransformer<sbyte[]> {
     456    protected override ByteString ToByteString(sbyte[] value) {
     457      return ByteString.CopyFrom(value.Select(b => (byte)b).ToArray());
     458    }
     459    protected override sbyte[] FromByteString(ByteString byteString) {
     460      return byteString.Select(b => (sbyte)b).ToArray();
     461    }
     462  }
     463
    431464  [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)]
    432465  internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> {
     
    435468  }
    436469
    437   [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
    438   internal sealed class ByteArrayTransformer : UnsignedIntArrayBoxTransformer<byte[]> {
    439     protected override IEnumerable<uint> ToBoxType(byte[] value, Mapper mapper) { return value.Cast<uint>(); }
    440     protected override byte[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<byte>().ToArray(); }
    441   }
    442 
    443   [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
    444   internal sealed class SByteArrayTransformer : IntArrayBoxTransformer<sbyte[]> {
    445     protected override IEnumerable<int> ToBoxType(sbyte[] value, Mapper mapper) { return value.Cast<int>(); }
    446     protected override sbyte[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.Cast<sbyte>().ToArray(); }
    447   }
    448 
    449470  [Transformer("2811FDD4-6800-4CBA-86D7-9071ED5775ED", 303)]
    450   internal sealed class ShortArrayTransformer : IntArrayBoxTransformer<short[]> {
    451     protected override IEnumerable<int> ToBoxType(short[] value, Mapper mapper) { return value.Cast<int>(); }
    452     protected override short[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.Cast<short>().ToArray(); }
     471  internal sealed class ShortArrayTransformer : ByteArrayBoxTransformer<short[]> {
     472    protected override ByteString ToByteString(short[] value) {
     473      var res = new byte[value.Length * 2];
     474      for (int i = 0; i < value.Length; i++) {
     475        var bytes = BitConverter.GetBytes(value[i]);
     476        Array.Copy(bytes, 0, res, i * 2, 2);
     477      }
     478      return ByteString.CopyFrom(res);
     479    }
     480    protected override short[] FromByteString(ByteString byteString) {
     481      var bytes = byteString.ToArray();
     482      var res = new short[bytes.Length / 2];
     483      for (int i = 0; i < bytes.Length; i += 2) {
     484        res[i / 2] = BitConverter.ToInt16(bytes, i);
     485      }
     486      return res;
     487    }
    453488  }
    454489
    455490  [Transformer("1AAC2625-356C-40BC-8CB4-15CB3D047EB8", 304)]
    456   internal sealed class UShortArrayTransformer : UnsignedIntArrayBoxTransformer<ushort[]> {
    457     protected override IEnumerable<uint> ToBoxType(ushort[] value, Mapper mapper) { return value.Cast<uint>(); }
    458     protected override ushort[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<ushort>().ToArray(); }
     491  internal sealed class UShortArrayTransformer : ByteArrayBoxTransformer<ushort[]> {
     492    protected override ByteString ToByteString(ushort[] value) {
     493      var res = new byte[value.Length * 2];
     494      for (int i = 0; i < value.Length; i++) {
     495        var bytes = BitConverter.GetBytes(value[i]);
     496        Array.Copy(bytes, 0, res, i * 2, 2);
     497      }
     498      return ByteString.CopyFrom(res);
     499    }
     500    protected override ushort[] FromByteString(ByteString byteString) {
     501      var bytes = byteString.ToArray();
     502      var res = new ushort[bytes.Length / 2];
     503      for (int i = 0; i < bytes.Length; i += 2) {
     504        res[i / 2] = BitConverter.ToUInt16(bytes, i);
     505      }
     506      return res;
     507    }
    459508  }
    460509
    461510  [Transformer("12F19098-5D49-4C23-8897-69087F1C146D", 305)]
    462   internal sealed class CharArrayTransformer : UnsignedIntArrayBoxTransformer<char[]> {
    463     protected override IEnumerable<uint> ToBoxType(char[] value, Mapper mapper) { return value.Cast<uint>(); }
    464     protected override char[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.Cast<char>().ToArray(); }
     511  internal sealed class CharArrayTransformer : ByteArrayBoxTransformer<char[]> {
     512    protected override ByteString ToByteString(char[] value) {
     513      var res = new byte[value.Length * 2];
     514      for (int i = 0; i < value.Length; i++) {
     515        var bytes = BitConverter.GetBytes(value[i]);
     516        Array.Copy(bytes, 0, res, i * 2, 2);
     517      }
     518      return ByteString.CopyFrom(res);
     519    }
     520    protected override char[] FromByteString(ByteString byteString) {
     521      var bytes = byteString.ToArray();
     522      var res = new char[bytes.Length / 2];
     523      for (int i = 0; i < bytes.Length; i += 2) {
     524        res[i / 2] = BitConverter.ToChar(bytes, i);
     525      }
     526      return res;
     527    }
    465528  }
    466529
     
    507570  }
    508571
    509   [Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)]
    510   internal sealed class GenericArrayTransformer : UnsignedIntArrayBoxTransformer<object> {
     572  //[Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)]
     573  //internal sealed class GenericArrayTransformer : UnsignedIntArrayBoxTransformer<object> {
     574  //  public override bool CanTransformType(Type type) {
     575  //    return type.IsArray;
     576  //  }
     577  //  protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
     578  //    var arr = (object[])value;
     579  //    return arr.Select(x => mapper.GetBoxId(x));
     580  //  }
     581  //  protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
     582  //    return value.Select(x => mapper.GetObject(x)).ToArray();
     583  //  }
     584  //}
     585
     586  //   internal sealed class QueueTransformer : Transformer {
     587  //     public override bool CanTransformType(Type type) {
     588  //       return type == typeof(System.Collections.Queue) || type == typeof(Queue<>);
     589  //     }
     590  //     public override Box ToBox(object o, Mapper mapper) {
     591  //       var box = Box.CreateBuilder();
     592  //       box.TransformerId = mapper.GetTransformerId(this);
     593  //       Populate(box, o, mapper);
     594  //       return box.Build();
     595  //     }
     596  //     public override object ToObject(Box box, Mapper mapper) {
     597  //       return Extract(box, mapper.GetType(box.TypeId), mapper);
     598  //     }
     599  //     private void Populate(Box.Builder box, object value, Mapper mapper) {
     600  //       var elements = (System.Collections.IEnumerable)value;
     601  //       if (value.GetType().IsGenericType) {
     602  //         var elementType = value.GetType().GetGenericTypeDefinition().GetGenericArguments().First();
     603  //         box.TypeId = mapper.GetTypeId(elementType);
     604  //       }
     605  //
     606  //       var b = UnsignedIntArrayBox.CreateBuilder();
     607  //       foreach (var elem in elements) {
     608  //         b.AddValues(mapper.GetBoxId(elem));
     609  //       }
     610  //
     611  //       box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
     612  //     }
     613  //     private object Extract(Box box, Type type, Mapper mapper) {
     614  //     }
     615  //   }
     616
     617  [Transformer("B01ADF0A-ACAA-444E-9F82-0C7C2AF1F490", 2000)]
     618  internal sealed class ArrayTransformer : Transformer {
    511619    public override bool CanTransformType(Type type) {
    512620      return type.IsArray;
    513621    }
    514     protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
    515       var arr = (object[])value;
    516       return arr.Select(x => mapper.GetBoxId(x));
    517     }
    518     protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
    519       return value.Select(x => mapper.GetObject(x)).ToArray();
     622    public override Box ToBox(object o, Mapper mapper) {
     623      var box = Box.CreateBuilder();
     624      box.TransformerId = mapper.GetTransformerId(this);
     625      Populate(box, o, mapper);
     626      return box.Build();
     627    }
     628    public override object ToObject(Box box, Mapper mapper) {
     629      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
     630    }
     631    private void Populate(Box.Builder box, object value, Mapper mapper) {
     632      var array = (object[])value;
     633      var elementType = value.GetType().GetElementType();
     634
     635      var b = UnsignedIntArrayBox.CreateBuilder();
     636      b.AddRangeValues(array.Select(x => mapper.GetBoxId(x)));
     637
     638      box.TypeId = mapper.GetBoxId(elementType);
     639      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
     640    }
     641    private object Extract(Box box, Type type, Mapper mapper) {
     642      var b = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
     643
     644      var array = (object[])Array.CreateInstance((Type)mapper.GetObject(box.TypeId), b.ValuesCount);
     645
     646      for (int i = 0; i < array.Length; i++) {
     647        array[i] = mapper.GetObject(b.GetValues(i));
     648      }
     649
     650      return array;
     651    }
     652  }
     653
     654  [Transformer("11B822C9-46A0-4B65-AE4A-D12F63DDE9F6", 1)]
     655  internal sealed class TypeTransformer : Transformer {
     656    public override bool CanTransformType(Type type) {
     657      return typeof(Type).IsAssignableFrom(type);
     658    }
     659
     660    public override Box ToBox(object o, Mapper mapper) {
     661      var box = Box.CreateBuilder();
     662      box.TransformerId = mapper.GetTransformerId(this);
     663      Populate(box, o, mapper);
     664      return box.Build();
     665    }
     666
     667    private void Populate(Box.Builder box, object o, Mapper mapper) {
     668      var type = (Type)o;
     669
     670      var b = TypeBox.CreateBuilder();
     671      if (type.IsGenericType) {
     672        box.TypeId = mapper.GetTypeId(type.GetGenericTypeDefinition());
     673        b.AddRangeGenericTypeIds(type.GetGenericArguments().Select(t => mapper.GetBoxId(t)));
     674      } else if (type.IsArray) {
     675        box.TypeId = mapper.GetTypeId(typeof(Array));
     676        b.AddGenericTypeIds(mapper.GetBoxId(type.GetElementType()));
     677      } else {
     678        box.TypeId = mapper.GetTypeId(type);
     679      }
     680      box.SetExtension(TypeBox.Type, b.Build());
     681    }
     682
     683    public override object ToObject(Box box, Mapper mapper) {
     684      return Extract(box, mapper.GetType(box.TypeId), mapper);
     685    }
     686
     687    private object Extract(Box box, Type type, Mapper mapper) {
     688      var b = box.GetExtension(TypeBox.Type);
     689      if (b.GenericTypeIdsCount > 0) {
     690        if (typeof(Array).IsAssignableFrom(type)) {
     691          return ((Type)mapper.GetObject(b.GetGenericTypeIds(0))).MakeArrayType();
     692        } else {
     693          return type.MakeGenericType(b.GenericTypeIdsList.Select(id => (Type)mapper.GetObject(id)).ToArray());
     694        }
     695      } else {
     696        return mapper.GetType(box.TypeId);
     697      }
     698    }
     699  }
     700
     701  [Transformer("4FA5EAAF-ECC7-4A9C-84E7-6583DA96F3B9", 1000)]
     702  internal sealed class EnumerableTransformer : Transformer {
     703    public override bool CanTransformType(Type type) {
     704      return (type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition()) ||
     705             type == typeof(System.Collections.Stack);
     706    }
     707    public override Box ToBox(object o, Mapper mapper) {
     708      var box = Box.CreateBuilder();
     709      box.TransformerId = mapper.GetTransformerId(this);
     710      Populate(box, o, mapper);
     711      return box.Build();
     712    }
     713    public override object ToObject(Box box, Mapper mapper) {
     714      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
     715    }
     716    private void Populate(Box.Builder box, object value, Mapper mapper) {
     717      var enumerableBox = UnsignedIntArrayBox.CreateBuilder();
     718      box.TypeId = mapper.GetBoxId(value.GetType());
     719      enumerableBox.AddRangeValues(((IEnumerable)value).OfType<object>().Select(mapper.GetBoxId));
     720
     721      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, enumerableBox.Build());
     722    }
     723    private object Extract(Box box, Type type, Mapper mapper) {
     724      var enumerableBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
     725      if ((type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition())) {
     726        var elemType = type.GenericTypeArguments[0];
     727        var stack = Activator.CreateInstance(type);
     728        var addMeth = type.GetMethod("Push");
     729        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject).Reverse())
     730          addMeth.Invoke(stack, new[] { e });
     731        return stack;
     732      } else if (typeof(Stack).IsAssignableFrom(type)) {
     733        var l = new System.Collections.ArrayList();
     734        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject)) {
     735          l.Add(e);
     736        }
     737        l.Reverse();
     738        return Activator.CreateInstance(type, l);
     739      } else {
     740        throw new NotSupportedException();
     741      }
     742    }
     743  }
     744
     745  [Transformer("C47A62F5-F113-4A43-A8EE-CF817EC799A2", 500)]
     746  internal sealed class DictionaryTransformer : Transformer {
     747    public override bool CanTransformType(Type type) {
     748      return type.IsGenericType && typeof(IDictionary<,>).IsAssignableFrom(type);
     749    }
     750    public override Box ToBox(object o, Mapper mapper) {
     751      var box = Box.CreateBuilder();
     752      box.TransformerId = mapper.GetTransformerId(this);
     753      Populate(box, o, mapper);
     754      return box.Build();
     755    }
     756    public override object ToObject(Box box, Mapper mapper) {
     757      return Extract(box, mapper.GetType(box.TypeId), mapper);
     758    }
     759    private void Populate(Box.Builder box, object value, Mapper mapper) {
     760      var dictionaryBox = DictionaryBox.CreateBuilder();
     761      foreach (DictionaryEntry item in (IDictionary)value) {
     762        dictionaryBox.AddKeyIds(mapper.GetBoxId(item.Key));
     763        dictionaryBox.AddValueIds(mapper.GetBoxId(item.Value));
     764      }
     765      // TODO: comparer
     766    }
     767    private object Extract(Box box, Type type, Mapper mapper) {
     768      throw new NotImplementedException();
    520769    }
    521770  }
Note: See TracChangeset for help on using the changeset viewer.