-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualizingCollection.cs
More file actions
543 lines (458 loc) · 17.4 KB
/
VirtualizingCollection.cs
File metadata and controls
543 lines (458 loc) · 17.4 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace DataVirtualization
{
/// <summary>
/// Specialized list implementation that provides data virtualization. The collection is divided up into pages,
/// and pages are dynamically fetched from the IItemsProvider when required. Stale pages are removed after a
/// configurable period of time.
/// Intended for use with large collections on a network or disk resource that cannot be instantiated locally
/// due to memory consumption or fetch latency.
/// </summary>
/// <remarks>
/// The IList implmentation is not fully complete, but should be sufficient for use as read only collection
/// data bound to a suitable ItemsControl.
/// </remarks>
/// <typeparam name="T"></typeparam>
public class VirtualizingCollection<T> : IList<T>, IList
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="VirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="pageTimeout">The page timeout.</param>
public VirtualizingCollection(IItemsProvider<T> itemsProvider, int pageSize, int pageTimeout)
{
_itemsProvider = itemsProvider;
_pageSize = pageSize;
_pageTimeout = pageTimeout;
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
/// <param name="pageSize">Size of the page.</param>
public VirtualizingCollection(IItemsProvider<T> itemsProvider, int pageSize)
{
_itemsProvider = itemsProvider;
_pageSize = pageSize;
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
public VirtualizingCollection(IItemsProvider<T> itemsProvider)
{
_itemsProvider = itemsProvider;
}
#endregion
#region ItemsProvider
private readonly IItemsProvider<T> _itemsProvider;
/// <summary>
/// Gets the items provider.
/// </summary>
/// <value>The items provider.</value>
public IItemsProvider<T> ItemsProvider
{
get { return _itemsProvider; }
}
#endregion
#region PageSize
private readonly int _pageSize = 100;
/// <summary>
/// Gets the size of the page.
/// </summary>
/// <value>The size of the page.</value>
public int PageSize
{
get { return _pageSize; }
}
#endregion
#region PageTimeout
private readonly long _pageTimeout = 10000;
/// <summary>
/// Gets the page timeout.
/// </summary>
/// <value>The page timeout.</value>
public long PageTimeout
{
get { return _pageTimeout; }
}
#endregion
#region IList<T>, IList
#region Count
private int _count = -1;
/// <summary>
/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// The first time this property is accessed, it will fetch the count from the IItemsProvider.
/// </summary>
/// <value></value>
/// <returns>
/// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// </returns>
public virtual int Count
{
get
{
if (_count == -1)
{
LoadCount();
}
return _count;
}
protected set
{
_count = value;
}
}
#endregion
#region Indexer
/// <summary>
/// Gets the item at the specified index. This property will fetch
/// the corresponding page from the IItemsProvider if required.
/// </summary>
/// <value></value>
public T this[int index]
{
get
{
// determine which page and offset within page
int pageIndex = index / PageSize;
int pageOffset = index % PageSize;
// request primary page
RequestPage(pageIndex);
// if accessing upper 50% then request next page
if ( pageOffset > PageSize/2 && pageIndex < Count / PageSize)
RequestPage(pageIndex + 1);
// if accessing lower 50% then request prev page
if (pageOffset < PageSize/2 && pageIndex > 0)
RequestPage(pageIndex - 1);
// remove stale pages
CleanUpPages();
// defensive check in case of async load
if (_pages[pageIndex] == null)
return default(T);
// return requested item
return _pages[pageIndex][pageOffset];
}
set { throw new NotSupportedException(); }
}
object IList.this[int index]
{
get { return this[index]; }
set { throw new NotSupportedException(); }
}
#endregion
#region IEnumerator<T>, IEnumerator
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <remarks>
/// This method should be avoided on large collections due to poor performance.
/// </remarks>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return this[i];
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Add
/// <summary>
/// Not supported.
/// </summary>
/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
/// </exception>
public void Add(T item)
{
throw new NotSupportedException();
}
int IList.Add(object value)
{
throw new NotSupportedException();
}
#endregion
#region Contains
bool IList.Contains(object value)
{
return Contains((T)value);
}
/// <summary>
/// Not supported.
/// </summary>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
/// <returns>
/// Always false.
/// </returns>
public bool Contains(T item)
{
return false;
}
#endregion
#region Clear
/// <summary>
/// Not supported.
/// </summary>
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
/// </exception>
public void Clear()
{
throw new NotSupportedException();
}
#endregion
#region IndexOf
int IList.IndexOf(object value)
{
return IndexOf((T) value);
}
/// <summary>
/// Not supported
/// </summary>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
/// <returns>
/// Always -1.
/// </returns>
public int IndexOf(T item)
{
return -1;
}
#endregion
#region Insert
/// <summary>
/// Not supported.
/// </summary>
/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
/// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
/// </exception>
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
void IList.Insert(int index, object value)
{
Insert(index, (T)value);
}
#endregion
#region Remove
/// <summary>
/// Not supported.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
/// </exception>
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
void IList.Remove(object value)
{
throw new NotSupportedException();
}
/// <summary>
/// Not supported.
/// </summary>
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
/// <returns>
/// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// </returns>
/// <exception cref="T:System.NotSupportedException">
/// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
/// </exception>
public bool Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region CopyTo
/// <summary>
/// Not supported.
/// </summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="array"/> is null.
/// </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="arrayIndex"/> is less than 0.
/// </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="array"/> is multidimensional.
/// -or-
/// <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
/// -or-
/// The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.
/// -or-
/// Type <paramref name="T"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.
/// </exception>
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotSupportedException();
}
void ICollection.CopyTo(Array array, int index)
{
throw new NotSupportedException();
}
#endregion
#region Misc
/// <summary>
/// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
/// </summary>
/// <value></value>
/// <returns>
/// An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
/// </returns>
public object SyncRoot
{
get { return this; }
}
/// <summary>
/// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
/// </summary>
/// <value></value>
/// <returns>Always false.
/// </returns>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
/// </summary>
/// <value></value>
/// <returns>Always true.
/// </returns>
public bool IsReadOnly
{
get { return true; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> has a fixed size.
/// </summary>
/// <value></value>
/// <returns>Always false.
/// </returns>
public bool IsFixedSize
{
get { return false; }
}
#endregion
#endregion
#region Paging
private readonly Dictionary<int, IList<T>> _pages = new Dictionary<int, IList<T>>();
private readonly Dictionary<int, DateTime> _pageTouchTimes = new Dictionary<int, DateTime>();
/// <summary>
/// Cleans up any stale pages that have not been accessed in the period dictated by PageTimeout.
/// </summary>
public void CleanUpPages()
{
List<int> keys = new List<int>(_pageTouchTimes.Keys);
foreach (int key in keys)
{
// page 0 is a special case, since WPF ItemsControl access the first item frequently
if ( key != 0 && (DateTime.Now - _pageTouchTimes[key]).TotalMilliseconds > PageTimeout )
{
_pages.Remove(key);
_pageTouchTimes.Remove(key);
Trace.WriteLine("Removed Page: " + key);
}
}
}
/// <summary>
/// Populates the page within the dictionary.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="page">The page.</param>
protected virtual void PopulatePage(int pageIndex, IList<T> page)
{
Trace.WriteLine("Page populated: "+pageIndex);
if ( _pages.ContainsKey(pageIndex) )
_pages[pageIndex] = page;
}
/// <summary>
/// Makes a request for the specified page, creating the necessary slots in the dictionary,
/// and updating the page touch time.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
protected virtual void RequestPage(int pageIndex)
{
if (!_pages.ContainsKey(pageIndex))
{
_pages.Add(pageIndex, null);
_pageTouchTimes.Add(pageIndex, DateTime.Now);
Trace.WriteLine("Added page: " + pageIndex);
LoadPage(pageIndex);
}
else
{
_pageTouchTimes[pageIndex] = DateTime.Now;
}
}
#endregion
#region Load methods
/// <summary>
/// Loads the count of items.
/// </summary>
protected virtual void LoadCount()
{
Count = FetchCount();
}
/// <summary>
/// Loads the page of items.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
protected virtual void LoadPage(int pageIndex)
{
PopulatePage(pageIndex, FetchPage(pageIndex));
}
#endregion
#region Fetch methods
/// <summary>
/// Fetches the requested page from the IItemsProvider.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <returns></returns>
protected IList<T> FetchPage(int pageIndex)
{
return ItemsProvider.FetchRange(pageIndex*PageSize, PageSize);
}
/// <summary>
/// Fetches the count of itmes from the IItemsProvider.
/// </summary>
/// <returns></returns>
protected int FetchCount()
{
return ItemsProvider.FetchCount();
}
#endregion
}
}