-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStorageContextQueryNow.cs
More file actions
230 lines (180 loc) · 7.95 KB
/
StorageContextQueryNow.cs
File metadata and controls
230 lines (180 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Azure.Data.Tables;
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
namespace CoreHelpers.WindowsAzure.Storage.Table.Internal
{
internal class StorageQueryContext<T>
{
public StorageContext context { get; set; }
public string filter { get; set; } = null;
public int? maxPerPage { get; set; } = null;
public IEnumerable<string> select { get; set; } = null;
public CancellationToken cancellationToken { get; set; } = default(CancellationToken);
}
internal class StorageContextQueryIEnumerator<T> : IEnumerator<T> where T: class, new()
{
private StorageQueryContext<T> _context;
private IEnumerator<Azure.Page<TableEntity>> _pageEnumerator;
private IEnumerator<TableEntity> _inPageEnumerator;
private int resultItemCounter = 0;
public StorageContextQueryIEnumerator(StorageQueryContext<T> queryContext)
{
_context = queryContext;
}
public T Current { get; private set; }
object IEnumerator.Current => this;
public void Dispose()
{
Reset();
}
public bool MoveNext()
{
// initialize the enumerator
InitializePageEnumeratorIfNeeded();
// increase the item counter
resultItemCounter++;
// evaluate the maxItems
int maxItemsAllowed = _context.maxPerPage.HasValue && _context.maxPerPage.Value > 0 ? _context.maxPerPage.Value : -1;
// limit the items to the absolut maximum
if (maxItemsAllowed > 0 && resultItemCounter > maxItemsAllowed)
return false;
// handle the rest internal
return MoveNextInternal(true);
}
private bool MoveNextInternal(bool initialPage)
{
try
{
// check if we need a new page
if (_inPageEnumerator == null)
{
// notify delegate
if (_context.context.GetDelegate() != null)
_context.context.GetDelegate().OnQuerying(typeof(T), _context.filter, _context.maxPerPage.HasValue ? _context.maxPerPage.Value : -1, !initialPage);
// go to the next page or end the enumeration
if (!_pageEnumerator.MoveNext())
return false;
// get the enumerator of the found items
_inPageEnumerator = _pageEnumerator.Current.Values.GetEnumerator();
}
// move to the next item
if (!_inPageEnumerator.MoveNext())
{
// notify delegate
if (_context.context.GetDelegate() != null)
_context.context.GetDelegate().OnQueryed(typeof(T), _context.filter, _context.maxPerPage.HasValue ? _context.maxPerPage.Value : -1, !initialPage, null);
// move forward
_inPageEnumerator = null;
return MoveNextInternal(false);
}
// set the item
Current = TableEntityDynamic.fromEntity<T>(_inPageEnumerator.Current, _context.context.GetEntityMapper<T>(), _context.context);
// done
return true;
} catch (Azure.RequestFailedException e)
{
// notify delegate
if (_context.context.GetDelegate() != null)
_context.context.GetDelegate().OnQueryed(typeof(T), _context.filter, _context.maxPerPage.HasValue ? _context.maxPerPage.Value : -1, !initialPage, e);
if (_context.context.IsAutoCreateTableEnabled() && e.ErrorCode.Equals("TableNotFound"))
return false;
// done
ExceptionDispatchInfo.Capture(e).Throw();
return false;
}
}
public void Reset()
{
if (_pageEnumerator != null)
{
_pageEnumerator.Dispose();
_pageEnumerator = null;
}
if (_inPageEnumerator != null)
{
_inPageEnumerator.Dispose();
_inPageEnumerator = null;
}
}
private void InitializePageEnumeratorIfNeeded()
{
if (_pageEnumerator != null)
return;
// get the table client
var tc = _context.context.GetTableClient<T>();
// evaluate the maxItems
int? maxPerPage = _context.maxPerPage.HasValue && _context.maxPerPage.Value > 0 ? _context.maxPerPage : null;
// fix Azurite bug
var filter = string.IsNullOrWhiteSpace(_context.filter) ? null : _context.filter;
// start the query
_pageEnumerator = tc.Query<TableEntity>(filter, maxPerPage, _context.select, _context.cancellationToken).AsPages().GetEnumerator();
}
}
internal class StorageContextQueryIEnumerable<T> : IEnumerable<T> where T : class, new()
{
private StorageQueryContext<T> _queryContext;
public StorageContextQueryIEnumerable(StorageQueryContext<T> queryContext)
=> _queryContext = queryContext;
public IEnumerator<T> GetEnumerator()
=> new StorageContextQueryIEnumerator<T>(_queryContext);
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}
public class StorageContextQueryNow<T> : IStorageContextQueryNow<T> where T : class, new()
{
protected string _optionalPartitionKey;
protected string _optionalRowKey;
protected string _optionalFilter;
protected int? _optionalMaxItems;
protected StorageContext _context;
public StorageContextQueryNow(StorageContext context, StorageContextQueryNow<T> src)
{
_context = context;
if (src != null)
{
_optionalPartitionKey = src._optionalPartitionKey;
_optionalRowKey = src._optionalRowKey;
_optionalFilter = src._optionalFilter;
_optionalMaxItems = src._optionalMaxItems;
}
}
public StorageContextQueryNow(StorageContext context, StorageContextQueryNow<T> src, string filter, int? maxItems)
: this(context, src)
{
_optionalFilter = filter;
_optionalMaxItems = maxItems;
}
public async Task<IEnumerable<T>> Now()
{
// build the query context
var queryContext = new StorageQueryContext<T>()
{
context = _context,
filter = _optionalFilter,
maxPerPage = _optionalMaxItems,
select = null,
cancellationToken = default(CancellationToken)
};
// build the filter correctly
var filterBuilder = new TableQueryFilterBuilder();
if (!String.IsNullOrEmpty(_optionalPartitionKey))
filterBuilder.And("PartitionKey", QueryFilterOperator.Equal, _optionalPartitionKey);
if (!String.IsNullOrEmpty(_optionalRowKey))
filterBuilder.And("RowKey", QueryFilterOperator.Equal, _optionalRowKey);
if (!String.IsNullOrEmpty(_optionalFilter))
filterBuilder.Attach(_optionalFilter);
// set the filter
queryContext.filter = filterBuilder.Build();
// build the enumerable
await Task.CompletedTask;
return new StorageContextQueryIEnumerable<T>(queryContext);
}
}
}