Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EFD4618C3E for ; Wed, 11 Nov 2015 09:12:48 +0000 (UTC) Received: (qmail 90626 invoked by uid 500); 11 Nov 2015 09:12:48 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 90518 invoked by uid 500); 11 Nov 2015 09:12:48 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 90418 invoked by uid 99); 11 Nov 2015 09:12:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Nov 2015 09:12:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 47178E0593; Wed, 11 Nov 2015 09:12:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: vozerov@apache.org To: commits@ignite.apache.org Date: Wed, 11 Nov 2015 09:12:51 -0000 Message-Id: <752658f1620b42218a22382ad0441f1c@git.apache.org> In-Reply-To: <81bf422c13814119918c9c27e5d01989@git.apache.org> References: <81bf422c13814119918c9c27e5d01989@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/24] 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/PortableWriterImpl.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableWriterImpl.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableWriterImpl.cs deleted file mode 100644 index ea3e368..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortableWriterImpl.cs +++ /dev/null @@ -1,1421 +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.IO; - using Apache.Ignite.Core.Impl.Portable.IO; - using Apache.Ignite.Core.Impl.Portable.Metadata; - using Apache.Ignite.Core.Impl.Portable.Structure; - using Apache.Ignite.Core.Portable; - - using PU = PortableUtils; - - /// - /// Portable writer implementation. - /// - internal class PortableWriterImpl : IPortableWriter, IPortableRawWriter - { - /** Marshaller. */ - private readonly PortableMarshaller _marsh; - - /** Stream. */ - private readonly IPortableStream _stream; - - /** Builder (used only during build). */ - private PortableBuilderImpl _builder; - - /** Handles. */ - private PortableHandleDictionary _hnds; - - /** Metadatas collected during this write session. */ - private IDictionary _metas; - - /** Current type ID. */ - private int _curTypeId; - - /** Current name converter */ - private IPortableNameMapper _curConverter; - - /** Current mapper. */ - private IPortableIdMapper _curMapper; - - /** Current object start position. */ - private int _curPos; - - /** Current raw position. */ - private int _curRawPos; - - /** Whether we are currently detaching an object. */ - private bool _detaching; - - /** Current type structure tracker, */ - private PortableStructureTracker _curStruct; - - /** Schema holder. */ - private readonly PortableObjectSchemaHolder _schema = PortableObjectSchemaHolder.Current; - - /// - /// Gets the marshaller. - /// - internal PortableMarshaller Marshaller - { - get { return _marsh; } - } - - /// - /// Write named boolean value. - /// - /// Field name. - /// Boolean value. - public void WriteBoolean(string fieldName, bool val) - { - WriteFieldId(fieldName, PU.TypeBool); - - _stream.WriteByte(PU.TypeBool); - _stream.WriteBool(val); - } - - /// - /// Write boolean value. - /// - /// Boolean value. - public void WriteBoolean(bool val) - { - _stream.WriteBool(val); - } - - /// - /// Write named boolean array. - /// - /// Field name. - /// Boolean array. - public void WriteBooleanArray(string fieldName, bool[] val) - { - WriteFieldId(fieldName, PU.TypeArrayBool); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayBool); - PU.WriteBooleanArray(val, _stream); - } - } - - /// - /// Write boolean array. - /// - /// Boolean array. - public void WriteBooleanArray(bool[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayBool); - PU.WriteBooleanArray(val, _stream); - } - } - - /// - /// Write named byte value. - /// - /// Field name. - /// Byte value. - public void WriteByte(string fieldName, byte val) - { - WriteFieldId(fieldName, PU.TypeBool); - - _stream.WriteByte(PU.TypeByte); - _stream.WriteByte(val); - } - - /// - /// Write byte value. - /// - /// Byte value. - public void WriteByte(byte val) - { - _stream.WriteByte(val); - } - - /// - /// Write named byte array. - /// - /// Field name. - /// Byte array. - public void WriteByteArray(string fieldName, byte[] val) - { - WriteFieldId(fieldName, PU.TypeArrayByte); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayByte); - PU.WriteByteArray(val, _stream); - } - } - - /// - /// Write byte array. - /// - /// Byte array. - public void WriteByteArray(byte[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayByte); - PU.WriteByteArray(val, _stream); - } - } - - /// - /// Write named short value. - /// - /// Field name. - /// Short value. - public void WriteShort(string fieldName, short val) - { - WriteFieldId(fieldName, PU.TypeShort); - - _stream.WriteByte(PU.TypeShort); - _stream.WriteShort(val); - } - - /// - /// Write short value. - /// - /// Short value. - public void WriteShort(short val) - { - _stream.WriteShort(val); - } - - /// - /// Write named short array. - /// - /// Field name. - /// Short array. - public void WriteShortArray(string fieldName, short[] val) - { - WriteFieldId(fieldName, PU.TypeArrayShort); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayShort); - PU.WriteShortArray(val, _stream); - } - } - - /// - /// Write short array. - /// - /// Short array. - public void WriteShortArray(short[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayShort); - PU.WriteShortArray(val, _stream); - } - } - - /// - /// Write named char value. - /// - /// Field name. - /// Char value. - public void WriteChar(string fieldName, char val) - { - WriteFieldId(fieldName, PU.TypeChar); - - _stream.WriteByte(PU.TypeChar); - _stream.WriteChar(val); - } - - /// - /// Write char value. - /// - /// Char value. - public void WriteChar(char val) - { - _stream.WriteChar(val); - } - - /// - /// Write named char array. - /// - /// Field name. - /// Char array. - public void WriteCharArray(string fieldName, char[] val) - { - WriteFieldId(fieldName, PU.TypeArrayChar); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayChar); - PU.WriteCharArray(val, _stream); - } - } - - /// - /// Write char array. - /// - /// Char array. - public void WriteCharArray(char[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayChar); - PU.WriteCharArray(val, _stream); - } - } - - /// - /// Write named int value. - /// - /// Field name. - /// Int value. - public void WriteInt(string fieldName, int val) - { - WriteFieldId(fieldName, PU.TypeInt); - - _stream.WriteByte(PU.TypeInt); - _stream.WriteInt(val); - } - - /// - /// Write int value. - /// - /// Int value. - public void WriteInt(int val) - { - _stream.WriteInt(val); - } - - /// - /// Write named int array. - /// - /// Field name. - /// Int array. - public void WriteIntArray(string fieldName, int[] val) - { - WriteFieldId(fieldName, PU.TypeArrayInt); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayInt); - PU.WriteIntArray(val, _stream); - } - } - - /// - /// Write int array. - /// - /// Int array. - public void WriteIntArray(int[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayInt); - PU.WriteIntArray(val, _stream); - } - } - - /// - /// Write named long value. - /// - /// Field name. - /// Long value. - public void WriteLong(string fieldName, long val) - { - WriteFieldId(fieldName, PU.TypeLong); - - _stream.WriteByte(PU.TypeLong); - _stream.WriteLong(val); - } - - /// - /// Write long value. - /// - /// Long value. - public void WriteLong(long val) - { - _stream.WriteLong(val); - } - - /// - /// Write named long array. - /// - /// Field name. - /// Long array. - public void WriteLongArray(string fieldName, long[] val) - { - WriteFieldId(fieldName, PU.TypeArrayLong); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayLong); - PU.WriteLongArray(val, _stream); - } - } - - /// - /// Write long array. - /// - /// Long array. - public void WriteLongArray(long[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayLong); - PU.WriteLongArray(val, _stream); - } - } - - /// - /// Write named float value. - /// - /// Field name. - /// Float value. - public void WriteFloat(string fieldName, float val) - { - WriteFieldId(fieldName, PU.TypeFloat); - - _stream.WriteByte(PU.TypeFloat); - _stream.WriteFloat(val); - } - - /// - /// Write float value. - /// - /// Float value. - public void WriteFloat(float val) - { - _stream.WriteFloat(val); - } - - /// - /// Write named float array. - /// - /// Field name. - /// Float array. - public void WriteFloatArray(string fieldName, float[] val) - { - WriteFieldId(fieldName, PU.TypeArrayFloat); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayFloat); - PU.WriteFloatArray(val, _stream); - } - } - - /// - /// Write float array. - /// - /// Float array. - public void WriteFloatArray(float[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayFloat); - PU.WriteFloatArray(val, _stream); - } - } - - /// - /// Write named double value. - /// - /// Field name. - /// Double value. - public void WriteDouble(string fieldName, double val) - { - WriteFieldId(fieldName, PU.TypeDouble); - - _stream.WriteByte(PU.TypeDouble); - _stream.WriteDouble(val); - } - - /// - /// Write double value. - /// - /// Double value. - public void WriteDouble(double val) - { - _stream.WriteDouble(val); - } - - /// - /// Write named double array. - /// - /// Field name. - /// Double array. - public void WriteDoubleArray(string fieldName, double[] val) - { - WriteFieldId(fieldName, PU.TypeArrayDouble); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayDouble); - PU.WriteDoubleArray(val, _stream); - } - } - - /// - /// Write double array. - /// - /// Double array. - public void WriteDoubleArray(double[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayDouble); - PU.WriteDoubleArray(val, _stream); - } - } - - /// - /// Write named decimal value. - /// - /// Field name. - /// Decimal value. - public void WriteDecimal(string fieldName, decimal? val) - { - WriteFieldId(fieldName, PU.TypeDecimal); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeDecimal); - PortableUtils.WriteDecimal(val.Value, _stream); - } - } - - /// - /// Write decimal value. - /// - /// Decimal value. - public void WriteDecimal(decimal? val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeDecimal); - PortableUtils.WriteDecimal(val.Value, _stream); - } - } - - /// - /// Write named decimal array. - /// - /// Field name. - /// Decimal array. - public void WriteDecimalArray(string fieldName, decimal?[] val) - { - WriteFieldId(fieldName, PU.TypeArrayDecimal); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayDecimal); - PU.WriteDecimalArray(val, _stream); - } - } - - /// - /// Write decimal array. - /// - /// Decimal array. - public void WriteDecimalArray(decimal?[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayDecimal); - PU.WriteDecimalArray(val, _stream); - } - } - - /// - /// Write named date value. - /// - /// Field name. - /// Date value. - public void WriteTimestamp(string fieldName, DateTime? val) - { - WriteFieldId(fieldName, PU.TypeTimestamp); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PortableUtils.TypeTimestamp); - PortableUtils.WriteTimestamp(val.Value, _stream); - } - } - - /// - /// Write date value. - /// - /// Date value. - public void WriteTimestamp(DateTime? val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PortableUtils.TypeTimestamp); - PortableUtils.WriteTimestamp(val.Value, _stream); - } - } - - /// - /// Write named date array. - /// - /// Field name. - /// Date array. - public void WriteTimestampArray(string fieldName, DateTime?[] val) - { - WriteFieldId(fieldName, PU.TypeTimestamp); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PortableUtils.TypeArrayTimestamp); - PortableUtils.WriteTimestampArray(val, _stream); - } - } - - /// - /// Write date array. - /// - /// Date array. - public void WriteTimestampArray(DateTime?[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PortableUtils.TypeArrayTimestamp); - PortableUtils.WriteTimestampArray(val, _stream); - } - } - - /// - /// Write named string value. - /// - /// Field name. - /// String value. - public void WriteString(string fieldName, string val) - { - WriteFieldId(fieldName, PU.TypeString); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeString); - PU.WriteString(val, _stream); - } - } - - /// - /// Write string value. - /// - /// String value. - public void WriteString(string val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeString); - PU.WriteString(val, _stream); - } - } - - /// - /// Write named string array. - /// - /// Field name. - /// String array. - public void WriteStringArray(string fieldName, string[] val) - { - WriteFieldId(fieldName, PU.TypeArrayString); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayString); - PU.WriteStringArray(val, _stream); - } - } - - /// - /// Write string array. - /// - /// String array. - public void WriteStringArray(string[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayString); - PU.WriteStringArray(val, _stream); - } - } - - /// - /// Write named GUID value. - /// - /// Field name. - /// GUID value. - public void WriteGuid(string fieldName, Guid? val) - { - WriteFieldId(fieldName, PU.TypeGuid); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeGuid); - PU.WriteGuid(val.Value, _stream); - } - } - - /// - /// Write GUID value. - /// - /// GUID value. - public void WriteGuid(Guid? val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeGuid); - PU.WriteGuid(val.Value, _stream); - } - } - - /// - /// Write named GUID array. - /// - /// Field name. - /// GUID array. - public void WriteGuidArray(string fieldName, Guid?[] val) - { - WriteFieldId(fieldName, PU.TypeArrayGuid); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayGuid); - PU.WriteGuidArray(val, _stream); - } - } - - /// - /// Write GUID array. - /// - /// GUID array. - public void WriteGuidArray(Guid?[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayGuid); - PU.WriteGuidArray(val, _stream); - } - } - - /// - /// Write named enum value. - /// - /// - /// Field name. - /// Enum value. - public void WriteEnum(string fieldName, T val) - { - WriteFieldId(fieldName, PU.TypeEnum); - - _stream.WriteByte(PU.TypeEnum); - PortableUtils.WriteEnum(_stream, (Enum)(object)val); - } - - /// - /// Write enum value. - /// - /// - /// Enum value. - public void WriteEnum(T val) - { - _stream.WriteByte(PU.TypeEnum); - PortableUtils.WriteEnum(_stream, (Enum)(object)val); - } - - /// - /// Write named enum array. - /// - /// - /// Field name. - /// Enum array. - public void WriteEnumArray(string fieldName, T[] val) - { - WriteFieldId(fieldName, PU.TypeArrayEnum); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArrayEnum); - PortableUtils.WriteArray(val, this); - } - } - - /// - /// Write enum array. - /// - /// - /// Enum array. - public void WriteEnumArray(T[] val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArrayEnum); - PortableUtils.WriteArray(val, this); - } - } - - /// - /// Write named object value. - /// - /// - /// Field name. - /// Object value. - public void WriteObject(string fieldName, T val) - { - WriteFieldId(fieldName, PU.TypeObject); - - if (val == null) - WriteNullField(); - else - Write(val); - } - - /// - /// Write object value. - /// - /// - /// Object value. - public void WriteObject(T val) - { - Write(val); - } - - /// - /// Write named object array. - /// - /// Element type. - /// Field name. - /// Object array. - public void WriteArray(string fieldName, T[] val) - { - WriteFieldId(fieldName, PU.TypeArray); - - if (val == null) - WriteNullField(); - else - { - _stream.WriteByte(PU.TypeArray); - PortableUtils.WriteArray(val, this); - } - } - - /// - /// Write object array. - /// - /// Element type. - /// Object array. - public void WriteArray(T[] val) - { - WriteArrayInternal(val); - } - - /// - /// Write object array. - /// - /// Object array. - public void WriteArrayInternal(Array val) - { - if (val == null) - WriteNullRawField(); - else - { - _stream.WriteByte(PU.TypeArray); - PortableUtils.WriteArray(val, this); - } - } - - /// - /// Write named collection. - /// - /// Field name. - /// Collection. - public void WriteCollection(string fieldName, ICollection val) - { - WriteFieldId(fieldName, PU.TypeCollection); - - if (val == null) - WriteNullField(); - else - WriteCollection(val); - } - - /// - /// Write collection. - /// - /// Collection. - public void WriteCollection(ICollection val) - { - WriteByte(PU.TypeCollection); - PU.WriteCollection(val, this); - } - - /// - /// Write named dictionary. - /// - /// Field name. - /// Dictionary. - public void WriteDictionary(string fieldName, IDictionary val) - { - WriteFieldId(fieldName, PU.TypeDictionary); - - if (val == null) - WriteNullField(); - else - WriteDictionary(val); - } - - /// - /// Write dictionary. - /// - /// Dictionary. - public void WriteDictionary(IDictionary val) - { - WriteByte(PU.TypeDictionary); - PU.WriteDictionary(val, this); - } - - /// - /// Write NULL field. - /// - private void WriteNullField() - { - _stream.WriteByte(PU.HdrNull); - } - - /// - /// Write NULL raw field. - /// - private void WriteNullRawField() - { - _stream.WriteByte(PU.HdrNull); - } - - /// - /// Get raw writer. - /// - /// - /// Raw writer. - /// - public IPortableRawWriter GetRawWriter() - { - if (_curRawPos == 0) - _curRawPos = _stream.Position; - - return this; - } - - /// - /// Set new builder. - /// - /// Builder. - /// Previous builder. - internal PortableBuilderImpl SetBuilder(PortableBuilderImpl builder) - { - PortableBuilderImpl ret = _builder; - - _builder = builder; - - return ret; - } - - /// - /// Constructor. - /// - /// Marshaller. - /// Stream. - internal PortableWriterImpl(PortableMarshaller marsh, IPortableStream stream) - { - _marsh = marsh; - _stream = stream; - } - - /// - /// Write object. - /// - /// Object. - public void Write(T obj) - { - // Handle special case for null. - if (obj == null) - { - _stream.WriteByte(PU.HdrNull); - - return; - } - - // We use GetType() of a real object instead of typeof(T) to take advantage of - // automatic Nullable'1 unwrapping. - Type type = obj.GetType(); - - // Handle common case when primitive is written. - if (type.IsPrimitive) - { - WritePrimitive(obj, type); - - return; - } - - // Handle special case for builder. - if (WriteBuilderSpecials(obj)) - return; - - // Suppose that we faced normal object and perform descriptor lookup. - IPortableTypeDescriptor desc = _marsh.GetDescriptor(type); - - if (desc != null) - { - // Writing normal object. - var pos = _stream.Position; - - // Dealing with handles. - if (!(desc.Serializer is IPortableSystemTypeSerializer) && WriteHandle(pos, obj)) - return; - - // Skip header length as not everything is known now - _stream.Seek(PortableObjectHeader.Size, SeekOrigin.Current); - - // Preserve old frame. - int oldTypeId = _curTypeId; - IPortableNameMapper oldConverter = _curConverter; - IPortableIdMapper oldMapper = _curMapper; - int oldRawPos = _curRawPos; - var oldPos = _curPos; - - var oldStruct = _curStruct; - - // Push new frame. - _curTypeId = desc.TypeId; - _curConverter = desc.NameMapper; - _curMapper = desc.IdMapper; - _curRawPos = 0; - _curPos = pos; - - _curStruct = new PortableStructureTracker(desc, desc.WriterTypeStructure); - var schemaIdx = _schema.PushSchema(); - - try - { - // Write object fields. - desc.Serializer.WritePortable(obj, this); - - // Write schema - var schemaOffset = _stream.Position - pos; - - int schemaId; - short flags; - var hasSchema = _schema.WriteSchema(_stream, schemaIdx, out schemaId, out flags); - - if (!hasSchema) - schemaOffset = PortableObjectHeader.Size; - - // Calculate and write header. - if (hasSchema && _curRawPos > 0) - _stream.WriteInt(_curRawPos - pos); // raw offset is in the last 4 bytes - - var len = _stream.Position - pos; - - var header = new PortableObjectHeader(desc.UserType, desc.TypeId, obj.GetHashCode(), len, - schemaId, schemaOffset, !hasSchema, flags); - - PortableObjectHeader.Write(header, _stream, pos); - - Stream.Seek(pos + len, SeekOrigin.Begin); // Seek to the end - } - finally - { - _schema.PopSchema(schemaIdx); - } - - // Apply structure updates if any. - _curStruct.UpdateWriterStructure(this); - - // Restore old frame. - _curTypeId = oldTypeId; - _curConverter = oldConverter; - _curMapper = oldMapper; - _curRawPos = oldRawPos; - _curPos = oldPos; - - _curStruct = oldStruct; - } - else - { - // Are we dealing with a well-known type? - var handler = PortableSystemHandlers.GetWriteHandler(type); - - if (handler == null) // We did our best, object cannot be marshalled. - throw new PortableException("Unsupported object type [type=" + type + ", object=" + obj + ']'); - - handler(this, obj); - } - } - - /// - /// Write primitive type. - /// - /// Object. - /// Type. - private unsafe void WritePrimitive(T val, Type type) - { - // .Net defines 14 primitive types. We support 12 - excluding IntPtr and UIntPtr. - // Types check sequence is designed to minimize comparisons for the most frequent types. - - if (type == typeof(int)) - { - _stream.WriteByte(PU.TypeInt); - _stream.WriteInt((int)(object)val); - } - else if (type == typeof(long)) - { - _stream.WriteByte(PU.TypeLong); - _stream.WriteLong((long)(object)val); - } - else if (type == typeof(bool)) - { - _stream.WriteByte(PU.TypeBool); - _stream.WriteBool((bool)(object)val); - } - else if (type == typeof(byte)) - { - _stream.WriteByte(PU.TypeByte); - _stream.WriteByte((byte)(object)val); - } - else if (type == typeof(short)) - { - _stream.WriteByte(PU.TypeShort); - _stream.WriteShort((short)(object)val); - } - else if (type == typeof (char)) - { - _stream.WriteByte(PU.TypeChar); - _stream.WriteChar((char)(object)val); - } - else if (type == typeof(float)) - { - _stream.WriteByte(PU.TypeFloat); - _stream.WriteFloat((float)(object)val); - } - else if (type == typeof(double)) - { - _stream.WriteByte(PU.TypeDouble); - _stream.WriteDouble((double)(object)val); - } - else if (type == typeof(sbyte)) - { - sbyte val0 = (sbyte)(object)val; - - _stream.WriteByte(PU.TypeByte); - _stream.WriteByte(*(byte*)&val0); - } - else if (type == typeof(ushort)) - { - ushort val0 = (ushort)(object)val; - - _stream.WriteByte(PU.TypeShort); - _stream.WriteShort(*(short*)&val0); - } - else if (type == typeof(uint)) - { - uint val0 = (uint)(object)val; - - _stream.WriteByte(PU.TypeInt); - _stream.WriteInt(*(int*)&val0); - } - else if (type == typeof(ulong)) - { - ulong val0 = (ulong)(object)val; - - _stream.WriteByte(PU.TypeLong); - _stream.WriteLong(*(long*)&val0); - } - else - throw new PortableException("Unsupported object type [type=" + type.FullName + ", object=" + val + ']'); - } - - /// - /// Try writing object as special builder type. - /// - /// Object. - /// True if object was written, false otherwise. - private bool WriteBuilderSpecials(T obj) - { - if (_builder != null) - { - // Special case for portable object during build. - PortableUserObject portObj = obj as PortableUserObject; - - if (portObj != null) - { - if (!WriteHandle(_stream.Position, portObj)) - _builder.ProcessPortable(_stream, portObj); - - return true; - } - - // Special case for builder during build. - PortableBuilderImpl portBuilder = obj as PortableBuilderImpl; - - if (portBuilder != null) - { - if (!WriteHandle(_stream.Position, portBuilder)) - _builder.ProcessBuilder(_stream, portBuilder); - - return true; - } - } - - return false; - } - - /// - /// Add handle to handles map. - /// - /// Position in stream. - /// Object. - /// true if object was written as handle. - private bool WriteHandle(long pos, object obj) - { - if (_hnds == null) - { - // Cache absolute handle position. - _hnds = new PortableHandleDictionary(obj, pos); - - return false; - } - - long hndPos; - - if (!_hnds.TryGetValue(obj, out hndPos)) - { - // Cache absolute handle position. - _hnds.Add(obj, pos); - - return false; - } - - _stream.WriteByte(PU.HdrHnd); - - // Handle is written as difference between position before header and handle position. - _stream.WriteInt((int)(pos - hndPos)); - - return true; - } - - /// - /// Perform action with detached semantics. - /// - /// - internal void WithDetach(Action a) - { - if (_detaching) - a(this); - else - { - _detaching = true; - - PortableHandleDictionary oldHnds = _hnds; - _hnds = null; - - try - { - a(this); - } - finally - { - _detaching = false; - - if (oldHnds != null) - { - // Merge newly recorded handles with old ones and restore old on the stack. - // Otherwise we can use current handles right away. - if (_hnds != null) - oldHnds.Merge(_hnds); - - _hnds = oldHnds; - } - } - } - } - - /// - /// Stream. - /// - internal IPortableStream Stream - { - get { return _stream; } - } - - /// - /// Gets collected metadatas. - /// - /// Collected metadatas (if any). - internal IDictionary Metadata() - { - return _metas; - } - - /// - /// Check whether the given object is portable, i.e. it can be - /// serialized with portable marshaller. - /// - /// Object. - /// True if portable. - internal bool IsPortable(object obj) - { - if (obj != null) - { - Type type = obj.GetType(); - - // We assume object as portable only in case it has descriptor. - // Collections, Enums and non-primitive arrays do not have descriptors - // and this is fine here because we cannot know whether their members - // are portable. - return _marsh.GetDescriptor(type) != null || PortableSystemHandlers.GetWriteHandler(type) != null; - } - - return true; - } - - /// - /// Write field ID. - /// - /// Field name. - /// Field type ID. - private void WriteFieldId(string fieldName, byte fieldTypeId) - { - if (_curRawPos != 0) - throw new PortableException("Cannot write named fields after raw data is written."); - - var fieldId = _curStruct.GetFieldId(fieldName, fieldTypeId); - - _schema.PushField(fieldId, _stream.Position - _curPos); - } - - /// - /// Saves metadata for this session. - /// - /// Type ID. - /// Type name. - /// Affinity key field name. - /// Fields metadata. - internal void SaveMetadata(int typeId, string typeName, string affKeyFieldName, IDictionary fields) - { - if (_metas == null) - { - PortableMetadataImpl meta = - new PortableMetadataImpl(typeId, typeName, fields, affKeyFieldName); - - _metas = new Dictionary(1); - - _metas[typeId] = meta; - } - else - { - IPortableMetadata meta; - - if (_metas.TryGetValue(typeId, out meta)) - { - IDictionary existingFields = ((PortableMetadataImpl)meta).FieldsMap(); - - foreach (KeyValuePair field in fields) - { - if (!existingFields.ContainsKey(field.Key)) - existingFields[field.Key] = field.Value; - } - } - else - _metas[typeId] = new PortableMetadataImpl(typeId, typeName, fields, affKeyFieldName); - } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortablesImpl.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortablesImpl.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortablesImpl.cs deleted file mode 100644 index e72ffac..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/PortablesImpl.cs +++ /dev/null @@ -1,191 +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.IO; - using Apache.Ignite.Core.Common; - using Apache.Ignite.Core.Impl.Common; - using Apache.Ignite.Core.Impl.Portable.IO; - using Apache.Ignite.Core.Portable; - - /// - /// Portables implementation. - /// - internal class PortablesImpl : IPortables - { - /** Owning grid. */ - private readonly PortableMarshaller _marsh; - - /// - /// Constructor. - /// - /// Marshaller. - internal PortablesImpl(PortableMarshaller marsh) - { - _marsh = marsh; - } - - /** */ - public T ToPortable(object obj) - { - if (obj is IPortableObject) - return (T)obj; - - IPortableStream stream = new PortableHeapStream(1024); - - // Serialize. - PortableWriterImpl writer = _marsh.StartMarshal(stream); - - try - { - writer.Write(obj); - } - finally - { - // Save metadata. - _marsh.FinishMarshal(writer); - } - - // Deserialize. - stream.Seek(0, SeekOrigin.Begin); - - return _marsh.Unmarshal(stream, PortableMode.ForcePortable); - } - - /** */ - public IPortableBuilder GetBuilder(Type type) - { - IgniteArgumentCheck.NotNull(type, "type"); - - IPortableTypeDescriptor desc = _marsh.GetDescriptor(type); - - if (desc == null) - throw new IgniteException("Type is not portable (add it to PortableConfiguration): " + - type.FullName); - - return Builder0(null, PortableFromDescriptor(desc), desc); - } - - /** */ - public IPortableBuilder GetBuilder(string typeName) - { - IgniteArgumentCheck.NotNullOrEmpty(typeName, "typeName"); - - IPortableTypeDescriptor desc = _marsh.GetDescriptor(typeName); - - return Builder0(null, PortableFromDescriptor(desc), desc); - } - - /** */ - public IPortableBuilder GetBuilder(IPortableObject obj) - { - IgniteArgumentCheck.NotNull(obj, "obj"); - - PortableUserObject obj0 = obj as PortableUserObject; - - if (obj0 == null) - throw new ArgumentException("Unsupported object type: " + obj.GetType()); - - IPortableTypeDescriptor desc = _marsh.GetDescriptor(true, obj0.TypeId); - - return Builder0(null, obj0, desc); - } - - /** */ - public int GetTypeId(string typeName) - { - IgniteArgumentCheck.NotNullOrEmpty(typeName, "typeName"); - - return Marshaller.GetDescriptor(typeName).TypeId; - } - - /** */ - public ICollection GetMetadata() - { - return Marshaller.Ignite.ClusterGroup.Metadata(); - } - - /** */ - public IPortableMetadata GetMetadata(int typeId) - { - return Marshaller.GetMetadata(typeId); - } - - /** */ - public IPortableMetadata GetMetadata(string typeName) - { - IgniteArgumentCheck.NotNullOrEmpty(typeName, "typeName"); - - return GetMetadata(GetTypeId(typeName)); - } - - /** */ - public IPortableMetadata GetMetadata(Type type) - { - IgniteArgumentCheck.NotNull(type, "type"); - - var desc = Marshaller.GetDescriptor(type); - - return desc == null ? null : Marshaller.GetMetadata(desc.TypeId); - } - - /// - /// Marshaller. - /// - internal PortableMarshaller Marshaller - { - get - { - return _marsh; - } - } - - /// - /// Create empty portable object from descriptor. - /// - /// Descriptor. - /// Empty portable object. - private PortableUserObject PortableFromDescriptor(IPortableTypeDescriptor desc) - { - var len = PortableObjectHeader.Size; - - var hdr = new PortableObjectHeader(desc.UserType, desc.TypeId, 0, len, 0, len, true, 0); - - var stream = new PortableHeapStream(len); - - PortableObjectHeader.Write(hdr, stream, 0); - - return new PortableUserObject(_marsh, stream.InternalArray, 0, hdr); - } - - /// - /// Internal builder creation routine. - /// - /// Parent builder. - /// Portable object. - /// Type descriptor. - /// Builder. - private PortableBuilderImpl Builder0(PortableBuilderImpl parent, PortableUserObject obj, - IPortableTypeDescriptor desc) - { - return new PortableBuilderImpl(this, parent, obj, desc); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/SerializableObjectHolder.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/SerializableObjectHolder.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/SerializableObjectHolder.cs deleted file mode 100644 index a33ea24..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/SerializableObjectHolder.cs +++ /dev/null @@ -1,73 +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.Diagnostics; - using System.Runtime.Serialization.Formatters.Binary; - using Apache.Ignite.Core.Impl.Portable.IO; - using Apache.Ignite.Core.Portable; - - /// - /// Wraps Serializable item in a portable. - /// - internal class SerializableObjectHolder : IPortableWriteAware - { - /** */ - private readonly object _item; - - /// - /// Initializes a new instance of the class. - /// - /// The item to wrap. - public SerializableObjectHolder(object item) - { - _item = item; - } - - /// - /// Gets the item to wrap. - /// - public object Item - { - get { return _item; } - } - - /** */ - public void WritePortable(IPortableWriter writer) - { - Debug.Assert(writer != null); - - var writer0 = (PortableWriterImpl)writer.GetRawWriter(); - - writer0.WithDetach(w => new BinaryFormatter().Serialize(new PortableStreamAdapter(w.Stream), Item)); - } - - /// - /// Initializes a new instance of the class. - /// - /// The reader. - public SerializableObjectHolder(IPortableReader reader) - { - Debug.Assert(reader != null); - - var reader0 = (PortableReaderImpl) reader.GetRawReader(); - - _item = new BinaryFormatter().Deserialize(new PortableStreamAdapter(reader0.Stream), null); - } - } -} \ 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/Structure/PortableStructure.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructure.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructure.cs deleted file mode 100644 index 5b97ef7..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructure.cs +++ /dev/null @@ -1,333 +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.Structure -{ - using System; - using System.Collections.Generic; - using System.Diagnostics; - - using Apache.Ignite.Core.Portable; - - /// - /// Portable type structure. Cache field IDs and metadata to improve marshalling performance. - /// Every object write contains a set of field writes. Every unique ordered set of written fields - /// produce write "path". We cache these paths allowing for very fast traverse over object structure - /// without expensive map lookups and field ID calculations. - /// - internal class PortableStructure - { - /// - /// Create empty type structure. - /// - /// Empty type structure. - public static PortableStructure CreateEmpty() - { - return new PortableStructure(new[] { new PortableStructureEntry[0] }, - new PortableStructureJumpTable[1], new Dictionary()); - } - - /** Entries. */ - private readonly PortableStructureEntry[][] _paths; - - /** Jumps. */ - private readonly PortableStructureJumpTable[] _jumps; - - /** Field types. */ - private readonly IDictionary _fieldTypes; - - /// - /// Constructor. - /// - /// Paths. - /// Jumps. - /// Field types. - private PortableStructure(PortableStructureEntry[][] paths, - PortableStructureJumpTable[] jumps, IDictionary fieldTypes) - { - _paths = paths; - _jumps = jumps; - _fieldTypes = fieldTypes; - } - - /// - /// Gets field ID if possible. - /// - /// Field name. - /// Field type. - /// Path index, changes during jumps. - /// Action index. - /// Field ID or zero in case there are no matching path. - public int GetFieldId(string fieldName, byte fieldType, ref int pathIdx, int actionIdx) - { - Debug.Assert(pathIdx <= _paths.Length); - - // Get path. - PortableStructureEntry[] path = _paths[pathIdx]; - - if (actionIdx < path.Length) - { - // Get entry matching the action index. - PortableStructureEntry entry = path[actionIdx]; - - if (entry.IsExpected(fieldName, fieldType)) - // Entry matches our expectations, return. - return entry.Id; - else if (entry.IsJumpTable) - { - // Entry is a pointer to a jump table. - Debug.Assert(entry.Id < _jumps.Length); - - PortableStructureJumpTable jmpTbl = _jumps[entry.Id]; - - int pathIdx0 = jmpTbl.GetPathIndex(fieldName); - - if (pathIdx0 < 0) - return 0; - - Debug.Assert(pathIdx0 < _paths.Length); - - entry = _paths[pathIdx0][actionIdx]; - - entry.ValidateType(fieldType); - - pathIdx = pathIdx0; - - return entry.Id; - } - } - - // Failed to find anything because this is a new field. - return 0; - } - - /// - /// Merge updates into a new type structure. - /// - /// Expected type structure to apply updates to - /// Path index. - /// Updates. - /// New type structure with updates. - public PortableStructure Merge(PortableStructure exp, int pathIdx, - IList updates) - { - if (updates.Count == 0) - return this; - - // Algorithm ensures that updates are applied to the same type structure, - // where they were initially observed. This allow us to keep structure - // internals simpler and more efficient. On the other hand, this imposes - // some performance hit because in case of concurrent update, recorded - // changes will be discarded and recorded again during the next write - // on the same path. This should occur only during application warmup. - - // Note that field types are merged anyway to avoid metadata clashes. - PortableStructure res = MergeFieldTypes(updates); - - if (ReferenceEquals(exp, this)) - { - PortableStructureUpdate firstUpdate = updates[0]; - - if (firstUpdate.Index == 0) - { - // Special case: the very first structure update. Simply attach all updates. - Debug.Assert(_paths.Length == 1); - Debug.Assert(_paths[0].Length == 0); - Debug.Assert(pathIdx == 0); - - var newPaths = CopyPaths(updates.Count, 0); - - ApplyUpdatesToPath(newPaths[0], updates); - - res = new PortableStructure(newPaths, _jumps, res._fieldTypes); - } - else - { - // Get entry where updates should start. - PortableStructureEntry[] path = _paths[pathIdx]; - - PortableStructureEntry startEntry = default(PortableStructureEntry); - - if (firstUpdate.Index < path.Length) - startEntry = path[firstUpdate.Index]; - - if (startEntry.IsEmpty) - { - // We are on the empty/non-existent entry. Continue the path without branching. - var newPaths = CopyPaths(firstUpdate.Index + updates.Count, 0); - - ApplyUpdatesToPath(newPaths[pathIdx], updates); - - res = new PortableStructure(newPaths, _jumps, res._fieldTypes); - } - else if (startEntry.IsJumpTable) - { - // We are on the jump table. Add a new path and record it in the jump table. - - // 1. Prepare new structures. - var newPaths = CopyPaths(firstUpdate.Index + updates.Count, 1); - var newJumps = CopyJumps(0); - - // New path will be the last one. - int newPathIdx = newPaths.Length - 1; - - // Apply updates to the new path. - ApplyUpdatesToPath(newPaths[newPathIdx], updates); - - // Add the jump to the table. - newJumps[startEntry.Id] = - newJumps[startEntry.Id].CopyAndAdd(firstUpdate.FieldName, newPathIdx); - - res = new PortableStructure(newPaths, newJumps, res._fieldTypes); - } - else - { - // We are on existing entry. Need to create a new jump table here and two new paths. - - // 1. Prepaare new structures. - var newPaths = CopyPaths(firstUpdate.Index + updates.Count, 2); - var newJumps = CopyJumps(1); - - // Old path will be moved here. - int oldPathIdx = newPaths.Length - 2; - - // New path will reside here. - int newPathIdx = newPaths.Length - 1; - - // Create new jump table. - int newJumpIdx = newJumps.Length - 1; - - newJumps[newJumpIdx] = new PortableStructureJumpTable(startEntry.Name, oldPathIdx, - firstUpdate.FieldName, newPathIdx); - - // Re-create old path in two steps: move old path to the new place, then clean the old path. - for (int i = firstUpdate.Index; i < path.Length; i++) - { - newPaths[oldPathIdx][i] = newPaths[pathIdx][i]; - - if (i == firstUpdate.Index) - // Inject jump table ... - newPaths[pathIdx][i] = new PortableStructureEntry(newJumpIdx); - else - // ... or just reset. - newPaths[pathIdx][i] = new PortableStructureEntry(); - } - - // Apply updates to the new path. - ApplyUpdatesToPath(newPaths[newPaths.Length - 1], updates); - - res = new PortableStructure(newPaths, newJumps, res._fieldTypes); - } - - } - } - - return res; - } - - /// - /// Copy and possibly expand paths. - /// - /// Minimum length. - /// Amount of additional paths required. - /// Result. - private PortableStructureEntry[][] CopyPaths(int minLen, int additionalPaths) - { - var newPaths = new PortableStructureEntry[_paths.Length + additionalPaths][]; - - int newPathLen = Math.Max(_paths[0].Length, minLen); - - for (int i = 0; i < newPaths.Length; i++) - { - newPaths[i] = new PortableStructureEntry[newPathLen]; - - if (i < _paths.Length) - Array.Copy(_paths[i], newPaths[i], _paths[i].Length); - } - - return newPaths; - } - - /// - /// Copy and possibly expand jump tables. - /// - /// Amount of additional jumps required. - /// Result. - private PortableStructureJumpTable[] CopyJumps(int additionalJumps) - { - var newJumps = new PortableStructureJumpTable[_jumps.Length + additionalJumps]; - - // The very first jump is always null so that we can distinguish between jump table - // and empty value in PortableStructureEntry. - for (int i = 1; i < _jumps.Length; i++) - newJumps[i] = _jumps[i].Copy(); - - return newJumps; - } - - /// - /// Apply updates to path. - /// - /// Path. - /// Updates. - private static void ApplyUpdatesToPath(IList path, - IEnumerable updates) - { - foreach (var u in updates) - path[u.Index] = new PortableStructureEntry(u.FieldName, u.FieldId, u.FieldType); - } - - /// - /// Merge field types. - /// - /// Updates. - /// Type structure with applied updates. - private PortableStructure MergeFieldTypes(IList updates) - { - IDictionary newFieldTypes = new Dictionary(_fieldTypes); - - foreach (PortableStructureUpdate update in updates) - { - byte expType; - - if (_fieldTypes.TryGetValue(update.FieldName, out expType)) - { - // This is an old field. - if (expType != update.FieldType) - { - throw new PortableException("Field type mismatch detected [fieldName=" + update.FieldName + - ", expectedType=" + expType + ", actualType=" + update.FieldType + ']'); - } - } - else - // This is a new field. - newFieldTypes[update.FieldName] = update.FieldType; - } - - return newFieldTypes.Count == _fieldTypes.Count ? - this : new PortableStructure(_paths, _jumps, newFieldTypes); - } - - /// - /// Recorded field types. - /// - internal IDictionary FieldTypes - { - get { return _fieldTypes; } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureEntry.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureEntry.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureEntry.cs deleted file mode 100644 index 9b6d282..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureEntry.cs +++ /dev/null @@ -1,129 +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.Structure -{ - using System.Diagnostics; - - using Apache.Ignite.Core.Portable; - - /// - /// Portable type structure entry. Might be either a normal field, a reference to jump table, or an empty entry. - /// - internal struct PortableStructureEntry - { - /** Field name. */ - private readonly string _name; - - /** Field ID. */ - private readonly int _id; - - /** Field type. */ - private readonly byte _type; - - /// - /// Constructor for jump table entry. - /// - /// Jump table index. - public PortableStructureEntry(int jumpTblIdx) - { - Debug.Assert(jumpTblIdx > 0); - - _name = null; - _id = jumpTblIdx; - _type = 0; - } - - /// - /// Constructor for field entry. - /// - /// Field name. - /// Field ID. - /// Field type. - public PortableStructureEntry(string name, int id, byte type) - { - Debug.Assert(name != null); - - _name = name; - _id = id; - _type = type; - } - - /// - /// Check whether current field entry matches passed arguments. - /// - /// Field name. - /// Field type. - /// True if expected. - public bool IsExpected(string name, byte type) - { - // Perform reference equality check first because field name is a literal in most cases. - if (!ReferenceEquals(_name, name) && !name.Equals(_name)) - return false; - - ValidateType(type); - - return true; - } - - /// - /// Validate field type. - /// - /// Expected type. - public void ValidateType(byte type) - { - if (_type != type) - { - throw new PortableException("Field type mismatch detected [fieldName=" + _name + - ", expectedType=" + _type + ", actualType=" + type + ']'); - } - } - - /// - /// Whether this is an empty entry. - /// - /// - public bool IsEmpty - { - get { return _id == 0; } - } - - /// - /// Whether this is a jump table. - /// - public bool IsJumpTable - { - get { return _name == null && _id >= 0; } - } - - /// - /// Field name. - /// - public string Name - { - get { return _name; } - } - - /// - /// Field ID. - /// - public int Id - { - get { return _id; } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureJumpTable.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureJumpTable.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureJumpTable.cs deleted file mode 100644 index 7f8b373..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureJumpTable.cs +++ /dev/null @@ -1,118 +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.Structure -{ - using System; - using System.Diagnostics; - - /// - /// Jump table. - /// - internal class PortableStructureJumpTable - { - /** Names. */ - private readonly string[] _names; - - /** Path indexes. */ - private readonly int[] _pathIdxs; - - /// - /// Create minimal jump table with two entries. - /// - /// First name. - /// First path index. - /// Second name. - /// Second path index. - public PortableStructureJumpTable(string firstName, int firstPathIdx, - string secondName, int secondPathIdx) - { - _names = new[] { firstName, secondName }; - _pathIdxs = new[] { firstPathIdx, secondPathIdx }; - } - - /// - /// Constructor. - /// - /// Field names. - /// Path indexes. - private PortableStructureJumpTable(string[] names, int[] pathIdxs) - { - Debug.Assert(names.Length > 1); - Debug.Assert(names.Length == pathIdxs.Length); - - _names = names; - _pathIdxs = pathIdxs; - } - - /// - /// Get path index for the given field. - /// - /// Field name. - /// Path index. - public int GetPathIndex(string fieldName) - { - Debug.Assert(fieldName != null); - - // Optimistically assume that field name is a literal. - for (var i = 0; i < _names.Length; i++) - { - if (ReferenceEquals(fieldName, _names[i])) - return _pathIdxs[i]; - } - - // Fallback to slow-path with normal string comparison. - for (var i = 0; i < _names.Length; i++) - { - if (fieldName.Equals(_names[i])) - return _pathIdxs[i]; - } - - // No path found for the field. - return -1; - } - - /// - /// Copy jump table. - /// - /// New jump table. - public PortableStructureJumpTable Copy() - { - return new PortableStructureJumpTable(_names, _pathIdxs); - } - - /// - /// Copy jump table with additional jump. - /// - /// Field name. - /// Path index. - /// New jump table. - public PortableStructureJumpTable CopyAndAdd(string name, int pathIdx) - { - var newNames = new string[_names.Length + 1]; - var newPathIdxs = new int[_pathIdxs.Length + 1]; - - Array.Copy(_names, newNames, _names.Length); - Array.Copy(_pathIdxs, newPathIdxs, _pathIdxs.Length); - - newNames[newNames.Length - 1] = name; - newPathIdxs[newPathIdxs.Length - 1] = pathIdx; - - return new PortableStructureJumpTable(newNames, newPathIdxs); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureTracker.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureTracker.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureTracker.cs deleted file mode 100644 index 11ba032..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureTracker.cs +++ /dev/null @@ -1,140 +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.Structure -{ - using System.Collections.Generic; - - /// - /// Encapsulates logic for tracking field access and updating type descriptor structure. - /// - internal struct PortableStructureTracker - { - /** Current type structure. */ - private readonly IPortableTypeDescriptor _desc; - - /** Struct. */ - private readonly PortableStructure _portStruct; - - /** Current type structure path index. */ - private int _curStructPath; - - /** Current type structure action index. */ - private int _curStructAction; - - /** Current type structure updates. */ - private List _curStructUpdates; - - /// - /// Initializes a new instance of the class. - /// - /// The desc. - /// The structure to work with. - public PortableStructureTracker(IPortableTypeDescriptor desc, PortableStructure portStruct) - { - _desc = desc; - _portStruct = portStruct; - _curStructPath = 0; - _curStructAction = 0; - _curStructUpdates = null; - } - - /// - /// Gets the current structure action. - /// - public int CurStructAction - { - get { return _curStructAction; } - } - - /// - /// Gets the field ID. - /// - public int GetFieldId(string fieldName, byte fieldTypeId = 0) - { - _curStructAction++; - - if (_curStructUpdates == null) - { - var fieldId = _portStruct.GetFieldId(fieldName, fieldTypeId, ref _curStructPath, - _curStructAction); - - if (fieldId != 0) - return fieldId; - } - - return GetNewFieldId(fieldName, fieldTypeId, _curStructAction); - } - - /// - /// Updates the type structure. - /// - public void UpdateReaderStructure() - { - if (_curStructUpdates != null) - _desc.UpdateReadStructure(_desc.ReaderTypeStructure, _curStructPath, _curStructUpdates); - } - - /// - /// Updates the type structure and metadata for the specified writer. - /// - /// The writer. - public void UpdateWriterStructure(PortableWriterImpl writer) - { - if (_curStructUpdates != null) - { - _desc.UpdateWriteStructure(_desc.WriterTypeStructure, _curStructPath, _curStructUpdates); - - var marsh = writer.Marshaller; - - var metaHnd = marsh.GetMetadataHandler(_desc); - - if (metaHnd != null) - { - foreach (var u in _curStructUpdates) - metaHnd.OnFieldWrite(u.FieldId, u.FieldName, u.FieldType); - - var meta = metaHnd.OnObjectWriteFinished(); - - if (meta != null) - writer.SaveMetadata(_desc.TypeId, _desc.TypeName, _desc.AffinityKeyFieldName, meta); - } - } - } - - /// - /// Get ID for the new field and save structure update. - /// - /// Field name. - /// Field type ID. - /// Action index. - /// - /// Field ID. - /// - private int GetNewFieldId(string fieldName, byte fieldTypeId, int action) - { - var fieldId = PortableUtils.FieldId(_desc.TypeId, fieldName, _desc.NameMapper, _desc.IdMapper); - - if (_curStructUpdates == null) - _curStructUpdates = new List(); - - _curStructUpdates.Add(new PortableStructureUpdate(fieldName, fieldId, fieldTypeId, action)); - - return fieldId; - } - } -} \ 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/Structure/PortableStructureUpdate.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureUpdate.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureUpdate.cs deleted file mode 100644 index fa239db..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Portable/Structure/PortableStructureUpdate.cs +++ /dev/null @@ -1,84 +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.Structure -{ - /// - /// Portable type structure update descriptor. - /// - internal class PortableStructureUpdate - { - /** Field name. */ - private readonly string _fieldName; - - /** Field ID. */ - private readonly int _fieldId; - - /** Field type. */ - private readonly byte _fieldType; - - /** Field index. */ - private readonly int _idx; - - /// - /// Constructor. - /// - /// Field name. - /// Field ID. - /// Field type. - /// Index. - public PortableStructureUpdate(string fieldName, int fieldId, byte fieldType, int idx) - { - _fieldName = fieldName; - _fieldId = fieldId; - _fieldType = fieldType; - _idx = idx; - } - - /// - /// Field name. - /// - public string FieldName - { - get { return _fieldName; } - } - - /// - /// Field ID. - /// - public int FieldId - { - get { return _fieldId; } - } - - /// - /// Field type. - /// - public byte FieldType - { - get { return _fieldType; } - } - - /// - /// Index. - /// - public int Index - { - get { return _idx; } - } - } -}