-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathPage.cs
104 lines (90 loc) · 3.35 KB
/
Page.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Waher.Persistence.Filters;
using Waher.Persistence.Serialization;
namespace Waher.Persistence.Files
{
/// <summary>
/// Contains a page of items.
/// </summary>
/// <typeparam name="T">Type of objects on the page.</typeparam>
public class Page<T> : IPage<T>
where T : class
{
private readonly int pageSize;
private readonly string collection;
private readonly Filter filter;
private readonly string[] sortOrder;
private readonly string objectIdName;
private readonly IEnumerable<T> items;
private readonly ObjectSerializer serializer;
private readonly FilesProvider provider;
private readonly T lastItem;
private readonly bool hasLastItem;
/// <summary>
/// Contains a page of items.
/// </summary>
/// <param name="PageSize">Number of items on a page.</param>
/// <param name="Collection">Optional collection name.</param>
/// <param name="Filter">Optional filter. Can be null.</param>
/// <param name="SortOrder">Sort order. Each string represents a field name. By default, sort order is ascending.
/// If descending sort order is desired, prefix the field name by a hyphen (minus) sign.</param>
/// <param name="Items">Items on page.</param>
/// <param name="Serializer">Object serializer.</param>
/// <param name="Provider">Current database provider.</param>
public Page(int PageSize, string Collection, Filter Filter, string[] SortOrder,
IEnumerable<T> Items, ObjectSerializer Serializer, FilesProvider Provider)
{
this.pageSize = PageSize;
this.collection = Collection;
this.filter = Filter;
this.sortOrder = SortOrder;
this.items = Items;
this.serializer = Serializer;
this.provider = Provider;
this.objectIdName = this.serializer.ObjectIdMemberName;
if (string.IsNullOrEmpty(this.objectIdName))
throw new IOException("Paginated objects must implement Object IDs.");
this.hasLastItem = false;
foreach (T Item in Items)
{
this.lastItem = Item;
this.hasLastItem = true;
}
}
/// <summary>
/// Items available in the page. The enumeration may be empty.
/// </summary>
public IEnumerable<T> Items => this.items;
/// <summary>
/// If there may be more pages following this page.
/// </summary>
public bool More => this.hasLastItem;
/// <summary>
/// Last item on page.
/// </summary>
public T LastItem => this.lastItem;
/// <summary>
/// Finds the next page of objects of a given class <typeparamref name="T"/>.
/// </summary>
/// <returns>Next page, directly following the current page.</returns>
public async Task<IPage<T>> FindNext()
{
if (!this.hasLastItem)
return new EmptyPage<T>();
Guid LastObjectId = await this.serializer.GetObjectId(this.lastItem, false, null);
if (LastObjectId == Guid.Empty)
return new EmptyPage<T>();
// Note: GUIDs generated by the Files Provider are ascending in time.
IEnumerable<T> NewItems;
if (string.IsNullOrEmpty(this.collection))
NewItems = await this.provider.Find(0, this.pageSize, this.filter, this.lastItem, this.sortOrder);
else
NewItems = await this.provider.Find(this.collection, 0, this.pageSize, this.filter, this.lastItem, this.sortOrder);
return new Page<T>(this.pageSize, this.collection, this.filter, this.sortOrder,
NewItems, this.serializer, this.provider);
}
}
}