Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7CDDB200D20 for ; Fri, 8 Sep 2017 10:26:28 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7BE0A1609C0; Fri, 8 Sep 2017 08:26:28 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 310031609BF for ; Fri, 8 Sep 2017 10:26:27 +0200 (CEST) Received: (qmail 92743 invoked by uid 500); 8 Sep 2017 08:26:26 -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 91656 invoked by uid 99); 8 Sep 2017 08:26:24 -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; Fri, 08 Sep 2017 08:26:24 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 36038F5710; Fri, 8 Sep 2017 08:26:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Fri, 08 Sep 2017 08:26:27 -0000 Message-Id: <2fce35ebb849455ab30a254700b503a5@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [07/27] ignite git commit: IGNITE-6279 .NET: Decouple AbstractQueryCursor from PlatformTarget archived-at: Fri, 08 Sep 2017 08:26:28 -0000 IGNITE-6279 .NET: Decouple AbstractQueryCursor from PlatformTarget This closes #2598 Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e66b98b4 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e66b98b4 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e66b98b4 Branch: refs/heads/ignite-3478 Commit: e66b98b419a90ecbb255d07e17f184c1834837bd Parents: 9d10d20 Author: Pavel Tupitsyn Authored: Wed Sep 6 16:24:41 2017 +0300 Committer: Pavel Tupitsyn Committed: Wed Sep 6 16:24:41 2017 +0300 ---------------------------------------------------------------------- .../Apache.Ignite.Core.csproj | 3 +- .../Impl/Cache/Query/AbstractQueryCursor.cs | 264 ----------------- .../Impl/Cache/Query/FieldsQueryCursor.cs | 2 +- .../Impl/Cache/Query/PlatformQueryQursorBase.cs | 84 ++++++ .../Impl/Cache/Query/QueryCursor.cs | 2 +- .../Impl/Cache/Query/QueryCursorBase.cs | 288 +++++++++++++++++++ 6 files changed, 376 insertions(+), 267 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Apache.Ignite.Core.csproj ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Apache.Ignite.Core.csproj b/modules/platforms/dotnet/Apache.Ignite.Core/Apache.Ignite.Core.csproj index dd40156..0a100f7 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Apache.Ignite.Core.csproj +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Apache.Ignite.Core.csproj @@ -100,6 +100,7 @@ + @@ -321,7 +322,7 @@ - + http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/AbstractQueryCursor.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/AbstractQueryCursor.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/AbstractQueryCursor.cs deleted file mode 100644 index 8e4985e..0000000 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/AbstractQueryCursor.cs +++ /dev/null @@ -1,264 +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.Cache.Query -{ - using System; - using System.Collections; - using System.Collections.Generic; - using Apache.Ignite.Core.Cache.Query; - using Apache.Ignite.Core.Impl.Binary; - using Apache.Ignite.Core.Impl.Binary.IO; - - /// - /// Abstract query cursor implementation. - /// - internal abstract class AbstractQueryCursor : PlatformDisposableTargetAdapter, IQueryCursor, IEnumerator - { - /** */ - private const int OpGetAll = 1; - - /** */ - private const int OpGetBatch = 2; - - /** */ - private const int OpIterator = 4; - - /** */ - private const int OpIteratorClose = 5; - - /** Position before head. */ - private const int BatchPosBeforeHead = -1; - - /** Keep binary flag. */ - private readonly bool _keepBinary; - - /** Wherther "GetAll" was called. */ - private bool _getAllCalled; - - /** Whether "GetEnumerator" was called. */ - private bool _iterCalled; - - /** Batch with entries. */ - private T[] _batch; - - /** Current position in batch. */ - private int _batchPos = BatchPosBeforeHead; - - /// - /// Constructor. - /// - /// Target. - /// Keep binary flag. - protected AbstractQueryCursor(IPlatformTargetInternal target, bool keepBinary) : base(target) - { - _keepBinary = keepBinary; - } - - #region Public methods - - /** */ - public IList GetAll() - { - ThrowIfDisposed(); - - if (_iterCalled) - throw new InvalidOperationException("Failed to get all entries because GetEnumerator() " + - "method has already been called."); - - if (_getAllCalled) - throw new InvalidOperationException("Failed to get all entries because GetAll() " + - "method has already been called."); - - var res = DoInOp(OpGetAll, ConvertGetAll); - - _getAllCalled = true; - - return res; - } - - /** */ - protected override void Dispose(bool disposing) - { - try - { - DoOutInOp(OpIteratorClose); - } - finally - { - base.Dispose(disposing); - } - } - - #endregion - - #region Public IEnumerable methods - - /** */ - public IEnumerator GetEnumerator() - { - ThrowIfDisposed(); - - if (_iterCalled) - throw new InvalidOperationException("Failed to get enumerator entries because " + - "GetEnumerator() method has already been called."); - - if (_getAllCalled) - throw new InvalidOperationException("Failed to get enumerator entries because " + - "GetAll() method has already been called."); - - DoOutInOp(OpIterator); - - _iterCalled = true; - - return this; - } - - /** */ - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - #endregion - - #region Public IEnumerator methods - - /** */ - public T Current - { - get - { - ThrowIfDisposed(); - - if (_batchPos == BatchPosBeforeHead) - throw new InvalidOperationException("MoveNext has not been called."); - - if (_batch == null) - throw new InvalidOperationException("Previous call to MoveNext returned false."); - - return _batch[_batchPos]; - } - } - - /** */ - object IEnumerator.Current - { - get { return Current; } - } - - /** */ - public bool MoveNext() - { - ThrowIfDisposed(); - - if (_batch == null) - { - if (_batchPos == BatchPosBeforeHead) - // Standing before head, let's get batch and advance position. - RequestBatch(); - } - else - { - _batchPos++; - - if (_batch.Length == _batchPos) - // Reached batch end => request another. - RequestBatch(); - } - - return _batch != null; - } - - /** */ - public void Reset() - { - throw new NotSupportedException("Reset is not supported."); - } - - #endregion - - #region Non-public methods - - /// - /// Read entry from the reader. - /// - /// Reader. - /// Entry. - protected abstract T Read(BinaryReader reader); - - /** */ - protected override T1 Unmarshal(IBinaryStream stream) - { - return Marshaller.Unmarshal(stream, _keepBinary); - } - - /// - /// Request next batch. - /// - private void RequestBatch() - { - _batch = DoInOp(OpGetBatch, ConvertGetBatch); - - _batchPos = 0; - } - - /// - /// Converter for GET_ALL operation. - /// - /// Stream. - /// Result. - private IList ConvertGetAll(IBinaryStream stream) - { - var reader = Marshaller.StartUnmarshal(stream, _keepBinary); - - var size = reader.ReadInt(); - - var res = new List(size); - - for (var i = 0; i < size; i++) - res.Add(Read(reader)); - - return res; - } - - /// - /// Converter for GET_BATCH operation. - /// - /// Stream. - /// Result. - private T[] ConvertGetBatch(IBinaryStream stream) - { - var reader = Marshaller.StartUnmarshal(stream, _keepBinary); - - var size = reader.ReadInt(); - - if (size == 0) - return null; - - var res = new T[size]; - - for (var i = 0; i < size; i++) - res[i] = Read(reader); - - return res; - } - - #endregion - - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/FieldsQueryCursor.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/FieldsQueryCursor.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/FieldsQueryCursor.cs index 9d021dc..17dc93b 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/FieldsQueryCursor.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/FieldsQueryCursor.cs @@ -26,7 +26,7 @@ namespace Apache.Ignite.Core.Impl.Cache.Query /// /// Cursor for entry-based queries. /// - internal class FieldsQueryCursor : AbstractQueryCursor + internal class FieldsQueryCursor : PlatformQueryQursorBase { /** */ private readonly Func _readerFunc; http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/PlatformQueryQursorBase.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/PlatformQueryQursorBase.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/PlatformQueryQursorBase.cs new file mode 100644 index 0000000..8a51dab --- /dev/null +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/PlatformQueryQursorBase.cs @@ -0,0 +1,84 @@ +/* + * 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.Cache.Query +{ + using System.Collections.Generic; + + /// + /// Base for platform cursors. + /// + internal abstract class PlatformQueryQursorBase : QueryCursorBase + { + /** */ + private readonly IPlatformTargetInternal _target; + + /** */ + private const int OpGetAll = 1; + + /** */ + private const int OpGetBatch = 2; + + /** */ + private const int OpIterator = 4; + + /** */ + private const int OpIteratorClose = 5; + + /// + /// Initializes a new instance of the class. + /// + /// The target. + /// Keep binary flag. + protected PlatformQueryQursorBase(IPlatformTargetInternal target, bool keepBinary) + : base(target.Marshaller, keepBinary) + { + _target = target; + } + + /** */ + protected override IList GetAllInternal() + { + return _target.OutStream(OpGetAll, ConvertGetAll); + } + + /** */ + protected override void InitIterator() + { + _target.InLongOutLong(OpIterator, 0); + } + + /** */ + protected override T[] GetBatch() + { + return _target.OutStream(OpGetBatch, ConvertGetBatch); + } + + /** */ + protected override void Dispose(bool disposing) + { + try + { + _target.InLongOutLong(OpIteratorClose, 0); + } + finally + { + base.Dispose(disposing); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursor.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursor.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursor.cs index bc3cdb6..b967d6a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursor.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursor.cs @@ -24,7 +24,7 @@ namespace Apache.Ignite.Core.Impl.Cache.Query /// /// Cursor for entry-based queries. /// - internal class QueryCursor : AbstractQueryCursor> + internal class QueryCursor : PlatformQueryQursorBase> { /// /// Constructor. http://git-wip-us.apache.org/repos/asf/ignite/blob/e66b98b4/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursorBase.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursorBase.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursorBase.cs new file mode 100644 index 0000000..418bb24 --- /dev/null +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/Query/QueryCursorBase.cs @@ -0,0 +1,288 @@ +/* + * 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.Cache.Query +{ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Diagnostics; + using Apache.Ignite.Core.Cache.Query; + using Apache.Ignite.Core.Impl.Binary; + using Apache.Ignite.Core.Impl.Binary.IO; + + /// + /// Abstract query cursor implementation. + /// + internal abstract class QueryCursorBase : IQueryCursor, IEnumerator + { + /** Position before head. */ + private const int BatchPosBeforeHead = -1; + + /** Keep binary flag. */ + private readonly bool _keepBinary; + + /** Marshaller. */ + private readonly Marshaller _marsh; + + /** Wherther "GetAll" was called. */ + private bool _getAllCalled; + + /** Whether "GetEnumerator" was called. */ + private bool _iterCalled; + + /** Batch with entries. */ + private T[] _batch; + + /** Current position in batch. */ + private int _batchPos = BatchPosBeforeHead; + + /** Disposed flag. */ + private volatile bool _disposed; + + /// + /// Constructor. + /// + /// Marshaller. + /// Keep binary flag. + protected QueryCursorBase(Marshaller marsh, bool keepBinary) + { + Debug.Assert(marsh != null); + + _keepBinary = keepBinary; + _marsh = marsh; + } + + /** */ + public IList GetAll() + { + ThrowIfDisposed(); + + if (_iterCalled) + throw new InvalidOperationException("Failed to get all entries because GetEnumerator() " + + "method has already been called."); + + if (_getAllCalled) + throw new InvalidOperationException("Failed to get all entries because GetAll() " + + "method has already been called."); + + var res = GetAllInternal(); + + _getAllCalled = true; + + return res; + } + + #region Public IEnumerable methods + + /** */ + public IEnumerator GetEnumerator() + { + ThrowIfDisposed(); + + if (_iterCalled) + { + throw new InvalidOperationException("Failed to get enumerator entries because " + + "GetEnumerator() method has already been called."); + } + + if (_getAllCalled) + { + throw new InvalidOperationException("Failed to get enumerator entries because " + + "GetAll() method has already been called."); + } + + InitIterator(); + + _iterCalled = true; + + return this; + } + + protected abstract void InitIterator(); + + /** */ + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + + #region Public IEnumerator methods + + /** */ + public T Current + { + get + { + ThrowIfDisposed(); + + if (_batchPos == BatchPosBeforeHead) + throw new InvalidOperationException("MoveNext has not been called."); + + if (_batch == null) + throw new InvalidOperationException("Previous call to MoveNext returned false."); + + return _batch[_batchPos]; + } + } + + /** */ + object IEnumerator.Current + { + get { return Current; } + } + + /** */ + public bool MoveNext() + { + ThrowIfDisposed(); + + if (_batch == null) + { + if (_batchPos == BatchPosBeforeHead) + // Standing before head, let's get batch and advance position. + RequestBatch(); + } + else + { + _batchPos++; + + if (_batch.Length == _batchPos) + // Reached batch end => request another. + RequestBatch(); + } + + return _batch != null; + } + + /** */ + public void Reset() + { + throw new NotSupportedException("Reset is not supported."); + } + + #endregion + + /// + /// Gets all entries. + /// + protected abstract IList GetAllInternal(); + + /// + /// Reads entry from the reader. + /// + /// Reader. + /// Entry. + protected abstract T Read(BinaryReader reader); + + /// + /// Requests next batch. + /// + private void RequestBatch() + { + _batch = GetBatch(); + + _batchPos = 0; + } + + /// + /// Gets the next batch. + /// + protected abstract T[] GetBatch(); + + /// + /// Converter for GET_ALL operation. + /// + /// Stream. + /// Result. + protected IList ConvertGetAll(IBinaryStream stream) + { + var reader = _marsh.StartUnmarshal(stream, _keepBinary); + + var size = reader.ReadInt(); + + var res = new List(size); + + for (var i = 0; i < size; i++) + res.Add(Read(reader)); + + return res; + } + + /// + /// Converter for GET_BATCH operation. + /// + /// Stream. + /// Result. + protected T[] ConvertGetBatch(IBinaryStream stream) + { + var reader = _marsh.StartUnmarshal(stream, _keepBinary); + + var size = reader.ReadInt(); + + if (size == 0) + return null; + + var res = new T[size]; + + for (var i = 0; i < size; i++) + res[i] = Read(reader); + + return res; + } + + /** */ + public void Dispose() + { + lock (this) + { + if (_disposed) + return; + + Dispose(true); + + GC.SuppressFinalize(this); + + _disposed = true; + } + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// + /// true when called from Dispose; false when called from finalizer. + /// + protected virtual void Dispose(bool disposing) + { + // No-op. + } + + /// + /// Throws if this instance has been disposed. + /// + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(GetType().Name, "Object has been disposed."); + } + } + } +}