-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualizedItemsSource.cs
776 lines (681 loc) · 32.3 KB
/
VirtualizedItemsSource.cs
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DNQ.VirtualizedItemsSource
{
/// <summary> A generic virtualized item source that can be used to provide a linear (sequential) view into a large data set. </summary>
///
/// <typeparam name="T"> Generic type parameter for the type of data items in the data set. </typeparam>
public class VirtualizedItemSource<T>
{
private ReaderWriterLockSlim _internalReadWriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private ManualResetEvent _evtReadyToDisplay = new ManualResetEvent(true);
private T[] _cachedItems;
private IBounds _currentBounds;
private int _totalCount = 0; // this indicates the reported total number of items (as reported by the _countSourceElementsFunction
private Operation<T> _currentOperation = null;
private Func<int> _countSourceElementsFunction;
private Func<int, int, CancellationToken, IEnumerable<T>> _retrieveSourceElementsQueryFunction;
private Action<int, string> _logFunction;
/// <summary> Creates a new instance of a virtualized items source. </summary>
///
/// <param name="span"> The number of items to make available ay any one point in the linear/sequential cache. </param>
/// <param name="countSourceElements"> A delegate that will be invoked whenever this object needs to obtain the total number of items available in the source data set. </param>
/// <param name="retrieveSourceElementsQueryFunction"> A delegate that will be invoked by this object to obtain items from the source data set. </param>
/// <param name="logFunction"> A delegate that will be invoked by this object to log certain events. </param>
public VirtualizedItemSource(int span, Func<int> countSourceElements, Func<int, int, CancellationToken, IEnumerable<T>> retrieveSourceElementsQueryFunction, Action<int, string> logFunction)
{
Span = span;
_countSourceElementsFunction = countSourceElements;
_retrieveSourceElementsQueryFunction = retrieveSourceElementsQueryFunction;
_logFunction = logFunction;
}
/// <summary> Event raised after this virtualized items source is reinitialized.
/// It indiactes the success or failure of the operation. </summary>
public event EventHandler<SourceReinitializedEventArgs> SourceReinitialized;
/// <summary> Event raised after a repositioning occures in this virtualized items source.
/// It indicates the success or failure of the operation. </summary>
/// <seealso cref="NotifyRepositionRequired"/>
public event EventHandler<RepositionCompleteEventArgs> SourceRepositioned;
/// <summary>
/// Event raised when a reposition is required in order to be able to retrieve a requested item.
/// </summary>
/// <remarks>
/// The event is raised in order to notify consumers that a repositioning is under way but no other information
/// is provided, nor are consumers given the ability to alter the subsequent repositioning in any way.
///
/// Consumers may use this event to signal in the user interface that a repositions is taking place.
/// The <see cref="SourceRepositioned"/> may then be used to restore the user interface to the normal state.
/// </remarks>
/// <seealso cref="SourceRepositioned"/>
public event EventHandler<EventArgs> NotifyRepositionRequired;
/// <summary> Event raised whenever an error occurs. </summary>
public event EventHandler<OperationErrorEventArgs> NotifyErrorOccured;
/// <summary> Gets the span of elements that are expected to be available at any one time in the virtualized items source.
/// This value is set in the constructor.</summary>
/// <remarks> The span should be large enough to contain all elements that would be visible at one time (i.e. if the
/// list can display 100 visisble items, the span should be at least 100) with a good number being twice
/// or three times that.</remarks>
/// <value> The span. </value>
public int Span
{
get;
private set;
}
/// <summary> Gets the total number of items in the source data set. </summary>
///
/// <value> Integer, total number of data items in the source data set. </value>
public int TotalCount
{
get
{
bool lockAcquired = false;
try
{
if (!_internalReadWriteLock.IsReadLockHeld)
{
_internalReadWriteLock.EnterReadLock();
lockAcquired = true;
}
return _totalCount;
}
finally
{
if (lockAcquired)
{
_internalReadWriteLock.ExitReadLock();
}
}
}
}
/// <summary> Gets an item from the source data set at the given index. </summary>
/// <remarks> This method returns an item from the source data set and handles all the necessary caching operations
/// in order to make this item available.
///
/// Note that the index of the item to be retrieved is relative to the source data set range, not the
/// virtualized item source internal caching.
/// </remarks>
///
/// <param name="index"> Zero-based index of the data item to be retrieved from the source data set. </param>
/// <param name="item"> [out] The data item that is returned. </param>
/// <seealso cref="EnsureItemsAvailable"/>
/// <returns> True if the item can be retrieved, false otherwise. </returns>
public bool GetItem(int index, out T item)
{
item = default(T);
bool doit = false;
int failCount = 0;
do
{
if (!EnsureItemsAvailable(index, index))
failCount++;
doit = false;
bool lockAcquired = false;
try
{
if (!_internalReadWriteLock.IsReadLockHeld)
{
_internalReadWriteLock.EnterReadLock();
lockAcquired = true;
}
int computedIndex = index - _currentBounds.LowBound;
if ((index >= 0 && index < _currentBounds.LowBound) || (index >= _currentBounds.HighBound && index < _totalCount))
{
doit = true;
}
if (!doit && (computedIndex >= 0 && computedIndex < _cachedItems.Length))
{
item = _cachedItems[computedIndex];
}
}
finally
{
if (lockAcquired)
{
_internalReadWriteLock.ExitReadLock();
}
}
} while (doit && failCount < 3);
return item != null;
}
public WaitHandle ReinitializeSource(int expectedCount)
{
_evtReadyToDisplay.Reset();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
T[] newItemsArray = null;
int newHBound = 0;
Exception storedException = null;
try
{
if (expectedCount == -1)
{
expectedCount = _countSourceElementsFunction();
}
newHBound = Math.Min(Span, expectedCount);
newItemsArray = _retrieveSourceElementsQueryFunction(0, newHBound, CancellationToken.None).ToArray();
bool lockAcquired = false;
try
{
_internalReadWriteLock.EnterWriteLock();
lockAcquired = true;
_currentBounds = new Bounds(0, newHBound, 0, newHBound);
_totalCount = expectedCount;
_cachedItems = newItemsArray;
}
finally
{
if (lockAcquired)
{
_internalReadWriteLock.ExitWriteLock();
}
}
sw.Stop();
}
catch(Exception exc)
{
storedException = exc;
}
finally
{
if (SourceReinitialized != null)
{
if (storedException == null)
{
SourceReinitialized(this, new SourceReinitializedEventArgs(expectedCount, sw.Elapsed));
}
else
{
SourceReinitialized(this, new SourceReinitializedEventArgs(storedException, sw.Elapsed));
}
}
if (storedException != null && NotifyErrorOccured != null)
{
NotifyErrorOccured(this, new OperationErrorEventArgs(storedException));
}
}
_evtReadyToDisplay.Set();
});
return _evtReadyToDisplay;
}
/// <summary> Ensures that items between <paramref name="startIndex"> and <paramref name="endIndex"> are available/cached. </summary>
///
/// <param name="startIndex"> The start index of the first item. </param>
/// <param name="endIndex"> The end index of the last item. </param>
///
/// <returns> True if it succeeds, false if it fails. </returns>
public bool EnsureItemsAvailable(int startIndex, int endIndex)
{
OperationType requiredOperation = OperationType.None;
Operation<T> scheduledOperation = null;
bool lockAcquired = false;
try
{
if (!_internalReadWriteLock.IsReadLockHeld)
{
_internalReadWriteLock.EnterReadLock();
lockAcquired = true;
}
if ((startIndex >= 0 && startIndex < _currentBounds.LowBound) || (startIndex >= _currentBounds.HighBound && startIndex < _totalCount) || (endIndex >= 0 && endIndex < _currentBounds.LowBound) || (endIndex >= _currentBounds.HighBound && endIndex < _totalCount))
{
requiredOperation = OperationType.Reposition;
}
else if (startIndex < _currentBounds.LowWater && _currentBounds.LowBound > 0)
{
requiredOperation = OperationType.CacheDown;
}
else if (endIndex > _currentBounds.HighWater && _currentBounds.HighBound < _totalCount)
{
requiredOperation = OperationType.CacheUp;
}
else
{
requiredOperation = OperationType.None;
}
if (_currentOperation != null) // if there's something going on now..
{
if (_currentOperation.OperationType == requiredOperation) // and it's exactly what we would like it to be..
requiredOperation = OperationType.None; // no operation is required anymore
else if (requiredOperation != OperationType.None) // but if it's not what we wanted it to be and we wanted anything but "NONE"
requiredOperation = OperationType.Reposition; // ... then we need to do a reposition (cancel whatever is going on and REPOSITION)
}
}
finally
{
if (lockAcquired)
{
_internalReadWriteLock.ExitReadLock();
}
}
if (requiredOperation != OperationType.None) // if we got here and we need to do something..
{
if (requiredOperation == OperationType.CacheUp)
{
scheduledOperation = ScheduleCacheUp(startIndex, endIndex, Span);
}
else if (requiredOperation == OperationType.CacheDown)
{
scheduledOperation = ScheduleCacheDown(startIndex, endIndex, Span);
}
else if (requiredOperation == OperationType.Reposition)
{
scheduledOperation = ScheduleReposition(startIndex, endIndex, Span);
}
}
_evtReadyToDisplay.WaitOne();
if (scheduledOperation != null)
{
return scheduledOperation.WasSuccessful;
}
return true;
}
#region Private Methods
private Operation<T> ScheduleReposition(int start, int end, int span)
{
if (NotifyRepositionRequired != null)
{
NotifyRepositionRequired(this, EventArgs.Empty);
}
try
{
_internalReadWriteLock.EnterWriteLock();
if (_currentOperation != null)
{
_currentOperation.Cancel();
}
_currentOperation = new Operation<T>(OperationType.Reposition, start, end, span, _currentBounds, _totalCount, RepositionComplete);
_evtReadyToDisplay.Reset();
ThreadPool.QueueUserWorkItem((WaitCallback)RepositionItemsSource, _currentOperation);
return _currentOperation;
}
finally
{
if(_internalReadWriteLock.IsWriteLockHeld)
_internalReadWriteLock.ExitWriteLock();
}
}
private Operation<T> ScheduleCacheDown(int start, int end, int span)
{
try
{
_internalReadWriteLock.EnterWriteLock();
if (_currentOperation != null)
{
if (_logFunction != null)
{
_logFunction(1, string.Format("VirtualizedList Error: Cannot schedule a Cache Down operation when another operation is still pending.."));
}
throw new InvalidOperationException("Cannot schedule a Cache Down operation when another operation is still pending..");
}
_currentOperation = new Operation<T>(OperationType.CacheDown, start, end, span, _currentBounds, _totalCount, CacheUpOrDownComplete);
ThreadPool.QueueUserWorkItem((WaitCallback)CacheDownItemsSource, _currentOperation);
return _currentOperation;
}
finally
{
if (_internalReadWriteLock.IsWriteLockHeld)
_internalReadWriteLock.ExitWriteLock();
}
}
private Operation<T> ScheduleCacheUp(int start, int end, int span)
{
try
{
_internalReadWriteLock.EnterWriteLock();
if (_currentOperation != null)
{
if (_logFunction != null)
{
_logFunction(1, string.Format("VirtualizedList Error: Cannot schedule a Cache Up operation when another operation is still pending.."));
}
throw new InvalidOperationException("Cannot schedule a Cache Up operation when another operation is still pending..");
}
_currentOperation = new Operation<T>(OperationType.CacheUp, start, end, span, _currentBounds, _totalCount, CacheUpOrDownComplete);
ThreadPool.QueueUserWorkItem((WaitCallback)CacheUpItemsSource, _currentOperation);
return _currentOperation;
}
finally
{
if (_internalReadWriteLock.IsWriteLockHeld)
_internalReadWriteLock.ExitWriteLock();
}
}
private void RepositionComplete(IOperationResult<T> operationResult)
{
try{
_internalReadWriteLock.EnterWriteLock();
if (!ReferenceEquals(operationResult.Operation, _currentOperation))
{
if (_logFunction != null)
{
_logFunction(6, string.Format("Cache REPOSITIONING #{0} COMPLETE BUT DISCARDING BECAUSE IT WAS NOT VALID ANYMORE..", operationResult.Operation.OperationNumber));
}
return;
}
_currentOperation = null;
if (operationResult.ResultSet != null && operationResult.NewBounds != null)
{
_currentBounds = operationResult.NewBounds;
_cachedItems = operationResult.ResultSet;
if (_logFunction != null)
{
_logFunction(6, string.Format("Cache REPOSITIONING #{0} COMPLETE .. UPDATED BOUNDS / ITEMS SOURCE.. {1} READY..", operationResult.Operation.OperationNumber, _currentBounds));
}
}
else
{
if (_logFunction != null)
{
_logFunction(6, string.Format("+++> Cache REPOSITIONING #{0} COMPLETE .. NOT UPDATED BOUNDS / ITEMS SOURCE.. {1} READY..", operationResult.Operation.OperationNumber, _currentBounds));
}
}
}
finally
{
if(_internalReadWriteLock.IsWriteLockHeld)
_internalReadWriteLock.ExitWriteLock();
}
if (SourceRepositioned != null)
{
if (operationResult.Exception == null)
{
SourceRepositioned(this, new RepositionCompleteEventArgs(operationResult.MustRefresh));
}
else
{
SourceRepositioned(this, new RepositionCompleteEventArgs(operationResult.Exception));
}
}
_evtReadyToDisplay.Set();
if (operationResult.Exception != null && NotifyErrorOccured != null)
{
NotifyErrorOccured(this, new OperationErrorEventArgs(operationResult.Exception));
}
}
private void CacheUpOrDownComplete(IOperationResult<T> operationResult)
{
var originalOperation = operationResult.Operation;
bool mustRepositionAfterThis = false;
try{
_internalReadWriteLock.EnterWriteLock();
if (!ReferenceEquals(originalOperation, _currentOperation))
return;
_currentOperation = null;
if (operationResult.ResultSet != null && operationResult.NewBounds != null && operationResult.MustRefresh == false)
{
T[] tmp = _cachedItems;
_cachedItems = new T[Math.Min(2 * originalOperation.Span, operationResult.ResultSet.Length + tmp.Length)];
int resultSetLen = operationResult.ResultSet.Length;
int preserveCount = _cachedItems.Length - resultSetLen;
if (originalOperation.OperationType == OperationType.CacheDown)
{
if (resultSetLen + tmp.Length <= _cachedItems.Length)
preserveCount = tmp.Length;
Array.Copy(operationResult.ResultSet, 0, _cachedItems, 0, resultSetLen);
Array.Copy(tmp, 0, _cachedItems, resultSetLen, preserveCount);
}
else if (originalOperation.OperationType == OperationType.CacheUp)
{
Array.Copy(tmp, tmp.Length - preserveCount, _cachedItems, 0, preserveCount);
Array.Copy(operationResult.ResultSet, 0, _cachedItems, preserveCount, resultSetLen);
}
_currentBounds = operationResult.NewBounds;
}
else if (operationResult.MustRefresh) // UN COMMENT THIS WHEN DONE WITH DEBUGGING THE REPOSITIONING PROBLEMS..
{
mustRepositionAfterThis = true;
}
}
finally
{
if (_internalReadWriteLock.IsWriteLockHeld)
_internalReadWriteLock.ExitWriteLock();
}
if (operationResult.Exception != null && NotifyErrorOccured != null)
{
var opErrArgs = new OperationErrorEventArgs(operationResult.Exception);
NotifyErrorOccured(this, opErrArgs);
mustRepositionAfterThis = opErrArgs.ContinueExecution;
}
if(mustRepositionAfterThis)
ScheduleReposition(originalOperation.Start, originalOperation.End, originalOperation.Span);
}
private void CacheDownItemsSource(object boxedOperation)
{
Operation<T> op = boxedOperation as Operation<T>;
Exception storedException = null;
bool mustRefresh = false;
T[] resultSetArray = null;
int newLBound = op.OriginalBounds.LowBound; int newHBound = op.OriginalBounds.HighBound;
int newLWater = op.OriginalBounds.LowWater; int newHWater = op.OriginalBounds.HighWater;
string logMessage = string.Empty;
try
{
int totCount = _countSourceElementsFunction();
if (totCount != op.OriginalTotalCount)
{
mustRefresh = true;
return;
}
logMessage = string.Format("Caching DOWN #{0}: Original {1} ..", op.OperationNumber, op.OriginalBounds);
int howMany = op.Span / 2;
if (op.OriginalBounds.LowBound < howMany)
howMany = op.OriginalBounds.LowBound;
if (howMany > 0)
{
resultSetArray = _retrieveSourceElementsQueryFunction(op.OriginalBounds.LowBound - howMany, howMany, op.CancellationToken).ToArray();
logMessage = string.Format("{0} adding {1} records .. ", logMessage, howMany);
var resultingItemsSourceSize = Math.Min(2 * op.Span, resultSetArray.Length + (op.OriginalBounds.HighBound - op.OriginalBounds.LowBound));
newLBound = op.OriginalBounds.LowBound - howMany;
newLWater = op.OriginalBounds.LowWater - howMany;
newHBound = newLBound + resultingItemsSourceSize;
newHWater = op.OriginalBounds.HighWater - howMany;
}
}
catch (Exception exc)
{
storedException = exc;
}
finally
{
if (!op.WasCancelled)
{
if (storedException != null)
{
if (_logFunction != null)
{
_logFunction(6, string.Format("{0} --> Failed!", logMessage));
}
op.SetComplete(storedException);
}
else
{
IBounds newBounds = new Bounds(newLBound, newHBound, newLWater, newHWater);
if (_logFunction != null)
{
_logFunction(6, string.Format("{0} --> New {1}", logMessage, newBounds));
}
op.SetComplete(resultSetArray, newBounds, mustRefresh);
}
}
}
}
private void CacheUpItemsSource(object boxedOperation)
{
Operation<T> op = boxedOperation as Operation<T>;
Exception storedException = null;
bool mustRefresh = false;
T[] resultSetArray = null;
int newLBound = op.OriginalBounds.LowBound; int newHBound = op.OriginalBounds.HighBound;
int newLWater = op.OriginalBounds.LowWater; int newHWater = op.OriginalBounds.HighWater;
string logMessage = string.Empty;
try
{
int totCount = _countSourceElementsFunction();
if (totCount != op.OriginalTotalCount)
{
mustRefresh = true;
return;
}
logMessage = string.Format("Caching UP #{0}: Original {1} ..", op.OperationNumber, op.OriginalBounds);
int howMany = op.Span / 2;
if ((totCount - op.OriginalBounds.HighBound) < howMany)
howMany = (totCount - op.OriginalBounds.HighBound);
if (howMany > 0)
{
resultSetArray = _retrieveSourceElementsQueryFunction(op.OriginalBounds.HighBound, howMany, op.CancellationToken).ToArray();
logMessage = string.Format("{0} adding {1} records .. ", logMessage, howMany);
var resultingItemsSourceSize = Math.Min(2 * op.Span, resultSetArray.Length + (op.OriginalBounds.HighBound - op.OriginalBounds.LowBound));
newHBound = op.OriginalBounds.HighBound + howMany;
newLBound = newHBound - resultingItemsSourceSize;
newHWater = op.OriginalBounds.HighWater + howMany;
newLWater = op.OriginalBounds.LowWater + howMany;
}
}
catch (Exception exc)
{
storedException = exc;
}
finally
{
if (!op.WasCancelled)
{
if (storedException != null)
{
if (_logFunction != null)
{
_logFunction(6, string.Format("{0} --> Failed!", logMessage));
}
op.SetComplete(storedException);
}
else
{
IBounds newBounds = new Bounds(newLBound, newHBound, newLWater, newHWater);
if (_logFunction != null)
{
_logFunction(6, string.Format("{0} --> New {1}", logMessage, newBounds));
}
op.SetComplete(resultSetArray, newBounds, mustRefresh);
}
}
}
}
private void RepositionItemsSource(object boxedOperation)
{
Operation<T> op = boxedOperation as Operation<T>;
Exception storedException = null;
T[] newItemsArray = null;
int newLBound = 0; int newHBound = 0;
int newLWater = 0; int newHWater = 0;
bool mustRefresh = false;
try
{
int howMany = op.Span * 2;
int totCount = _countSourceElementsFunction();
if (op.WasCancelled)
return;
if (totCount != op.OriginalTotalCount)
{
newHBound = Math.Min(Span * 2, totCount);
newHWater = Math.Min(Span, newHBound);
if (_logFunction != null)
{
_logFunction(6, string.Format("Cache REPOSITIONING #{0}: Original {1} .. (TotalCount {2} != Op.OriginalCount {3})", op.OperationNumber, op.OriginalBounds, totCount, op.OriginalTotalCount));
}
newItemsArray = _retrieveSourceElementsQueryFunction(0, newHBound, op.CancellationToken).ToArray();
mustRefresh = true;
return;
}
if (op.WasCancelled)
return;
if (_logFunction != null)
{
_logFunction(6, string.Format("Cache REPOSITIONING #{0}: Original {1} .. ", op.OperationNumber, op.OriginalBounds));
}
int center = (op.Start - op.End) / 2 + op.Start;
if (center - op.Span < 0)
{
newLBound = 0;
if (newLBound + op.Span * 2 >= totCount)
{
newHBound = totCount;
newLWater = 0;
newHWater = totCount;
}
else
{
newHBound = newLBound + 2 * op.Span;
newLWater = newLBound + op.Span / 2;
newHWater = newHBound - op.Span / 2;
}
}
else if (center + op.Span >= totCount)
{
newLBound = Math.Max(totCount - op.Span * 2, 0);
newHBound = totCount;
if (newHBound - newLBound > op.Span * 2)
{
newLWater = newLBound + op.Span / 2;
newHWater = newHWater - op.Span / 2;
}
else
{
newLWater = newLBound;
newHWater = newHBound;
}
}
else
{
newLBound = center - op.Span;
newHBound = newLBound + op.Span * 2;
newLWater = newLBound + op.Span / 2;
newHWater = newHBound - op.Span / 2;
}
howMany = newHBound - newLBound;
if (op.WasCancelled)
return;
newItemsArray = _retrieveSourceElementsQueryFunction(newLBound, howMany, op.CancellationToken).ToArray();
if (newItemsArray.Length != howMany)
{
if (_logFunction != null)
{
_logFunction(1, string.Format("VirtualizedList records cache failed to reposition. Expected {0} records and instead received {1} (new low bound was {2})", howMany, newItemsArray.Length, newLBound));
}
throw new Exception(string.Format("VirtualizedList records cache failed to reposition. Expected {0} records and instead received {1} (new low bound was {2})", howMany, newItemsArray.Length, newLBound));
}
if (op.WasCancelled)
return;
}
catch (Exception exc)
{
storedException = exc;
}
finally
{
if (!op.WasCancelled)
{
if (storedException != null)
{
op.SetComplete(storedException);
}
else
{
op.SetComplete(newItemsArray, new Bounds(newLBound, newHBound, newLWater, newHWater), mustRefresh);
}
}
else
{
if (_logFunction != null)
{
_logFunction(6, string.Format("Cache REPOSITIONING #{0} - Cancelled.. Aborted! Not Firing Callback!", op.OperationNumber));
}
}
}
}
#endregion
}
}