You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ak...@apache.org on 2015/11/12 09:04:04 UTC
[09/28] ignite git commit: IGNITE-1845: Adopted new binary API in
.Net.
http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableReflectiveSerializer.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableReflectiveSerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableReflectiveSerializer.cs
deleted file mode 100644
index 849230e..0000000
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableReflectiveSerializer.cs
+++ /dev/null
@@ -1,218 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace Apache.Ignite.Core.Impl.Portable
-{
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using Apache.Ignite.Core.Portable;
-
- /// <summary>
- /// Portable serializer which reflectively writes all fields except of ones with
- /// <see cref="System.NonSerializedAttribute"/>.
- /// <para />
- /// Note that Java platform stores dates as a difference between current time
- /// and predefined absolute UTC date. Therefore, this difference is always the
- /// same for all time zones. .Net, in contrast, stores dates as a difference
- /// between current time and some predefined date relative to the current time
- /// zone. It means that this difference will be different as you change time zones.
- /// To overcome this discrepancy Ignite always converts .Net date to UTC form
- /// before serializing and allows user to decide whether to deserialize them
- /// in UTC or local form using <c>ReadTimestamp(..., true/false)</c> methods in
- /// <see cref="IPortableReader"/> and <see cref="IPortableRawReader"/>.
- /// This serializer always read dates in UTC form. It means that if you have
- /// local date in any field/property, it will be implicitly converted to UTC
- /// form after the first serialization-deserialization cycle.
- /// </summary>
- internal class PortableReflectiveSerializer : IPortableSerializer
- {
- /** Cached binding flags. */
- private static readonly BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public |
- BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
-
- /** Cached type descriptors. */
- private readonly IDictionary<Type, Descriptor> _types = new Dictionary<Type, Descriptor>();
-
- /// <summary>
- /// Write portalbe object.
- /// </summary>
- /// <param name="obj">Object.</param>
- /// <param name="writer">Portable writer.</param>
- /// <exception cref="PortableException">Type is not registered in serializer: + type.Name</exception>
- public void WritePortable(object obj, IPortableWriter writer)
- {
- var portableMarshalAware = obj as IPortableMarshalAware;
-
- if (portableMarshalAware != null)
- portableMarshalAware.WritePortable(writer);
- else
- GetDescriptor(obj).Write(obj, writer);
- }
-
- /// <summary>
- /// Read portable object.
- /// </summary>
- /// <param name="obj">Instantiated empty object.</param>
- /// <param name="reader">Portable reader.</param>
- /// <exception cref="PortableException">Type is not registered in serializer: + type.Name</exception>
- public void ReadPortable(object obj, IPortableReader reader)
- {
- var portableMarshalAware = obj as IPortableMarshalAware;
-
- if (portableMarshalAware != null)
- portableMarshalAware.ReadPortable(reader);
- else
- GetDescriptor(obj).Read(obj, reader);
- }
-
- /// <summary>Register type.</summary>
- /// <param name="type">Type.</param>
- /// <param name="typeId">Type ID.</param>
- /// <param name="converter">Name converter.</param>
- /// <param name="idMapper">ID mapper.</param>
- public void Register(Type type, int typeId, IPortableNameMapper converter,
- IPortableIdMapper idMapper)
- {
- if (type.GetInterface(typeof(IPortableMarshalAware).Name) != null)
- return;
-
- List<FieldInfo> fields = new List<FieldInfo>();
-
- Type curType = type;
-
- while (curType != null)
- {
- foreach (FieldInfo field in curType.GetFields(Flags))
- {
- if (!field.IsNotSerialized)
- fields.Add(field);
- }
-
- curType = curType.BaseType;
- }
-
- IDictionary<int, string> idMap = new Dictionary<int, string>();
-
- foreach (FieldInfo field in fields)
- {
- string fieldName = PortableUtils.CleanFieldName(field.Name);
-
- int fieldId = PortableUtils.FieldId(typeId, fieldName, converter, idMapper);
-
- if (idMap.ContainsKey(fieldId))
- {
- throw new PortableException("Conflicting field IDs [type=" +
- type.Name + ", field1=" + idMap[fieldId] + ", field2=" + fieldName +
- ", fieldId=" + fieldId + ']');
- }
-
- idMap[fieldId] = fieldName;
- }
-
- fields.Sort(Compare);
-
- Descriptor desc = new Descriptor(fields);
-
- _types[type] = desc;
- }
-
- /// <summary>
- /// Gets the descriptor for an object.
- /// </summary>
- private Descriptor GetDescriptor(object obj)
- {
- var type = obj.GetType();
-
- Descriptor desc;
-
- if (!_types.TryGetValue(type, out desc))
- throw new PortableException("Type is not registered in serializer: " + type.Name);
-
- return desc;
- }
-
- /// <summary>
- /// Compare two FieldInfo instances.
- /// </summary>
- private static int Compare(FieldInfo info1, FieldInfo info2) {
- string name1 = PortableUtils.CleanFieldName(info1.Name);
- string name2 = PortableUtils.CleanFieldName(info2.Name);
-
- return string.Compare(name1, name2, StringComparison.OrdinalIgnoreCase);
- }
-
- /// <summary>
- /// Type descriptor.
- /// </summary>
- private class Descriptor
- {
- /** Write actions to be performed. */
- private readonly List<PortableReflectiveWriteAction> _wActions;
-
- /** Read actions to be performed. */
- private readonly List<PortableReflectiveReadAction> _rActions;
-
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="fields">Fields.</param>
- public Descriptor(List<FieldInfo> fields)
- {
- _wActions = new List<PortableReflectiveWriteAction>(fields.Count);
- _rActions = new List<PortableReflectiveReadAction>(fields.Count);
-
- foreach (FieldInfo field in fields)
- {
- PortableReflectiveWriteAction writeAction;
- PortableReflectiveReadAction readAction;
-
- PortableReflectiveActions.TypeActions(field, out writeAction, out readAction);
-
- _wActions.Add(writeAction);
- _rActions.Add(readAction);
- }
- }
-
- /// <summary>
- /// Write object.
- /// </summary>
- /// <param name="obj">Object.</param>
- /// <param name="writer">Portable writer.</param>
- public void Write(object obj, IPortableWriter writer)
- {
- int cnt = _wActions.Count;
-
- for (int i = 0; i < cnt; i++)
- _wActions[i](obj, writer);
- }
-
- /// <summary>
- /// Read object.
- /// </summary>
- /// <param name="obj">Object.</param>
- /// <param name="reader">Portable reader.</param>
- public void Read(object obj, IPortableReader reader)
- {
- int cnt = _rActions.Count;
-
- for (int i = 0; i < cnt; i++ )
- _rActions[i](obj, reader);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSurrogateTypeDescriptor.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSurrogateTypeDescriptor.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSurrogateTypeDescriptor.cs
deleted file mode 100644
index 8cd2f7c..0000000
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSurrogateTypeDescriptor.cs
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace Apache.Ignite.Core.Impl.Portable
-{
- using System;
- using System.Collections.Generic;
-
- using Apache.Ignite.Core.Impl.Portable.Structure;
- using Apache.Ignite.Core.Portable;
-
- /// <summary>
- /// Surrogate type descriptor. Used in cases when type if identified by name and
- /// is not provided in configuration.
- /// </summary>
- internal class PortableSurrogateTypeDescriptor : IPortableTypeDescriptor
- {
- /** Portable configuration. */
- private readonly PortableConfiguration _cfg;
-
- /** Type ID. */
- private readonly int _id;
-
- /** Type name. */
- private readonly string _name;
-
- /** Type structure. */
- private volatile PortableStructure _writerTypeStruct = PortableStructure.CreateEmpty();
-
- /** Type structure. */
- private PortableStructure _readerTypeStructure = PortableStructure.CreateEmpty();
-
- /** Type schema. */
- private readonly PortableObjectSchema _schema = new PortableObjectSchema();
-
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="cfg">Portable configuration.</param>
- /// <param name="id">Type ID.</param>
- public PortableSurrogateTypeDescriptor(PortableConfiguration cfg, int id)
- {
- _cfg = cfg;
- _id = id;
- }
-
- /// <summary>
- /// Constrcutor.
- /// </summary>
- /// <param name="cfg">Portable configuration.</param>
- /// <param name="name">Type name.</param>
- public PortableSurrogateTypeDescriptor(PortableConfiguration cfg, string name)
- {
- _cfg = cfg;
- _name = name;
-
- _id = PortableUtils.TypeId(name, cfg.DefaultNameMapper, cfg.DefaultIdMapper);
- }
-
- /** <inheritDoc /> */
- public Type Type
- {
- get { return null; }
- }
-
- /** <inheritDoc /> */
- public int TypeId
- {
- get { return _id; }
- }
-
- /** <inheritDoc /> */
- public string TypeName
- {
- get { return _name; }
- }
-
- /** <inheritDoc /> */
- public bool UserType
- {
- get { return true; }
- }
-
- /** <inheritDoc /> */
- public bool KeepDeserialized
- {
- get { return _cfg.DefaultKeepDeserialized; }
- }
-
- /** <inheritDoc /> */
- public IPortableNameMapper NameMapper
- {
- get { return _cfg.DefaultNameMapper; }
- }
-
- /** <inheritDoc /> */
- public IPortableIdMapper IdMapper
- {
- get { return _cfg.DefaultIdMapper; }
- }
-
- /** <inheritDoc /> */
- public IPortableSerializer Serializer
- {
- get { return _cfg.DefaultSerializer; }
- }
-
- /** <inheritDoc /> */
- public string AffinityKeyFieldName
- {
- get { return null; }
- }
-
- /** <inheritDoc /> */
- public PortableStructure WriterTypeStructure
- {
- get { return _writerTypeStruct; }
- }
-
- public PortableStructure ReaderTypeStructure
- {
- get { return _readerTypeStructure; }
- }
-
- /** <inheritDoc /> */
- public void UpdateWriteStructure(PortableStructure exp, int pathIdx, IList<PortableStructureUpdate> updates)
- {
- lock (this)
- {
- _writerTypeStruct = _writerTypeStruct.Merge(exp, pathIdx, updates);
- }
- }
-
- /** <inheritDoc /> */
- public void UpdateReadStructure(PortableStructure exp, int pathIdx, IList<PortableStructureUpdate> updates)
- {
- lock (this)
- {
- _readerTypeStructure = _readerTypeStructure.Merge(exp, pathIdx, updates);
- }
- }
-
- /** <inheritDoc /> */
- public PortableObjectSchema Schema
- {
- get { return _schema; }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemHandlers.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemHandlers.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemHandlers.cs
deleted file mode 100644
index dabe5c8..0000000
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemHandlers.cs
+++ /dev/null
@@ -1,832 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace Apache.Ignite.Core.Impl.Portable
-{
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using Apache.Ignite.Core.Impl.Common;
- using Apache.Ignite.Core.Impl.Portable.IO;
-
- /// <summary>
- /// Write delegate.
- /// </summary>
- /// <param name="writer">Write context.</param>
- /// <param name="obj">Object to write.</param>
- internal delegate void PortableSystemWriteDelegate(PortableWriterImpl writer, object obj);
-
- /**
- * <summary>Collection of predefined handlers for various system types.</summary>
- */
- internal static class PortableSystemHandlers
- {
- /** Write handlers. */
- private static volatile Dictionary<Type, PortableSystemWriteDelegate> _writeHandlers =
- new Dictionary<Type, PortableSystemWriteDelegate>();
-
- /** Mutex for write handlers update. */
- private static readonly object WriteHandlersMux = new object();
-
- /** Read handlers. */
- private static readonly IPortableSystemReader[] ReadHandlers = new IPortableSystemReader[255];
-
- /** Type ids. */
- private static readonly Dictionary<Type, byte> TypeIds = new Dictionary<Type, byte>
- {
- {typeof (bool), PortableUtils.TypeBool},
- {typeof (byte), PortableUtils.TypeByte},
- {typeof (sbyte), PortableUtils.TypeByte},
- {typeof (short), PortableUtils.TypeShort},
- {typeof (ushort), PortableUtils.TypeShort},
- {typeof (char), PortableUtils.TypeChar},
- {typeof (int), PortableUtils.TypeInt},
- {typeof (uint), PortableUtils.TypeInt},
- {typeof (long), PortableUtils.TypeLong},
- {typeof (ulong), PortableUtils.TypeLong},
- {typeof (float), PortableUtils.TypeFloat},
- {typeof (double), PortableUtils.TypeDouble},
- {typeof (string), PortableUtils.TypeString},
- {typeof (decimal), PortableUtils.TypeDecimal},
- {typeof (Guid), PortableUtils.TypeGuid},
- {typeof (Guid?), PortableUtils.TypeGuid},
- {typeof (ArrayList), PortableUtils.TypeCollection},
- {typeof (Hashtable), PortableUtils.TypeDictionary},
- {typeof (DictionaryEntry), PortableUtils.TypeMapEntry},
- {typeof (bool[]), PortableUtils.TypeArrayBool},
- {typeof (byte[]), PortableUtils.TypeArrayByte},
- {typeof (sbyte[]), PortableUtils.TypeArrayByte},
- {typeof (short[]), PortableUtils.TypeArrayShort},
- {typeof (ushort[]), PortableUtils.TypeArrayShort},
- {typeof (char[]), PortableUtils.TypeArrayChar},
- {typeof (int[]), PortableUtils.TypeArrayInt},
- {typeof (uint[]), PortableUtils.TypeArrayInt},
- {typeof (long[]), PortableUtils.TypeArrayLong},
- {typeof (ulong[]), PortableUtils.TypeArrayLong},
- {typeof (float[]), PortableUtils.TypeArrayFloat},
- {typeof (double[]), PortableUtils.TypeArrayDouble},
- {typeof (string[]), PortableUtils.TypeArrayString},
- {typeof (decimal?[]), PortableUtils.TypeArrayDecimal},
- {typeof (Guid?[]), PortableUtils.TypeArrayGuid},
- {typeof (object[]), PortableUtils.TypeArray}
- };
-
- /// <summary>
- /// Initializes the <see cref="PortableSystemHandlers"/> class.
- /// </summary>
- [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline",
- Justification = "Readability.")]
- static PortableSystemHandlers()
- {
- // 1. Primitives.
- ReadHandlers[PortableUtils.TypeBool] = new PortableSystemReader<bool>(s => s.ReadBool());
- ReadHandlers[PortableUtils.TypeByte] = new PortableSystemReader<byte>(s => s.ReadByte());
- ReadHandlers[PortableUtils.TypeShort] = new PortableSystemReader<short>(s => s.ReadShort());
- ReadHandlers[PortableUtils.TypeChar] = new PortableSystemReader<char>(s => s.ReadChar());
- ReadHandlers[PortableUtils.TypeInt] = new PortableSystemReader<int>(s => s.ReadInt());
- ReadHandlers[PortableUtils.TypeLong] = new PortableSystemReader<long>(s => s.ReadLong());
- ReadHandlers[PortableUtils.TypeFloat] = new PortableSystemReader<float>(s => s.ReadFloat());
- ReadHandlers[PortableUtils.TypeDouble] = new PortableSystemReader<double>(s => s.ReadDouble());
- ReadHandlers[PortableUtils.TypeDecimal] = new PortableSystemReader<decimal?>(PortableUtils.ReadDecimal);
-
- // 2. Date.
- ReadHandlers[PortableUtils.TypeTimestamp] = new PortableSystemReader<DateTime?>(PortableUtils.ReadTimestamp);
-
- // 3. String.
- ReadHandlers[PortableUtils.TypeString] = new PortableSystemReader<string>(PortableUtils.ReadString);
-
- // 4. Guid.
- ReadHandlers[PortableUtils.TypeGuid] = new PortableSystemReader<Guid?>(PortableUtils.ReadGuid);
-
- // 5. Primitive arrays.
- ReadHandlers[PortableUtils.TypeArrayBool] = new PortableSystemReader<bool[]>(PortableUtils.ReadBooleanArray);
-
- ReadHandlers[PortableUtils.TypeArrayByte] =
- new PortableSystemDualReader<byte[], sbyte[]>(PortableUtils.ReadByteArray, PortableUtils.ReadSbyteArray);
-
- ReadHandlers[PortableUtils.TypeArrayShort] =
- new PortableSystemDualReader<short[], ushort[]>(PortableUtils.ReadShortArray,
- PortableUtils.ReadUshortArray);
-
- ReadHandlers[PortableUtils.TypeArrayChar] =
- new PortableSystemReader<char[]>(PortableUtils.ReadCharArray);
-
- ReadHandlers[PortableUtils.TypeArrayInt] =
- new PortableSystemDualReader<int[], uint[]>(PortableUtils.ReadIntArray, PortableUtils.ReadUintArray);
-
- ReadHandlers[PortableUtils.TypeArrayLong] =
- new PortableSystemDualReader<long[], ulong[]>(PortableUtils.ReadLongArray,
- PortableUtils.ReadUlongArray);
-
- ReadHandlers[PortableUtils.TypeArrayFloat] =
- new PortableSystemReader<float[]>(PortableUtils.ReadFloatArray);
-
- ReadHandlers[PortableUtils.TypeArrayDouble] =
- new PortableSystemReader<double[]>(PortableUtils.ReadDoubleArray);
-
- ReadHandlers[PortableUtils.TypeArrayDecimal] =
- new PortableSystemReader<decimal?[]>(PortableUtils.ReadDecimalArray);
-
- // 6. Date array.
- ReadHandlers[PortableUtils.TypeArrayTimestamp] =
- new PortableSystemReader<DateTime?[]>(PortableUtils.ReadTimestampArray);
-
- // 7. String array.
- ReadHandlers[PortableUtils.TypeArrayString] = new PortableSystemTypedArrayReader<string>();
-
- // 8. Guid array.
- ReadHandlers[PortableUtils.TypeArrayGuid] = new PortableSystemTypedArrayReader<Guid?>();
-
- // 9. Array.
- ReadHandlers[PortableUtils.TypeArray] = new PortableSystemReader(ReadArray);
-
- // 11. Arbitrary collection.
- ReadHandlers[PortableUtils.TypeCollection] = new PortableSystemReader(ReadCollection);
-
- // 13. Arbitrary dictionary.
- ReadHandlers[PortableUtils.TypeDictionary] = new PortableSystemReader(ReadDictionary);
-
- // 15. Map entry.
- ReadHandlers[PortableUtils.TypeMapEntry] = new PortableSystemReader(ReadMapEntry);
-
- // 16. Enum.
- ReadHandlers[PortableUtils.TypeEnum] = new PortableSystemReader<int>(PortableUtils.ReadEnum<int>);
- ReadHandlers[PortableUtils.TypeArrayEnum] = new PortableSystemReader(ReadEnumArray);
- }
-
- /// <summary>
- /// Try getting write handler for type.
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static PortableSystemWriteDelegate GetWriteHandler(Type type)
- {
- PortableSystemWriteDelegate res;
-
- var writeHandlers0 = _writeHandlers;
-
- // Have we ever met this type?
- if (writeHandlers0 != null && writeHandlers0.TryGetValue(type, out res))
- return res;
-
- // Determine write handler for type and add it.
- res = FindWriteHandler(type);
-
- if (res != null)
- AddWriteHandler(type, res);
-
- return res;
- }
-
- /// <summary>
- /// Find write handler for type.
- /// </summary>
- /// <param name="type">Type.</param>
- /// <returns>Write handler or NULL.</returns>
- private static PortableSystemWriteDelegate FindWriteHandler(Type type)
- {
- // 1. Well-known types.
- if (type == typeof(string))
- return WriteString;
- if (type == typeof(decimal))
- return WriteDecimal;
- if (type == typeof(DateTime))
- return WriteDate;
- if (type == typeof(Guid))
- return WriteGuid;
- if (type == typeof (PortableUserObject))
- return WritePortable;
- if (type == typeof (ArrayList))
- return WriteArrayList;
- if (type == typeof(Hashtable))
- return WriteHashtable;
- if (type == typeof(DictionaryEntry))
- return WriteMapEntry;
- if (type.IsArray)
- {
- // We know how to write any array type.
- Type elemType = type.GetElementType();
-
- // Primitives.
- if (elemType == typeof (bool))
- return WriteBoolArray;
- if (elemType == typeof(byte))
- return WriteByteArray;
- if (elemType == typeof(short))
- return WriteShortArray;
- if (elemType == typeof(char))
- return WriteCharArray;
- if (elemType == typeof(int))
- return WriteIntArray;
- if (elemType == typeof(long))
- return WriteLongArray;
- if (elemType == typeof(float))
- return WriteFloatArray;
- if (elemType == typeof(double))
- return WriteDoubleArray;
- // Non-CLS primitives.
- if (elemType == typeof(sbyte))
- return WriteSbyteArray;
- if (elemType == typeof(ushort))
- return WriteUshortArray;
- if (elemType == typeof(uint))
- return WriteUintArray;
- if (elemType == typeof(ulong))
- return WriteUlongArray;
- // Special types.
- if (elemType == typeof (decimal?))
- return WriteDecimalArray;
- if (elemType == typeof(string))
- return WriteStringArray;
- if (elemType == typeof(Guid?))
- return WriteGuidArray;
- // Enums.
- if (elemType.IsEnum)
- return WriteEnumArray;
-
- // Object array.
- if (elemType == typeof (object))
- return WriteArray;
- }
-
- if (type.IsEnum)
- // We know how to write enums.
- return WriteEnum;
-
- if (type.IsSerializable)
- return WriteSerializable;
-
- return null;
- }
-
- /// <summary>
- /// Find write handler for type.
- /// </summary>
- /// <param name="type">Type.</param>
- /// <returns>Write handler or NULL.</returns>
- public static byte GetTypeId(Type type)
- {
- byte res;
-
- if (TypeIds.TryGetValue(type, out res))
- return res;
-
- if (type.IsEnum)
- return PortableUtils.TypeEnum;
-
- if (type.IsArray && type.GetElementType().IsEnum)
- return PortableUtils.TypeArrayEnum;
-
- return PortableUtils.TypeObject;
- }
-
- /// <summary>
- /// Add write handler for type.
- /// </summary>
- /// <param name="type"></param>
- /// <param name="handler"></param>
- private static void AddWriteHandler(Type type, PortableSystemWriteDelegate handler)
- {
- lock (WriteHandlersMux)
- {
- if (_writeHandlers == null)
- {
- Dictionary<Type, PortableSystemWriteDelegate> writeHandlers0 =
- new Dictionary<Type, PortableSystemWriteDelegate>();
-
- writeHandlers0[type] = handler;
-
- _writeHandlers = writeHandlers0;
- }
- else if (!_writeHandlers.ContainsKey(type))
- {
- Dictionary<Type, PortableSystemWriteDelegate> writeHandlers0 =
- new Dictionary<Type, PortableSystemWriteDelegate>(_writeHandlers);
-
- writeHandlers0[type] = handler;
-
- _writeHandlers = writeHandlers0;
- }
- }
- }
-
- /// <summary>
- /// Reads an object of predefined type.
- /// </summary>
- public static T ReadSystemType<T>(byte typeId, PortableReaderImpl ctx)
- {
- var handler = ReadHandlers[typeId];
-
- Debug.Assert(handler != null, "Cannot find predefined read handler: " + typeId);
-
- return handler.Read<T>(ctx);
- }
-
- /// <summary>
- /// Write decimal.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteDecimal(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeDecimal);
-
- PortableUtils.WriteDecimal((decimal)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write date.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteDate(PortableWriterImpl ctx, object obj)
- {
- ctx.Write(new DateTimeHolder((DateTime) obj));
- }
-
- /// <summary>
- /// Write string.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Object.</param>
- private static void WriteString(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeString);
-
- PortableUtils.WriteString((string)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write Guid.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteGuid(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeGuid);
-
- PortableUtils.WriteGuid((Guid)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write boolaen array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteBoolArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayBool);
-
- PortableUtils.WriteBooleanArray((bool[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write byte array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteByteArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayByte);
-
- PortableUtils.WriteByteArray((byte[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write sbyte array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteSbyteArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayByte);
-
- PortableUtils.WriteByteArray((byte[])(Array)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write short array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteShortArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayShort);
-
- PortableUtils.WriteShortArray((short[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write ushort array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteUshortArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayShort);
-
- PortableUtils.WriteShortArray((short[])(Array)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write char array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteCharArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayChar);
-
- PortableUtils.WriteCharArray((char[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write int array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteIntArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayInt);
-
- PortableUtils.WriteIntArray((int[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write uint array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteUintArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayInt);
-
- PortableUtils.WriteIntArray((int[])(Array)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write long array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteLongArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayLong);
-
- PortableUtils.WriteLongArray((long[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write ulong array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteUlongArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayLong);
-
- PortableUtils.WriteLongArray((long[])(Array)obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write float array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteFloatArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayFloat);
-
- PortableUtils.WriteFloatArray((float[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write double array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteDoubleArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayDouble);
-
- PortableUtils.WriteDoubleArray((double[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write decimal array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteDecimalArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayDecimal);
-
- PortableUtils.WriteDecimalArray((decimal?[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write string array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteStringArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayString);
-
- PortableUtils.WriteStringArray((string[])obj, ctx.Stream);
- }
-
- /// <summary>
- /// Write nullable GUID array.
- /// </summary>
- /// <param name="ctx">Context.</param>
- /// <param name="obj">Value.</param>
- private static void WriteGuidArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayGuid);
-
- PortableUtils.WriteGuidArray((Guid?[])obj, ctx.Stream);
- }
-
- /**
- * <summary>Write enum array.</summary>
- */
- private static void WriteEnumArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArrayEnum);
-
- PortableUtils.WriteArray((Array)obj, ctx);
- }
-
- /**
- * <summary>Write array.</summary>
- */
- private static void WriteArray(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeArray);
-
- PortableUtils.WriteArray((Array)obj, ctx);
- }
-
- /**
- * <summary>Write ArrayList.</summary>
- */
- private static void WriteArrayList(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeCollection);
-
- PortableUtils.WriteCollection((ICollection)obj, ctx, PortableUtils.CollectionArrayList);
- }
-
- /**
- * <summary>Write Hashtable.</summary>
- */
- private static void WriteHashtable(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeDictionary);
-
- PortableUtils.WriteDictionary((IDictionary)obj, ctx, PortableUtils.MapHashMap);
- }
-
- /**
- * <summary>Write map entry.</summary>
- */
- private static void WriteMapEntry(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeMapEntry);
-
- PortableUtils.WriteMapEntry(ctx, (DictionaryEntry)obj);
- }
-
- /**
- * <summary>Write portable object.</summary>
- */
- private static void WritePortable(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypePortable);
-
- PortableUtils.WritePortable(ctx.Stream, (PortableUserObject)obj);
- }
-
- /// <summary>
- /// Write enum.
- /// </summary>
- private static void WriteEnum(PortableWriterImpl ctx, object obj)
- {
- ctx.Stream.WriteByte(PortableUtils.TypeEnum);
-
- PortableUtils.WriteEnum(ctx.Stream, (Enum)obj);
- }
-
- /// <summary>
- /// Writes serializable.
- /// </summary>
- /// <param name="writer">The writer.</param>
- /// <param name="o">The object.</param>
- private static void WriteSerializable(PortableWriterImpl writer, object o)
- {
- writer.Write(new SerializableObjectHolder(o));
- }
-
- /**
- * <summary>Read enum array.</summary>
- */
- private static object ReadEnumArray(PortableReaderImpl ctx, Type type)
- {
- return PortableUtils.ReadTypedArray(ctx, true, type.GetElementType());
- }
-
- /**
- * <summary>Read array.</summary>
- */
- private static object ReadArray(PortableReaderImpl ctx, Type type)
- {
- var elemType = type.IsArray ? type.GetElementType() : typeof(object);
-
- return PortableUtils.ReadTypedArray(ctx, true, elemType);
- }
-
- /**
- * <summary>Read collection.</summary>
- */
- private static object ReadCollection(PortableReaderImpl ctx, Type type)
- {
- return PortableUtils.ReadCollection(ctx, null, null);
- }
-
- /**
- * <summary>Read dictionary.</summary>
- */
- private static object ReadDictionary(PortableReaderImpl ctx, Type type)
- {
- return PortableUtils.ReadDictionary(ctx, null);
- }
-
- /**
- * <summary>Read map entry.</summary>
- */
- private static object ReadMapEntry(PortableReaderImpl ctx, Type type)
- {
- return PortableUtils.ReadMapEntry(ctx);
- }
-
- /**
- * <summary>Add element to array list.</summary>
- * <param name="col">Array list.</param>
- * <param name="elem">Element.</param>
- */
-
-
- /**
- * <summary>Read delegate.</summary>
- * <param name="ctx">Read context.</param>
- * <param name="type">Type.</param>
- */
- private delegate object PortableSystemReadDelegate(PortableReaderImpl ctx, Type type);
-
- /// <summary>
- /// System type reader.
- /// </summary>
- private interface IPortableSystemReader
- {
- /// <summary>
- /// Reads a value of specified type from reader.
- /// </summary>
- T Read<T>(PortableReaderImpl ctx);
- }
-
- /// <summary>
- /// System type generic reader.
- /// </summary>
- private interface IPortableSystemReader<out T>
- {
- /// <summary>
- /// Reads a value of specified type from reader.
- /// </summary>
- T Read(PortableReaderImpl ctx);
- }
-
- /// <summary>
- /// Default reader with boxing.
- /// </summary>
- private class PortableSystemReader : IPortableSystemReader
- {
- /** */
- private readonly PortableSystemReadDelegate _readDelegate;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PortableSystemReader"/> class.
- /// </summary>
- /// <param name="readDelegate">The read delegate.</param>
- public PortableSystemReader(PortableSystemReadDelegate readDelegate)
- {
- Debug.Assert(readDelegate != null);
-
- _readDelegate = readDelegate;
- }
-
- /** <inheritdoc /> */
- public T Read<T>(PortableReaderImpl ctx)
- {
- return (T)_readDelegate(ctx, typeof(T));
- }
- }
-
- /// <summary>
- /// Reader without boxing.
- /// </summary>
- private class PortableSystemReader<T> : IPortableSystemReader
- {
- /** */
- private readonly Func<IPortableStream, T> _readDelegate;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PortableSystemReader{T}"/> class.
- /// </summary>
- /// <param name="readDelegate">The read delegate.</param>
- public PortableSystemReader(Func<IPortableStream, T> readDelegate)
- {
- Debug.Assert(readDelegate != null);
-
- _readDelegate = readDelegate;
- }
-
- /** <inheritdoc /> */
- public TResult Read<TResult>(PortableReaderImpl ctx)
- {
- return TypeCaster<TResult>.Cast(_readDelegate(ctx.Stream));
- }
- }
-
- /// <summary>
- /// Reader without boxing.
- /// </summary>
- private class PortableSystemTypedArrayReader<T> : IPortableSystemReader
- {
- public TResult Read<TResult>(PortableReaderImpl ctx)
- {
- return TypeCaster<TResult>.Cast(PortableUtils.ReadArray<T>(ctx, false));
- }
- }
-
- /// <summary>
- /// Reader with selection based on requested type.
- /// </summary>
- private class PortableSystemDualReader<T1, T2> : IPortableSystemReader, IPortableSystemReader<T2>
- {
- /** */
- private readonly Func<IPortableStream, T1> _readDelegate1;
-
- /** */
- private readonly Func<IPortableStream, T2> _readDelegate2;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PortableSystemDualReader{T1, T2}"/> class.
- /// </summary>
- /// <param name="readDelegate1">The read delegate1.</param>
- /// <param name="readDelegate2">The read delegate2.</param>
- public PortableSystemDualReader(Func<IPortableStream, T1> readDelegate1, Func<IPortableStream, T2> readDelegate2)
- {
- Debug.Assert(readDelegate1 != null);
- Debug.Assert(readDelegate2 != null);
-
- _readDelegate1 = readDelegate1;
- _readDelegate2 = readDelegate2;
- }
-
- /** <inheritdoc /> */
- T2 IPortableSystemReader<T2>.Read(PortableReaderImpl ctx)
- {
- return _readDelegate2(ctx.Stream);
- }
-
- /** <inheritdoc /> */
- public T Read<T>(PortableReaderImpl ctx)
- {
- // Can't use "as" because of variance.
- // For example, IPortableSystemReader<byte[]> can be cast to IPortableSystemReader<sbyte[]>, which
- // will cause incorrect behavior.
- if (typeof (T) == typeof (T2))
- return ((IPortableSystemReader<T>) this).Read(ctx);
-
- return TypeCaster<T>.Cast(_readDelegate1(ctx.Stream));
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemTypeSerializer.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemTypeSerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemTypeSerializer.cs
deleted file mode 100644
index 014955b..0000000
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableSystemTypeSerializer.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace Apache.Ignite.Core.Impl.Portable
-{
- using System;
- using System.Diagnostics;
- using Apache.Ignite.Core.Portable;
-
- /// <summary>
- /// Portable serializer for system types.
- /// </summary>
- /// <typeparam name="T">Object type.</typeparam>
- internal class PortableSystemTypeSerializer<T> : IPortableSystemTypeSerializer where T : IPortableWriteAware
- {
- /** Ctor delegate. */
- private readonly Func<PortableReaderImpl, T> _ctor;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PortableSystemTypeSerializer{T}"/> class.
- /// </summary>
- /// <param name="ctor">Constructor delegate.</param>
- public PortableSystemTypeSerializer(Func<PortableReaderImpl, T> ctor)
- {
- Debug.Assert(ctor != null);
-
- _ctor = ctor;
- }
-
- /** <inheritdoc /> */
- public void WritePortable(object obj, IPortableWriter writer)
- {
- ((T) obj).WritePortable(writer);
- }
-
- /** <inheritdoc /> */
- public void ReadPortable(object obj, IPortableReader reader)
- {
- throw new NotSupportedException("System serializer does not support ReadPortable.");
- }
-
- /** <inheritdoc /> */
- public object ReadInstance(PortableReaderImpl reader)
- {
- return _ctor(reader);
- }
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableUserObject.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableUserObject.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableUserObject.cs
deleted file mode 100644
index 43a20af..0000000
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableUserObject.cs
+++ /dev/null
@@ -1,354 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace Apache.Ignite.Core.Impl.Portable
-{
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Text;
- using Apache.Ignite.Core.Common;
- using Apache.Ignite.Core.Impl.Portable.IO;
- using Apache.Ignite.Core.Portable;
-
- /// <summary>
- /// User portable object.
- /// </summary>
- internal class PortableUserObject : IPortableObject
- {
- /** Cache empty dictionary. */
- private static readonly IDictionary<int, int> EmptyFields = new Dictionary<int, int>();
-
- /** Marshaller. */
- private readonly PortableMarshaller _marsh;
-
- /** Raw data of this portable object. */
- private readonly byte[] _data;
-
- /** Offset in data array. */
- private readonly int _offset;
-
- /** Header. */
- private readonly PortableObjectHeader _header;
-
- /** Fields. */
- private volatile IDictionary<int, int> _fields;
-
- /** Deserialized value. */
- private object _deserialized;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PortableUserObject" /> class.
- /// </summary>
- /// <param name="marsh">Marshaller.</param>
- /// <param name="data">Raw data of this portable object.</param>
- /// <param name="offset">Offset in data array.</param>
- /// <param name="header">The header.</param>
- public PortableUserObject(PortableMarshaller marsh, byte[] data, int offset, PortableObjectHeader header)
- {
- _marsh = marsh;
-
- _data = data;
- _offset = offset;
-
- _header = header;
- }
-
- /** <inheritdoc /> */
- public int TypeId
- {
- get { return _header.TypeId; }
- }
-
- /** <inheritdoc /> */
- public T GetField<T>(string fieldName)
- {
- int pos;
-
- return TryGetFieldPosition(fieldName, out pos) ? GetField<T>(pos, null) : default(T);
- }
-
- /// <summary>
- /// Gets field value on the given object.
- /// </summary>
- /// <param name="pos">Position.</param>
- /// <param name="builder">Builder.</param>
- /// <returns>Field value.</returns>
- public T GetField<T>(int pos, PortableBuilderImpl builder)
- {
- IPortableStream stream = new PortableHeapStream(_data);
-
- stream.Seek(pos + _offset, SeekOrigin.Begin);
-
- return _marsh.Unmarshal<T>(stream, PortableMode.ForcePortable, builder);
- }
-
- /** <inheritdoc /> */
- public T Deserialize<T>()
- {
- return Deserialize<T>(PortableMode.Deserialize);
- }
-
- /// <summary>
- /// Internal deserialization routine.
- /// </summary>
- /// <param name="mode">The mode.</param>
- /// <returns>
- /// Deserialized object.
- /// </returns>
- private T Deserialize<T>(PortableMode mode)
- {
- if (_deserialized == null)
- {
- IPortableStream stream = new PortableHeapStream(_data);
-
- stream.Seek(_offset, SeekOrigin.Begin);
-
- T res = _marsh.Unmarshal<T>(stream, mode);
-
- IPortableTypeDescriptor desc = _marsh.GetDescriptor(true, _header.TypeId);
-
- if (!desc.KeepDeserialized)
- return res;
-
- _deserialized = res;
- }
-
- return (T)_deserialized;
- }
-
- /** <inheritdoc /> */
- public IPortableMetadata GetMetadata()
- {
- return _marsh.GetMetadata(_header.TypeId);
- }
-
- /// <summary>
- /// Raw data of this portable object.
- /// </summary>
- public byte[] Data
- {
- get { return _data; }
- }
-
- /// <summary>
- /// Offset in data array.
- /// </summary>
- public int Offset
- {
- get { return _offset; }
- }
-
- public bool TryGetFieldPosition(string fieldName, out int pos)
- {
- var desc = _marsh.GetDescriptor(true, _header.TypeId);
-
- InitializeFields();
-
- int fieldId = PortableUtils.FieldId(_header.TypeId, fieldName, desc.NameMapper, desc.IdMapper);
-
- return _fields.TryGetValue(fieldId, out pos);
- }
-
- /// <summary>
- /// Lazy fields initialization routine.
- /// </summary>
- private void InitializeFields()
- {
- if (_fields != null)
- return;
-
- var stream = new PortableHeapStream(_data);
-
- var hdr = PortableObjectHeader.Read(stream, _offset);
-
- _fields = hdr.ReadSchemaAsDictionary(stream, _offset) ?? EmptyFields;
- }
-
- /** <inheritdoc /> */
- public override int GetHashCode()
- {
- return _header.HashCode;
- }
-
- /** <inheritdoc /> */
- public override bool Equals(object obj)
- {
- if (this == obj)
- return true;
-
- PortableUserObject that = obj as PortableUserObject;
-
- if (that != null)
- {
- if (_data == that._data && _offset == that._offset)
- return true;
-
- // 1. Check headers
- if (_header == that._header)
- {
- // 2. Check if objects have the same field sets.
- InitializeFields();
- that.InitializeFields();
-
- if (_fields.Keys.Count != that._fields.Keys.Count)
- return false;
-
- foreach (int id in _fields.Keys)
- {
- if (!that._fields.ContainsKey(id))
- return false;
- }
-
- // 3. Check if objects have the same field values.
- foreach (KeyValuePair<int, int> field in _fields)
- {
- object fieldVal = GetField<object>(field.Value, null);
- object thatFieldVal = that.GetField<object>(that._fields[field.Key], null);
-
- if (!Equals(fieldVal, thatFieldVal))
- return false;
- }
-
- // 4. Check if objects have the same raw data.
- // ReSharper disable ImpureMethodCallOnReadonlyValueField (method is not impure)
- var stream = new PortableHeapStream(_data);
- var rawOffset = _header.GetRawOffset(stream, _offset);
-
- var thatStream = new PortableHeapStream(that._data);
- var thatRawOffset = that._header.GetRawOffset(thatStream, that._offset);
- // ReSharper restore ImpureMethodCallOnReadonlyValueField
-
- return PortableUtils.CompareArrays(_data, _offset + rawOffset, _header.Length - rawOffset,
- that._data, that._offset + thatRawOffset, that._header.Length - thatRawOffset);
- }
- }
-
- return false;
- }
-
- /** <inheritdoc /> */
- public override string ToString()
- {
- return ToString(new Dictionary<int, int>());
- }
-
- /// <summary>
- /// ToString implementation.
- /// </summary>
- /// <param name="handled">Already handled objects.</param>
- /// <returns>Object string.</returns>
- private string ToString(IDictionary<int, int> handled)
- {
- int idHash;
-
- bool alreadyHandled = handled.TryGetValue(_offset, out idHash);
-
- if (!alreadyHandled)
- idHash = RuntimeHelpers.GetHashCode(this);
-
- StringBuilder sb;
-
- IPortableTypeDescriptor desc = _marsh.GetDescriptor(true, _header.TypeId);
-
- IPortableMetadata meta;
-
- try
- {
- meta = _marsh.GetMetadata(_header.TypeId);
- }
- catch (IgniteException)
- {
- meta = null;
- }
-
- if (meta == null)
- sb = new StringBuilder("PortableObject [typeId=").Append(_header.TypeId).Append(", idHash=" + idHash);
- else
- {
- sb = new StringBuilder(meta.TypeName).Append(" [idHash=" + idHash);
-
- if (!alreadyHandled)
- {
- handled[_offset] = idHash;
-
- InitializeFields();
-
- foreach (string fieldName in meta.Fields)
- {
- sb.Append(", ");
-
- int fieldId = PortableUtils.FieldId(_header.TypeId, fieldName, desc.NameMapper, desc.IdMapper);
-
- int fieldPos;
-
- if (_fields.TryGetValue(fieldId, out fieldPos))
- {
- sb.Append(fieldName).Append('=');
-
- ToString0(sb, GetField<object>(fieldPos, null), handled);
- }
- }
- }
- else
- sb.Append(", ...");
- }
-
- sb.Append(']');
-
- return sb.ToString();
- }
-
- /// <summary>
- /// Internal ToString routine with correct collections printout.
- /// </summary>
- /// <param name="sb">String builder.</param>
- /// <param name="obj">Object to print.</param>
- /// <param name="handled">Already handled objects.</param>
- /// <returns>The same string builder.</returns>
- private static void ToString0(StringBuilder sb, object obj, IDictionary<int, int> handled)
- {
- IEnumerable col = (obj is string) ? null : obj as IEnumerable;
-
- if (col == null)
- {
- PortableUserObject obj0 = obj as PortableUserObject;
-
- sb.Append(obj0 == null ? obj : obj0.ToString(handled));
- }
- else
- {
- sb.Append('[');
-
- bool first = true;
-
- foreach (object elem in col)
- {
- if (first)
- first = false;
- else
- sb.Append(", ");
-
- ToString0(sb, elem, handled);
- }
-
- sb.Append(']');
- }
- }
- }
-}