| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | | 3 | | |
| | | 4 | | using Microsoft.Azure.Batch.Utils; |
| | | 5 | | using System; |
| | | 6 | | using System.Collections; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using System.Threading; |
| | | 12 | | |
| | | 13 | | namespace Microsoft.Azure.Batch |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Implementents sync and async enumerator based on async calls to server. |
| | | 17 | | /// </summary> |
| | | 18 | | internal abstract class PagedEnumeratorBase<EnumerationType> : IPagedEnumerator<EnumerationType>, IEnumerator<Enumer |
| | | 19 | | { |
| | | 20 | | private readonly IPagedEnumerator<EnumerationType> _implInstance; // simplifies complexities around explicit dec |
| | | 21 | | |
| | | 22 | | // index into current batch |
| | | 23 | | internal int _currentIndex; |
| | | 24 | | |
| | | 25 | | // current batch of objects returned by last call |
| | | 26 | | internal object[] _currentBatch; |
| | | 27 | | |
| | | 28 | | // manages the skip token |
| | | 29 | | private SkipTokenHandler _skipHandler; |
| | | 30 | | |
| | | 31 | | #region // constructors |
| | | 32 | | |
| | | 33 | | internal PagedEnumeratorBase() |
| | | 34 | | { |
| | | 35 | | _implInstance = this; |
| | | 36 | | |
| | | 37 | | // sets up state variables to trigger first call |
| | | 38 | | this.Reset(); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | #endregion // constructors |
| | | 42 | | |
| | | 43 | | #region // abstract methods that must be implemented by inheriting class |
| | | 44 | | |
| | | 45 | | // for IPagedEnumerator<T> and IEnumerator<T> |
| | | 46 | | public abstract EnumerationType Current { get;} |
| | | 47 | | |
| | | 48 | | protected abstract System.Threading.Tasks.Task GetNextBatchFromServerAsync(SkipTokenHandler skipHandler, Cancell |
| | | 49 | | |
| | | 50 | | |
| | | 51 | | #endregion // abstract methods |
| | | 52 | | |
| | | 53 | | object System.Collections.IEnumerator.Current // for IEnumerator |
| | | 54 | | { |
| | | 55 | | get |
| | | 56 | | { |
| | | 57 | | object cur = _implInstance.Current; |
| | | 58 | | |
| | | 59 | | return cur; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public bool MoveNext() // for IEnumerator and IEnumerator<T> |
| | | 64 | | { |
| | | 65 | | Task<bool> asyncTask = MoveNextAsync(); |
| | | 66 | | bool result = asyncTask.WaitAndUnaggregateException(); |
| | | 67 | | |
| | | 68 | | return result; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<bool> MoveNextAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | | 75 | | // move to next item in current batch |
| | | 76 | | ++_currentIndex; |
| | | 77 | | |
| | | 78 | | // if the index fits in the current batch, return true |
| | | 79 | | if ((null != _currentBatch) && (_currentIndex < _currentBatch.Length)) // we have results in memory, jus |
| | | 80 | | { |
| | | 81 | | return true; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | // if we are out of data then return false |
| | | 85 | | if (_skipHandler.AtLeastOneCallMade && !_skipHandler.ThereIsMoreData) |
| | | 86 | | { |
| | | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // at this point we need to call for more data |
| | | 91 | | System.Threading.Tasks.Task asyncTask = this.GetNextBatchFromServerAsync(_skipHandler, cancellationToken |
| | | 92 | | |
| | | 93 | | // wait for it to complete |
| | | 94 | | await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| | | 95 | | |
| | | 96 | | // if we have new data in cache, serve it up |
| | | 97 | | if ((null != _currentBatch) && (_currentBatch.Length > 0)) |
| | | 98 | | { |
| | | 99 | | _currentIndex = 0; |
| | | 100 | | |
| | | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | catch |
| | | 105 | | { |
| | | 106 | | throw; // set breakpoint here |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | return false; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public void Reset() |
| | | 113 | | { |
| | | 114 | | Task asyncTask = ResetAsync(); |
| | | 115 | | asyncTask.WaitAndUnaggregateException(); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public Task ResetAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| | | 119 | | { |
| | | 120 | | _skipHandler = new SkipTokenHandler(); |
| | | 121 | | _currentIndex = -1; |
| | | 122 | | _currentBatch = null; |
| | | 123 | | return Async.CompletedTask; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | // IDisposable |
| | | 127 | | public void Dispose() |
| | | 128 | | { |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | internal class SkipTokenHandler |
| | | 133 | | { |
| | | 134 | | private string _skipToken; |
| | | 135 | | private bool _hasBeenCalled = false; |
| | | 136 | | |
| | 0 | 137 | | public bool AtLeastOneCallMade { get { return _hasBeenCalled; } set { _hasBeenCalled = value; } } |
| | | 138 | | |
| | | 139 | | public string SkipToken |
| | | 140 | | { |
| | | 141 | | get |
| | | 142 | | { |
| | 61 | 143 | | return _skipToken; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | set |
| | | 147 | | { |
| | 53 | 148 | | _skipToken = value; |
| | 53 | 149 | | _hasBeenCalled = true; |
| | 53 | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | 60 | 153 | | public bool ThereIsMoreData { get { return !string.IsNullOrEmpty(_skipToken); } } |
| | | 154 | | } |
| | | 155 | | } |