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 2823718482 for ; Mon, 21 Sep 2015 14:27:57 +0000 (UTC) Received: (qmail 62075 invoked by uid 500); 21 Sep 2015 14:26:58 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 61947 invoked by uid 500); 21 Sep 2015 14:26:58 -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 61618 invoked by uid 99); 21 Sep 2015 14:26:58 -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; Mon, 21 Sep 2015 14:26:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E4DD5E03C8; Mon, 21 Sep 2015 14:26:57 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: vozerov@apache.org To: commits@ignite.apache.org Date: Mon, 21 Sep 2015 14:27:05 -0000 Message-Id: <80f7813aeb2d48fd818d2507dbbb07f6@git.apache.org> In-Reply-To: <93d26f1773af4c11ad83de1f4f477c7a@git.apache.org> References: <93d26f1773af4c11ad83de1f4f477c7a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [09/52] [partial] ignite git commit: IGNITE-1513: Moved .Net. http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableHeapStream.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableHeapStream.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableHeapStream.cs deleted file mode 100644 index 690f92c..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableHeapStream.cs +++ /dev/null @@ -1,447 +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.IO -{ - using System; - using System.IO; - using System.Text; - - /// - /// Portable onheap stream. - /// - internal unsafe class PortableHeapStream : PortableAbstractStream - { - /** Data array. */ - protected byte[] Data; - - /// - /// Constructor. - /// - /// Initial capacity. - public PortableHeapStream(int cap) - { - Data = new byte[cap]; - } - - /// - /// Constructor. - /// - /// Data array. - public PortableHeapStream(byte[] data) - { - Data = data; - } - - /** */ - public override void WriteByte(byte val) - { - int pos0 = EnsureWriteCapacityAndShift(1); - - Data[pos0] = val; - } - - /** */ - public override byte ReadByte() - { - int pos0 = EnsureReadCapacityAndShift(1); - - return Data[pos0]; - } - - /** */ - public override void WriteByteArray(byte[] val) - { - int pos0 = EnsureWriteCapacityAndShift(val.Length); - - fixed (byte* data0 = Data) - { - WriteByteArray0(val, data0 + pos0); - } - } - - /** */ - public override byte[] ReadByteArray(int cnt) - { - int pos0 = EnsureReadCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - return ReadByteArray0(cnt, data0 + pos0); - } - } - - /** */ - public override void WriteBoolArray(bool[] val) - { - int pos0 = EnsureWriteCapacityAndShift(val.Length); - - fixed (byte* data0 = Data) - { - WriteBoolArray0(val, data0 + pos0); - } - } - - /** */ - public override bool[] ReadBoolArray(int cnt) - { - int pos0 = EnsureReadCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - return ReadBoolArray0(cnt, data0 + pos0); - } - } - - /** */ - public override void WriteShort(short val) - { - int pos0 = EnsureWriteCapacityAndShift(2); - - fixed (byte* data0 = Data) - { - WriteShort0(val, data0 + pos0); - } - } - - /** */ - public override short ReadShort() - { - int pos0 = EnsureReadCapacityAndShift(2); - - fixed (byte* data0 = Data) - { - return ReadShort0(data0 + pos0); - } - } - - /** */ - public override void WriteShortArray(short[] val) - { - int cnt = val.Length << 1; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteShortArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override short[] ReadShortArray(int cnt) - { - int cnt0 = cnt << 1; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadShortArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override void WriteCharArray(char[] val) - { - int cnt = val.Length << 1; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteCharArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override char[] ReadCharArray(int cnt) - { - int cnt0 = cnt << 1; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadCharArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override void WriteInt(int val) - { - int pos0 = EnsureWriteCapacityAndShift(4); - - fixed (byte* data0 = Data) - { - WriteInt0(val, data0 + pos0); - } - } - - /** */ - public override void WriteInt(int writePos, int val) - { - EnsureWriteCapacity(writePos + 4); - - fixed (byte* data0 = Data) - { - WriteInt0(val, data0 + writePos); - } - } - - /** */ - public override int ReadInt() - { - int pos0 = EnsureReadCapacityAndShift(4); - - fixed (byte* data0 = Data) - { - return ReadInt0(data0 + pos0); - } - } - - /** */ - public override void WriteIntArray(int[] val) - { - int cnt = val.Length << 2; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteIntArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override int[] ReadIntArray(int cnt) - { - int cnt0 = cnt << 2; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadIntArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override void WriteFloatArray(float[] val) - { - int cnt = val.Length << 2; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteFloatArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override float[] ReadFloatArray(int cnt) - { - int cnt0 = cnt << 2; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadFloatArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override void WriteLong(long val) - { - int pos0 = EnsureWriteCapacityAndShift(8); - - fixed (byte* data0 = Data) - { - WriteLong0(val, data0 + pos0); - } - } - - /** */ - public override long ReadLong() - { - int pos0 = EnsureReadCapacityAndShift(8); - - fixed (byte* data0 = Data) - { - return ReadLong0(data0 + pos0); - } - } - - /** */ - public override void WriteLongArray(long[] val) - { - int cnt = val.Length << 3; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteLongArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override long[] ReadLongArray(int cnt) - { - int cnt0 = cnt << 3; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadLongArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override void WriteDoubleArray(double[] val) - { - int cnt = val.Length << 3; - - int pos0 = EnsureWriteCapacityAndShift(cnt); - - fixed (byte* data0 = Data) - { - WriteDoubleArray0(val, data0 + pos0, cnt); - } - } - - /** */ - public override double[] ReadDoubleArray(int cnt) - { - int cnt0 = cnt << 3; - - int pos0 = EnsureReadCapacityAndShift(cnt0); - - fixed (byte* data0 = Data) - { - return ReadDoubleArray0(cnt, data0 + pos0, cnt0); - } - } - - /** */ - public override int WriteString(char* chars, int charCnt, int byteCnt, Encoding encoding) - { - int pos0 = EnsureWriteCapacityAndShift(byteCnt); - - int written; - - fixed (byte* data0 = Data) - { - written = WriteString0(chars, charCnt, byteCnt, encoding, data0 + pos0); - } - - return written; - } - - /** */ - public override void Write(byte* src, int cnt) - { - EnsureWriteCapacity(Pos + cnt); - - fixed (byte* data0 = Data) - { - WriteInternal(src, cnt, data0); - } - - ShiftWrite(cnt); - } - - /** */ - public override void Read(byte* dest, int cnt) - { - fixed (byte* data0 = Data) - { - ReadInternal(dest, cnt, data0); - } - } - - /** */ - public override int Remaining() - { - return Data.Length - Pos; - } - - /** */ - public override byte[] Array() - { - return Data; - } - - /** */ - public override byte[] ArrayCopy() - { - byte[] copy = new byte[Pos]; - - Buffer.BlockCopy(Data, 0, copy, 0, Pos); - - return copy; - } - - /** */ - public override bool IsSameArray(byte[] arr) - { - return Data == arr; - } - - /** */ - protected override void Dispose(bool disposing) - { - // No-op. - } - - /// - /// Internal array. - /// - internal byte[] InternalArray - { - get { return Data; } - } - - /** */ - protected override void EnsureWriteCapacity(int cnt) - { - if (cnt > Data.Length) - { - int newCap = Capacity(Data.Length, cnt); - - byte[] data0 = new byte[newCap]; - - // Copy the whole initial array length here because it can be changed - // from Java without position adjusting. - Buffer.BlockCopy(Data, 0, data0, 0, Data.Length); - - Data = data0; - } - } - - /** */ - protected override void EnsureReadCapacity(int cnt) - { - if (Data.Length - Pos < cnt) - throw new EndOfStreamException("Not enough data in stream [expected=" + cnt + - ", remaining=" + (Data.Length - Pos) + ']'); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableStreamAdapter.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableStreamAdapter.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableStreamAdapter.cs deleted file mode 100644 index 1d17f89..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Io/PortableStreamAdapter.cs +++ /dev/null @@ -1,114 +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.IO -{ - using System; - using System.IO; - - /// - /// Adapter providing .Net streaming functionality over the portable stream. - /// - internal class PortableStreamAdapter : Stream - { - /// - /// - /// - private readonly IPortableStream _stream; - - /// - /// Constructor. - /// - /// Stream. - public PortableStreamAdapter(IPortableStream stream) - { - _stream = stream; - } - - /** */ - public override void Write(byte[] buffer, int offset, int count) - { - _stream.Write(buffer, offset, count); - } - - /** */ - public override int Read(byte[] buffer, int offset, int count) - { - _stream.Read(buffer, offset, count); - - return count; - } - - /** */ - public override void Flush() - { - // No-op. - } - - /** */ - public override bool CanRead - { - get { return true; } - } - - /** */ - public override bool CanWrite - { - get { return true; } - } - - /** */ - public override bool CanSeek - { - get { return false; } - } - - /** */ - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException("Stream is not seekable."); - } - - /** */ - public override long Position - { - get - { - throw new NotSupportedException("Stream is not seekable."); - } - set - { - throw new NotSupportedException("Stream is not seekable."); - } - } - - /** */ - public override long Length - { - get - { - throw new NotSupportedException("Stream is not seekable."); - } - } - - /** */ - public override void SetLength(long value) - { - throw new NotSupportedException("Stream is not seekable."); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/IPortableMetadataHandler.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/IPortableMetadataHandler.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/IPortableMetadataHandler.cs deleted file mode 100644 index dc3090f..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/IPortableMetadataHandler.cs +++ /dev/null @@ -1,41 +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.Metadata -{ - using System.Collections.Generic; - - /// - /// Portable metadata handler. - /// - public interface IPortableMetadataHandler - { - /// - /// Callback invoked when named field is written. - /// - /// Field ID. - /// Field name. - /// Field type ID. - void OnFieldWrite(int fieldId, string fieldName, int typeId); - - /// - /// Callback invoked when object write is finished and it is time to collect missing metadata. - /// - /// Collected metadata. - IDictionary OnObjectWriteFinished(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableHashsetMetadataHandler.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableHashsetMetadataHandler.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableHashsetMetadataHandler.cs deleted file mode 100644 index 8df5f36..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableHashsetMetadataHandler.cs +++ /dev/null @@ -1,69 +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.Metadata -{ - using System.Collections.Generic; - - /// - /// Metadata handler which uses hash set to determine whether field was already written or not. - /// - internal class PortableHashsetMetadataHandler : IPortableMetadataHandler - { - /** Empty fields collection. */ - private static readonly IDictionary EmptyFields = new Dictionary(); - - /** IDs known when serialization starts. */ - private readonly ICollection _ids; - - /** New fields. */ - private IDictionary _fieldMap; - - /** */ - private readonly bool _newType; - - /// - /// Constructor. - /// - /// IDs. - /// True is metadata for type is not saved. - public PortableHashsetMetadataHandler(ICollection ids, bool newType) - { - _ids = ids; - _newType = newType; - } - - /** */ - public void OnFieldWrite(int fieldId, string fieldName, int typeId) - { - if (!_ids.Contains(fieldId)) - { - if (_fieldMap == null) - _fieldMap = new Dictionary(); - - if (!_fieldMap.ContainsKey(fieldName)) - _fieldMap[fieldName] = typeId; - } - } - - /** */ - public IDictionary OnObjectWriteFinished() - { - return _fieldMap ?? (_newType ? EmptyFields : null); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataHolder.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataHolder.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataHolder.cs deleted file mode 100644 index a3fa90f..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataHolder.cs +++ /dev/null @@ -1,149 +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.Metadata -{ - using System; - using System.Collections.Generic; - using Apache.Ignite.Core.Portable; - - /// - /// Metadata for particular type. - /// - internal class PortableMetadataHolder - { - /** Type ID. */ - private readonly int _typeId; - - /** Type name. */ - private readonly string _typeName; - - /** Affinity key field name. */ - private readonly string _affKeyFieldName; - - /** Empty metadata when nothig is know about object fields yet. */ - private readonly IPortableMetadata _emptyMeta; - - /** Collection of know field IDs. */ - private volatile ICollection _ids; - - /** Last known unmodifiable metadata which is given to the user. */ - private volatile PortableMetadataImpl _meta; - - /** Saved flag (set if type metadata was saved at least once). */ - private volatile bool _saved; - - /// - /// Constructor. - /// - /// Type ID. - /// Type name. - /// Affinity key field name. - public PortableMetadataHolder(int typeId, string typeName, string affKeyFieldName) - { - _typeId = typeId; - _typeName = typeName; - _affKeyFieldName = affKeyFieldName; - - _emptyMeta = new PortableMetadataImpl(typeId, typeName, null, affKeyFieldName); - } - - /// - /// Get saved flag. - /// - /// True if type metadata was saved at least once. - public bool Saved() - { - return _saved; - } - - /// - /// Get current type metadata. - /// - /// Type metadata. - public IPortableMetadata Metadata() - { - PortableMetadataImpl meta0 = _meta; - - return meta0 != null ? _meta : _emptyMeta; - } - - /// - /// Currently cached field IDs. - /// - /// Cached field IDs. - public ICollection FieldIds() - { - ICollection ids0 = _ids; - - if (_ids == null) - { - lock (this) - { - ids0 = _ids; - - if (ids0 == null) - { - ids0 = new HashSet(); - - _ids = ids0; - } - } - } - - return ids0; - } - - /// - /// Merge newly sent field metadatas into existing ones. - /// - /// New field metadatas map. - public void Merge(IDictionary> newMap) - { - _saved = true; - - if (newMap == null || newMap.Count == 0) - return; - - lock (this) - { - // 1. Create copies of the old meta. - ICollection ids0 = _ids; - PortableMetadataImpl meta0 = _meta; - - ICollection newIds = ids0 != null ? new HashSet(ids0) : new HashSet(); - - IDictionary newFields = meta0 != null ? - new Dictionary(meta0.FieldsMap()) : new Dictionary(newMap.Count); - - // 2. Add new fields. - foreach (KeyValuePair> newEntry in newMap) - { - if (!newIds.Contains(newEntry.Key)) - newIds.Add(newEntry.Key); - - if (!newFields.ContainsKey(newEntry.Value.Item1)) - newFields[newEntry.Value.Item1] = newEntry.Value.Item2; - } - - // 3. Assign new meta. Order is important here: meta must be assigned before field IDs. - _meta = new PortableMetadataImpl(_typeId, _typeName, newFields, _affKeyFieldName); - _ids = newIds; - } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataImpl.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataImpl.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataImpl.cs deleted file mode 100644 index 06578c0..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/Metadata/PortableMetadataImpl.cs +++ /dev/null @@ -1,200 +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.Metadata -{ - using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; - using Apache.Ignite.Core.Portable; - - /// - /// Portable metadata implementation. - /// - internal class PortableMetadataImpl : IPortableMetadata - { - /** Empty metadata. */ - [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] - public static readonly PortableMetadataImpl EmptyMeta = - new PortableMetadataImpl(PortableUtils.TypeObject, PortableTypeNames.TypeNameObject, null, null); - - /** Empty dictionary. */ - private static readonly IDictionary EmptyDict = new Dictionary(); - - /** Empty list. */ - private static readonly ICollection EmptyList = new List().AsReadOnly(); - - /** Fields. */ - private readonly IDictionary _fields; - - /// - /// Get type name by type ID. - /// - /// Type ID. - /// Type name. - private static string ConvertTypeName(int typeId) - { - switch (typeId) - { - case PortableUtils.TypeBool: - return PortableTypeNames.TypeNameBool; - case PortableUtils.TypeByte: - return PortableTypeNames.TypeNameByte; - case PortableUtils.TypeShort: - return PortableTypeNames.TypeNameShort; - case PortableUtils.TypeChar: - return PortableTypeNames.TypeNameChar; - case PortableUtils.TypeInt: - return PortableTypeNames.TypeNameInt; - case PortableUtils.TypeLong: - return PortableTypeNames.TypeNameLong; - case PortableUtils.TypeFloat: - return PortableTypeNames.TypeNameFloat; - case PortableUtils.TypeDouble: - return PortableTypeNames.TypeNameDouble; - case PortableUtils.TypeDecimal: - return PortableTypeNames.TypeNameDecimal; - case PortableUtils.TypeString: - return PortableTypeNames.TypeNameString; - case PortableUtils.TypeGuid: - return PortableTypeNames.TypeNameGuid; - case PortableUtils.TypeDate: - return PortableTypeNames.TypeNameDate; - case PortableUtils.TypeEnum: - return PortableTypeNames.TypeNameEnum; - case PortableUtils.TypePortable: - case PortableUtils.TypeObject: - return PortableTypeNames.TypeNameObject; - case PortableUtils.TypeArrayBool: - return PortableTypeNames.TypeNameArrayBool; - case PortableUtils.TypeArrayByte: - return PortableTypeNames.TypeNameArrayByte; - case PortableUtils.TypeArrayShort: - return PortableTypeNames.TypeNameArrayShort; - case PortableUtils.TypeArrayChar: - return PortableTypeNames.TypeNameArrayChar; - case PortableUtils.TypeArrayInt: - return PortableTypeNames.TypeNameArrayInt; - case PortableUtils.TypeArrayLong: - return PortableTypeNames.TypeNameArrayLong; - case PortableUtils.TypeArrayFloat: - return PortableTypeNames.TypeNameArrayFloat; - case PortableUtils.TypeArrayDouble: - return PortableTypeNames.TypeNameArrayDouble; - case PortableUtils.TypeArrayDecimal: - return PortableTypeNames.TypeNameArrayDecimal; - case PortableUtils.TypeArrayString: - return PortableTypeNames.TypeNameArrayString; - case PortableUtils.TypeArrayGuid: - return PortableTypeNames.TypeNameArrayGuid; - case PortableUtils.TypeArrayDate: - return PortableTypeNames.TypeNameArrayDate; - case PortableUtils.TypeArrayEnum: - return PortableTypeNames.TypeNameArrayEnum; - case PortableUtils.TypeArray: - return PortableTypeNames.TypeNameArrayObject; - case PortableUtils.TypeCollection: - return PortableTypeNames.TypeNameCollection; - case PortableUtils.TypeDictionary: - return PortableTypeNames.TypeNameMap; - default: - throw new PortableException("Invalid type ID: " + typeId); - } - } - - /// - /// Initializes a new instance of the class. - /// - /// The reader. - public PortableMetadataImpl(IPortableRawReader reader) - { - TypeId = reader.ReadInt(); - TypeName = reader.ReadString(); - AffinityKeyFieldName = reader.ReadString(); - _fields = reader.ReadGenericDictionary(); - } - - /// - /// Constructor. - /// - /// Type ID. - /// Type name. - /// Fields. - /// Affinity key field name. - public PortableMetadataImpl(int typeId, string typeName, IDictionary fields, - string affKeyFieldName) - { - TypeId = typeId; - TypeName = typeName; - AffinityKeyFieldName = affKeyFieldName; - _fields = fields; - } - - /// - /// Type ID. - /// - /// - public int TypeId { get; private set; } - - /// - /// Gets type name. - /// - public string TypeName { get; private set; } - - /// - /// Gets field names for that type. - /// - public ICollection Fields - { - get { return _fields != null ? _fields.Keys : EmptyList; } - } - - /// - /// Gets field type for the given field name. - /// - /// Field name. - /// - /// Field type. - /// - public string GetFieldTypeName(string fieldName) - { - if (_fields != null) - { - int typeId; - - _fields.TryGetValue(fieldName, out typeId); - - return ConvertTypeName(typeId); - } - - return null; - } - - /// - /// Gets optional affinity key field name. - /// - public string AffinityKeyFieldName { get; private set; } - - /// - /// Gets fields map. - /// - /// Fields map. - public IDictionary FieldsMap() - { - return _fields ?? EmptyDict; - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderField.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderField.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderField.cs deleted file mode 100644 index 026d0d4..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderField.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; - - /// - /// Portable builder field. - /// - internal class PortableBuilderField - { - /** Remove marker object. */ - public static readonly object RmvMarkerObj = new object(); - - /** Remove marker. */ - public static readonly PortableBuilderField RmvMarker = - new PortableBuilderField(null, RmvMarkerObj); - - /** Type. */ - private readonly Type _typ; - - /** Value. */ - private readonly object _val; - - /// - /// Constructor. - /// - /// Type. - /// Value. - public PortableBuilderField(Type typ, object val) - { - _typ = typ; - _val = val; - } - - /// - /// Type. - /// - public Type Type - { - get - { - return _typ; - } - } - - /// - /// Value. - /// - public object Value - { - get - { - return _val; - } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderImpl.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderImpl.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderImpl.cs deleted file mode 100644 index dc0f570..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableBuilderImpl.cs +++ /dev/null @@ -1,923 +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.Portable.IO; - using Apache.Ignite.Core.Impl.Portable.Metadata; - using Apache.Ignite.Core.Portable; - - /// - /// Portable builder implementation. - /// - internal class PortableBuilderImpl : IPortableBuilder - { - /** Type IDs for metadata. */ - private static readonly IDictionary TypeIds; - - /** Cached dictionary with no values. */ - private static readonly IDictionary EmptyVals = new Dictionary(); - - /** Offset: length. */ - private const int OffsetLen = 10; - - /** Portables. */ - private readonly PortablesImpl _portables; - - /** */ - private readonly PortableBuilderImpl _parent; - - /** Initial portable object. */ - private readonly PortableUserObject _obj; - - /** Type descriptor. */ - private readonly IPortableTypeDescriptor _desc; - - /** Values. */ - private IDictionary _vals; - - /** Contextual fields. */ - private IDictionary _cache; - - /** Hash code. */ - private int _hashCode; - - /** Current context. */ - private Context _ctx; - - /// - /// Static initializer. - /// - static PortableBuilderImpl() - { - TypeIds = new Dictionary(); - - // 1. Primitives. - TypeIds[typeof(byte)] = PortableUtils.TypeByte; - TypeIds[typeof(bool)] = PortableUtils.TypeBool; - TypeIds[typeof(short)] = PortableUtils.TypeShort; - TypeIds[typeof(char)] = PortableUtils.TypeChar; - TypeIds[typeof(int)] = PortableUtils.TypeInt; - TypeIds[typeof(long)] = PortableUtils.TypeLong; - TypeIds[typeof(float)] = PortableUtils.TypeFloat; - TypeIds[typeof(double)] = PortableUtils.TypeDouble; - TypeIds[typeof(decimal)] = PortableUtils.TypeDecimal; - - TypeIds[typeof(byte[])] = PortableUtils.TypeArrayByte; - TypeIds[typeof(bool[])] = PortableUtils.TypeArrayBool; - TypeIds[typeof(short[])] = PortableUtils.TypeArrayShort; - TypeIds[typeof(char[])] = PortableUtils.TypeArrayChar; - TypeIds[typeof(int[])] = PortableUtils.TypeArrayInt; - TypeIds[typeof(long[])] = PortableUtils.TypeArrayLong; - TypeIds[typeof(float[])] = PortableUtils.TypeArrayFloat; - TypeIds[typeof(double[])] = PortableUtils.TypeArrayDouble; - TypeIds[typeof(decimal[])] = PortableUtils.TypeArrayDecimal; - - // 2. String. - TypeIds[typeof(string)] = PortableUtils.TypeString; - TypeIds[typeof(string[])] = PortableUtils.TypeArrayString; - - // 3. Guid. - TypeIds[typeof(Guid)] = PortableUtils.TypeGuid; - TypeIds[typeof(Guid?)] = PortableUtils.TypeGuid; - TypeIds[typeof(Guid[])] = PortableUtils.TypeArrayGuid; - TypeIds[typeof(Guid?[])] = PortableUtils.TypeArrayGuid; - - // 4. Date. - TypeIds[typeof(DateTime)] = PortableUtils.TypeDate; - TypeIds[typeof(DateTime?)] = PortableUtils.TypeDate; - TypeIds[typeof(DateTime[])] = PortableUtils.TypeArrayDate; - TypeIds[typeof(DateTime?[])] = PortableUtils.TypeArrayDate; - } - - /// - /// Constructor. - /// - /// Portables. - /// Initial portable object. - /// Type descriptor. - public PortableBuilderImpl(PortablesImpl portables, PortableUserObject obj, - IPortableTypeDescriptor desc) : this(portables, null, obj, desc) - { - // No-op. - } - - /// - /// Constructor. - /// - /// Portables. - /// Parent builder. - /// Initial portable object. - /// Type descriptor. - public PortableBuilderImpl(PortablesImpl portables, PortableBuilderImpl parent, - PortableUserObject obj, IPortableTypeDescriptor desc) - { - _portables = portables; - _parent = parent ?? this; - _obj = obj; - _desc = desc; - - _hashCode = obj.GetHashCode(); - } - - /** */ - public IPortableBuilder SetHashCode(int hashCode) - { - _hashCode = hashCode; - - return this; - } - - /** */ - public T GetField(string name) - { - PortableBuilderField field; - - if (_vals != null && _vals.TryGetValue(name, out field)) - return field != PortableBuilderField.RmvMarker ? (T)field.Value : default(T); - T val = _obj.Field(name, this); - - if (_vals == null) - _vals = new Dictionary(2); - - _vals[name] = new PortableBuilderField(typeof(T), val); - - return val; - } - - /** */ - public IPortableBuilder SetField(string name, T val) - { - return SetField0(name, new PortableBuilderField(typeof(T), val)); - } - - /** */ - public IPortableBuilder RemoveField(string name) - { - return SetField0(name, PortableBuilderField.RmvMarker); - } - - /** */ - public IPortableObject Build() - { - PortableHeapStream inStream = new PortableHeapStream(_obj.Data); - - inStream.Seek(_obj.Offset, SeekOrigin.Begin); - - // Assume that resulting length will be no less than header + [fields_cnt] * 12; - int len = PortableUtils.FullHdrLen + (_vals == null ? 0 : _vals.Count * 12); - - PortableHeapStream outStream = new PortableHeapStream(len); - - PortableWriterImpl writer = _portables.Marshaller.StartMarshal(outStream); - - writer.Builder(this); - - // All related builders will work in this context with this writer. - _parent._ctx = new Context(writer); - - try - { - // Write. - writer.Write(this, null); - - // Process metadata. - _portables.Marshaller.FinishMarshal(writer); - - // Create portable object once metadata is processed. - return new PortableUserObject(_portables.Marshaller, outStream.InternalArray, 0, - _desc.TypeId, _hashCode); - } - finally - { - // Cleanup. - _parent._ctx.Closed = true; - } - } - - /// - /// Create child builder. - /// - /// Portable object. - /// Child builder. - public PortableBuilderImpl Child(PortableUserObject obj) - { - return _portables.ChildBuilder(_parent, obj); - } - - /// - /// Get cache field. - /// - /// Position. - /// Value. - /// true if value is found in cache. - public bool CachedField(int pos, out T val) - { - if (_parent._cache != null) - { - object res; - - if (_parent._cache.TryGetValue(pos, out res)) - { - val = res != null ? (T)res : default(T); - - return true; - } - } - - val = default(T); - - return false; - } - - /// - /// Add field to cache test. - /// - /// Position. - /// Value. - public void CacheField(int pos, object val) - { - if (_parent._cache == null) - _parent._cache = new Dictionary(2); - - _parent._cache[pos] = val; - } - - /// - /// Internal set field routine. - /// - /// Name. - /// Value. - /// This builder. - private IPortableBuilder SetField0(string fieldName, PortableBuilderField val) - { - if (_vals == null) - _vals = new Dictionary(); - - _vals[fieldName] = val; - - return this; - } - - /// - /// Mutate portable object. - /// - /// Input stream with initial object. - /// Output stream. - /// Portable type descriptor. - /// Hash code. - /// Values. - internal void Mutate( - PortableHeapStream inStream, - PortableHeapStream outStream, - IPortableTypeDescriptor desc, - int hashCode, - IDictionary vals) - { - // Set correct builder to writer frame. - PortableBuilderImpl oldBuilder = _parent._ctx.Writer.Builder(_parent); - - int streamPos = inStream.Position; - - try - { - // Prepare fields. - IPortableMetadataHandler metaHnd = _portables.Marshaller.MetadataHandler(desc); - - IDictionary vals0; - - if (vals == null || vals.Count == 0) - vals0 = EmptyVals; - else - { - vals0 = new Dictionary(vals.Count); - - foreach (KeyValuePair valEntry in vals) - { - int fieldId = PortableUtils.FieldId(desc.TypeId, valEntry.Key, desc.NameConverter, desc.Mapper); - - if (vals0.ContainsKey(fieldId)) - throw new IgniteException("Collision in field ID detected (change field name or " + - "define custom ID mapper) [fieldName=" + valEntry.Key + ", fieldId=" + fieldId + ']'); - - vals0[fieldId] = valEntry.Value.Value; - - // Write metadata if: 1) it is enabled for type; 2) type is not null (i.e. it is neither - // remove marker, nor a field read through "GetField" method. - if (metaHnd != null && valEntry.Value.Type != null) - metaHnd.OnFieldWrite(fieldId, valEntry.Key, TypeId(valEntry.Value.Type)); - } - } - - // Actual processing. - Mutate0(_parent._ctx, inStream, outStream, true, hashCode, vals0); - - // 3. Handle metadata. - if (metaHnd != null) - { - IDictionary meta = metaHnd.OnObjectWriteFinished(); - - if (meta != null) - _parent._ctx.Writer.SaveMetadata(desc.TypeId, desc.TypeName, desc.AffinityKeyFieldName, meta); - } - } - finally - { - // Restore builder frame. - _parent._ctx.Writer.Builder(oldBuilder); - - inStream.Seek(streamPos, SeekOrigin.Begin); - } - } - - /// - /// Internal mutation routine. - /// - /// Input stream. - /// Output stream. - /// Context. - /// WHether hash should be changed. - /// New hash. - /// Values to be replaced. - /// Mutated object. - private void Mutate0(Context ctx, PortableHeapStream inStream, IPortableStream outStream, - bool changeHash, int hash, IDictionary vals) - { - int inStartPos = inStream.Position; - int outStartPos = outStream.Position; - - byte inHdr = inStream.ReadByte(); - - if (inHdr == PortableUtils.HdrNull) - outStream.WriteByte(PortableUtils.HdrNull); - else if (inHdr == PortableUtils.HdrHnd) - { - int inHnd = inStream.ReadInt(); - - int oldPos = inStartPos - inHnd; - int newPos; - - if (ctx.OldToNew(oldPos, out newPos)) - { - // Handle is still valid. - outStream.WriteByte(PortableUtils.HdrHnd); - outStream.WriteInt(outStartPos - newPos); - } - else - { - // Handle is invalid, write full object. - int inRetPos = inStream.Position; - - inStream.Seek(oldPos, SeekOrigin.Begin); - - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - - inStream.Seek(inRetPos, SeekOrigin.Begin); - } - } - else if (inHdr == PortableUtils.HdrFull) - { - byte inUsrFlag = inStream.ReadByte(); - int inTypeId = inStream.ReadInt(); - int inHash = inStream.ReadInt(); - int inLen = inStream.ReadInt(); - int inRawOff = inStream.ReadInt(); - - int hndPos; - - if (ctx.AddOldToNew(inStartPos, outStartPos, out hndPos)) - { - // Object could be cached in parent builder. - object cachedVal; - - if (_parent._cache != null && _parent._cache.TryGetValue(inStartPos, out cachedVal)) { - ctx.Writer.Write(cachedVal, null); - } - else - { - // New object, write in full form. - outStream.WriteByte(PortableUtils.HdrFull); - outStream.WriteByte(inUsrFlag); - outStream.WriteInt(inTypeId); - outStream.WriteInt(changeHash ? hash : inHash); - - // Skip length and raw offset as they are not known at this point. - outStream.Seek(8, SeekOrigin.Current); - - // Write regular fields. - while (inStream.Position < inStartPos + inRawOff) - { - int inFieldId = inStream.ReadInt(); - int inFieldLen = inStream.ReadInt(); - int inFieldDataPos = inStream.Position; - - object fieldVal; - - bool fieldFound = vals.TryGetValue(inFieldId, out fieldVal); - - if (!fieldFound || fieldVal != PortableBuilderField.RmvMarkerObj) - { - outStream.WriteInt(inFieldId); - - int fieldLenPos = outStream.Position; // Here we will write length later. - - outStream.Seek(4, SeekOrigin.Current); - - if (fieldFound) - { - // Replace field with new value. - if (fieldVal != PortableBuilderField.RmvMarkerObj) - ctx.Writer.Write(fieldVal, null); - - vals.Remove(inFieldId); - } - else - { - // If field was requested earlier, then we must write tracked value - if (_parent._cache != null && _parent._cache.TryGetValue(inFieldDataPos, out fieldVal)) - ctx.Writer.Write(fieldVal, null); - else - // Filed is not tracked, re-write as is. - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - } - - int fieldEndPos = outStream.Position; - - outStream.Seek(fieldLenPos, SeekOrigin.Begin); - outStream.WriteInt(fieldEndPos - fieldLenPos - 4); - outStream.Seek(fieldEndPos, SeekOrigin.Begin); - } - - // Position intput stream pointer after the field. - inStream.Seek(inFieldDataPos + inFieldLen, SeekOrigin.Begin); - } - - // Write remaining new fields. - foreach (KeyValuePair valEntry in vals) - { - if (valEntry.Value != PortableBuilderField.RmvMarkerObj) - { - outStream.WriteInt(valEntry.Key); - - int fieldLenPos = outStream.Position; // Here we will write length later. - - outStream.Seek(4, SeekOrigin.Current); - - ctx.Writer.Write(valEntry.Value, null); - - int fieldEndPos = outStream.Position; - - outStream.Seek(fieldLenPos, SeekOrigin.Begin); - outStream.WriteInt(fieldEndPos - fieldLenPos - 4); - outStream.Seek(fieldEndPos, SeekOrigin.Begin); - } - } - - // Write raw data. - int rawPos = outStream.Position; - - outStream.Write(inStream.InternalArray, inStartPos + inRawOff, inLen - inRawOff); - - // Write length and raw data offset. - int outResPos = outStream.Position; - - outStream.Seek(outStartPos + OffsetLen, SeekOrigin.Begin); - - outStream.WriteInt(outResPos - outStartPos); // Length. - outStream.WriteInt(rawPos - outStartPos); // Raw offset. - - outStream.Seek(outResPos, SeekOrigin.Begin); - } - } - else - { - // Object has already been written, write as handle. - outStream.WriteByte(PortableUtils.HdrHnd); - outStream.WriteInt(outStartPos - hndPos); - } - - // Synchronize input stream position. - inStream.Seek(inStartPos + inLen, SeekOrigin.Begin); - } - else - { - // Try writing as well-known type with fixed size. - outStream.WriteByte(inHdr); - - if (!WriteAsPredefined(inHdr, inStream, outStream, ctx)) - throw new IgniteException("Unexpected header [position=" + (inStream.Position - 1) + - ", header=" + inHdr + ']'); - } - } - - /// - /// Process portable object inverting handles if needed. - /// - /// Output stream. - /// Portable. - internal void ProcessPortable(IPortableStream outStream, PortableUserObject port) - { - // Special case: writing portable object with correct inversions. - PortableHeapStream inStream = new PortableHeapStream(port.Data); - - inStream.Seek(port.Offset, SeekOrigin.Begin); - - // Use fresh context to ensure correct portable inversion. - Mutate0(new Context(), inStream, outStream, false, 0, EmptyVals); - } - - /// - /// Process child builder. - /// - /// Output stream. - /// Builder. - internal void ProcessBuilder(IPortableStream outStream, PortableBuilderImpl builder) - { - PortableHeapStream inStream = new PortableHeapStream(builder._obj.Data); - - inStream.Seek(builder._obj.Offset, SeekOrigin.Begin); - - // Builder parent context might be null only in one case: if we never met this group of - // builders before. In this case we set context to their parent and track it. Context - // cleanup will be performed at the very end of build process. - if (builder._parent._ctx == null || builder._parent._ctx.Closed) - builder._parent._ctx = new Context(_parent._ctx); - - builder.Mutate(inStream, outStream as PortableHeapStream, builder._desc, - builder._hashCode, builder._vals); - } - - /// - /// Write object as a predefined type if possible. - /// - /// Header. - /// Input stream. - /// Output stream. - /// Context. - /// True if was written. - private bool WriteAsPredefined(byte hdr, PortableHeapStream inStream, IPortableStream outStream, - Context ctx) - { - switch (hdr) - { - case PortableUtils.TypeByte: - TransferBytes(inStream, outStream, 1); - - break; - - case PortableUtils.TypeShort: - TransferBytes(inStream, outStream, 2); - - break; - - case PortableUtils.TypeInt: - TransferBytes(inStream, outStream, 4); - - break; - - case PortableUtils.TypeLong: - TransferBytes(inStream, outStream, 8); - - break; - - case PortableUtils.TypeFloat: - TransferBytes(inStream, outStream, 4); - - break; - - case PortableUtils.TypeDouble: - TransferBytes(inStream, outStream, 8); - - break; - - case PortableUtils.TypeChar: - TransferBytes(inStream, outStream, 2); - - break; - - case PortableUtils.TypeBool: - TransferBytes(inStream, outStream, 1); - - break; - - case PortableUtils.TypeDecimal: - TransferBytes(inStream, outStream, 4); // Transfer scale - - int magLen = inStream.ReadInt(); // Transfer magnitude length. - - outStream.WriteInt(magLen); - - TransferBytes(inStream, outStream, magLen); // Transfer magnitude. - - break; - - case PortableUtils.TypeString: - PortableUtils.WriteString(PortableUtils.ReadString(inStream), outStream); - - break; - - case PortableUtils.TypeGuid: - TransferBytes(inStream, outStream, 16); - - break; - - case PortableUtils.TypeDate: - TransferBytes(inStream, outStream, 12); - - break; - - case PortableUtils.TypeArrayByte: - TransferArray(inStream, outStream, 1); - - break; - - case PortableUtils.TypeArrayShort: - TransferArray(inStream, outStream, 2); - - break; - - case PortableUtils.TypeArrayInt: - TransferArray(inStream, outStream, 4); - - break; - - case PortableUtils.TypeArrayLong: - TransferArray(inStream, outStream, 8); - - break; - - case PortableUtils.TypeArrayFloat: - TransferArray(inStream, outStream, 4); - - break; - - case PortableUtils.TypeArrayDouble: - TransferArray(inStream, outStream, 8); - - break; - - case PortableUtils.TypeArrayChar: - TransferArray(inStream, outStream, 2); - - break; - - case PortableUtils.TypeArrayBool: - TransferArray(inStream, outStream, 1); - - break; - - case PortableUtils.TypeArrayDecimal: - case PortableUtils.TypeArrayString: - case PortableUtils.TypeArrayGuid: - case PortableUtils.TypeArrayDate: - case PortableUtils.TypeArrayEnum: - case PortableUtils.TypeArray: - int arrLen = inStream.ReadInt(); - - outStream.WriteInt(arrLen); - - for (int i = 0; i < arrLen; i++) - Mutate0(ctx, inStream, outStream, false, 0, null); - - break; - - case PortableUtils.TypeCollection: - int colLen = inStream.ReadInt(); - - outStream.WriteInt(colLen); - - outStream.WriteByte(inStream.ReadByte()); - - for (int i = 0; i < colLen; i++) - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - - break; - - case PortableUtils.TypeDictionary: - int dictLen = inStream.ReadInt(); - - outStream.WriteInt(dictLen); - - outStream.WriteByte(inStream.ReadByte()); - - for (int i = 0; i < dictLen; i++) - { - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - } - - break; - - case PortableUtils.TypeMapEntry: - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - - break; - - case PortableUtils.TypePortable: - TransferArray(inStream, outStream, 1); // Data array. - TransferBytes(inStream, outStream, 4); // Offset in array. - - break; - - case PortableUtils.TypeEnum: - TransferBytes(inStream, outStream, 4); // Integer ordinal. - - break; - - default: - return false; - } - - return true; - } - - /// - /// Get's metadata field type ID for the given type. - /// - /// Type. - /// Type ID. - private static int TypeId(Type type) - { - int typeId; - - if (TypeIds.TryGetValue(type, out typeId)) - return typeId; - if (type.IsEnum) - return PortableUtils.TypeEnum; - if (type.IsArray) - return type.GetElementType().IsEnum ? PortableUtils.TypeArrayEnum : PortableUtils.TypeArray; - PortableCollectionInfo colInfo = PortableCollectionInfo.Info(type); - - return colInfo.IsAny ? colInfo.IsCollection || colInfo.IsGenericCollection ? - PortableUtils.TypeCollection : PortableUtils.TypeDictionary : PortableUtils.TypeObject; - } - - /// - /// Transfer bytes from one stream to another. - /// - /// Input stream. - /// Output stream. - /// Bytes count. - private static void TransferBytes(PortableHeapStream inStream, IPortableStream outStream, int cnt) - { - outStream.Write(inStream.InternalArray, inStream.Position, cnt); - - inStream.Seek(cnt, SeekOrigin.Current); - } - - /// - /// Transfer array of fixed-size elements from one stream to another. - /// - /// Input stream. - /// Output stream. - /// Element size. - private static void TransferArray(PortableHeapStream inStream, IPortableStream outStream, - int elemSize) - { - int len = inStream.ReadInt(); - - outStream.WriteInt(len); - - TransferBytes(inStream, outStream, elemSize * len); - } - - /// - /// Mutation ocntext. - /// - private class Context - { - /** Map from object position in old portable to position in new portable. */ - private IDictionary _oldToNew; - - /** Parent context. */ - private readonly Context _parent; - - /** Portable writer. */ - private readonly PortableWriterImpl _writer; - - /** Children contexts. */ - private ICollection _children; - - /** Closed flag; if context is closed, it can no longer be used. */ - private bool _closed; - - /// - /// Constructor for parent context where writer invocation is not expected. - /// - public Context() - { - // No-op. - } - - /// - /// Constructor for parent context. - /// - /// Writer - public Context(PortableWriterImpl writer) - { - _writer = writer; - } - - /// - /// Constructor. - /// - /// Parent context. - public Context(Context parent) - { - _parent = parent; - - _writer = parent._writer; - - if (parent._children == null) - parent._children = new List(); - - parent._children.Add(this); - } - - /// - /// Add another old-to-new position mapping. - /// - /// Old position. - /// New position. - /// Handle position. - /// True if ampping was added, false if mapping already existed and handle - /// position in the new object is returned. - public bool AddOldToNew(int oldPos, int newPos, out int hndPos) - { - if (_oldToNew == null) - _oldToNew = new Dictionary(); - - if (_oldToNew.TryGetValue(oldPos, out hndPos)) - return false; - _oldToNew[oldPos] = newPos; - - return true; - } - - /// - /// Get mapping of old position to the new one. - /// - /// Old position. - /// New position. - /// True if mapping exists. - public bool OldToNew(int oldPos, out int newPos) - { - return _oldToNew.TryGetValue(oldPos, out newPos); - } - - /// - /// Writer. - /// - public PortableWriterImpl Writer - { - get { return _writer; } - } - - /// - /// Closed flag. - /// - public bool Closed - { - get - { - return _closed; - } - set - { - Context ctx = this; - - while (ctx != null) - { - ctx._closed = value; - - if (_children != null) { - foreach (Context child in _children) - child.Closed = value; - } - - ctx = ctx._parent; - } - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/f2eb16cd/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableCollectionInfo.cs ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableCollectionInfo.cs b/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableCollectionInfo.cs deleted file mode 100644 index fc61833..0000000 --- a/modules/platform/src/main/dotnet/Apache.Ignite.Core/Impl/Portable/PortableCollectionInfo.cs +++ /dev/null @@ -1,251 +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.Concurrent; - using System.Collections.Generic; - using System.Diagnostics; - using System.Reflection; - using Apache.Ignite.Core.Impl.Common; - - /** - * Collection info helper. - */ - internal class PortableCollectionInfo - { - /** Flag: none. */ - private const byte FlagNone = 0; - - /** Flag: generic dictionary. */ - private const byte FlagGenericDictionary = 1; - - /** Flag: generic collection. */ - private const byte FlagGenericCollection = 2; - - /** Flag: dictionary. */ - private const byte FlagDictionary = 3; - - /** Flag: collection. */ - private const byte FlagCollection = 4; - - /** Cache "none" value. */ - private static readonly PortableCollectionInfo None = - new PortableCollectionInfo(FlagNone, null, null, null); - - /** Cache "dictionary" value. */ - private static readonly PortableCollectionInfo Dictionary = - new PortableCollectionInfo(FlagDictionary, PortableSystemHandlers.WriteHndDictionary, null, null); - - /** Cache "collection" value. */ - private static readonly PortableCollectionInfo Collection = - new PortableCollectionInfo(FlagCollection, PortableSystemHandlers.WriteHndCollection, null, null); - - /** Cached infos. */ - private static readonly IDictionary Infos = - new ConcurrentDictionary(64, 32); - - /** - * Get collection info for type. - * Type. - * Collection info. - */ - public static PortableCollectionInfo Info(Type type) - { - PortableCollectionInfo info; - - if (!Infos.TryGetValue(type, out info)) - { - info = Info0(type); - - Infos[type] = info; - } - - return info; - } - - /** - * Internal routine to get collection info for type. - * Type. - * Collection info. - */ - private static PortableCollectionInfo Info0(Type type) - { - if (type.IsGenericType) - { - if (type.GetGenericTypeDefinition() == PortableUtils.TypGenericDictionary) - { - MethodInfo writeMthd = - PortableUtils.MtdhWriteGenericDictionary.MakeGenericMethod(type.GetGenericArguments()); - MethodInfo readMthd = - PortableUtils.MtdhReadGenericDictionary.MakeGenericMethod(type.GetGenericArguments()); - - return new PortableCollectionInfo(FlagGenericDictionary, - PortableSystemHandlers.WriteHndGenericDictionary, writeMthd, readMthd); - } - - Type genTyp = type.GetInterface(PortableUtils.TypGenericDictionary.FullName); - - if (genTyp != null) - { - MethodInfo writeMthd = - PortableUtils.MtdhWriteGenericDictionary.MakeGenericMethod(genTyp.GetGenericArguments()); - MethodInfo readMthd = - PortableUtils.MtdhReadGenericDictionary.MakeGenericMethod(genTyp.GetGenericArguments()); - - return new PortableCollectionInfo(FlagGenericDictionary, - PortableSystemHandlers.WriteHndGenericDictionary, writeMthd, readMthd); - } - - if (type.GetGenericTypeDefinition() == PortableUtils.TypGenericCollection) - { - MethodInfo writeMthd = - PortableUtils.MtdhWriteGenericCollection.MakeGenericMethod(type.GetGenericArguments()); - MethodInfo readMthd = - PortableUtils.MtdhReadGenericCollection.MakeGenericMethod(type.GetGenericArguments()); - - return new PortableCollectionInfo(FlagGenericCollection, - PortableSystemHandlers.WriteHndGenericCollection, writeMthd, readMthd); - } - - genTyp = type.GetInterface(PortableUtils.TypGenericCollection.FullName); - - if (genTyp != null) - { - MethodInfo writeMthd = - PortableUtils.MtdhWriteGenericCollection.MakeGenericMethod(genTyp.GetGenericArguments()); - MethodInfo readMthd = - PortableUtils.MtdhReadGenericCollection.MakeGenericMethod(genTyp.GetGenericArguments()); - - return new PortableCollectionInfo(FlagGenericCollection, - PortableSystemHandlers.WriteHndGenericCollection, writeMthd, readMthd); - } - } - - if (type == PortableUtils.TypDictionary || type.GetInterface(PortableUtils.TypDictionary.FullName) != null) - return Dictionary; - if (type == PortableUtils.TypCollection || type.GetInterface(PortableUtils.TypCollection.FullName) != null) - return Collection; - return None; - } - - /** Flag. */ - private readonly byte _flag; - - /** Write handler. */ - private readonly PortableSystemWriteDelegate _writeHnd; - - /** Generic write func. */ - private readonly Action _writeFunc; - - /** Generic read func. */ - private readonly Func _readFunc; - - /** - * Constructor. - * Flag. - * Write handler. - * Generic write method. - * Generic read method. - */ - private PortableCollectionInfo(byte flag0, PortableSystemWriteDelegate writeHnd0, - MethodInfo writeMthd0, MethodInfo readMthd0) - { - _flag = flag0; - _writeHnd = writeHnd0; - - if (writeMthd0 != null) - _writeFunc = DelegateConverter.CompileFunc>(null, writeMthd0, null, - new[] {true, false, false}); - - if (readMthd0 != null) - _readFunc = DelegateConverter.CompileFunc>(null, readMthd0, - null, new[] {false, true, false}); - } - - /** - * Generic dictionary flag. - */ - public bool IsGenericDictionary - { - get { return _flag == FlagGenericDictionary; } - } - - /** - * Generic collection flag. - */ - public bool IsGenericCollection - { - get { return _flag == FlagGenericCollection; } - } - - /** - * Dictionary flag. - */ - public bool IsDictionary - { - get { return _flag == FlagDictionary; } - } - - /** - * Collection flag. - */ - public bool IsCollection - { - get { return _flag == FlagCollection; } - } - - /** - * Whether at least one flag is set.. - */ - public bool IsAny - { - get { return _flag != FlagNone; } - } - - /** - * Write handler. - */ - public PortableSystemWriteDelegate WriteHandler - { - get { return _writeHnd; } - } - - /// - /// Reads the generic collection. - /// - public object ReadGeneric(PortableReaderImpl reader) - { - Debug.Assert(reader != null); - Debug.Assert(_readFunc != null); - - return _readFunc(reader, null); - } - - /// - /// Writes the generic collection. - /// - public void WriteGeneric(PortableWriterImpl writer, object value) - { - Debug.Assert(writer != null); - Debug.Assert(_writeFunc != null); - - _writeFunc(value, writer); - } - } -}