aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.Database/Jellyfin.Database.Implementations/QueryPartitionHelpers.cs
blob: bb66bddca3761de13864f69c0875ab62a2f377ad (plain)
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
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Jellyfin.Database.Implementations;

/// <summary>
/// Contains helpers to partition EFCore queries.
/// </summary>
public static class QueryPartitionHelpers
{
    /// <summary>
    /// Adds a callback to any directly following calls of Partition for every partition thats been invoked.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="query">The source query.</param>
    /// <param name="beginPartition">The callback invoked for partition before enumerating items.</param>
    /// <param name="endPartition">The callback invoked for partition after enumerating items.</param>
    /// <returns>A queryable that can be used to partition.</returns>
    public static ProgressablePartitionReporting<TEntity> WithPartitionProgress<TEntity>(this IOrderedQueryable<TEntity> query, Action<int>? beginPartition = null, Action<int, TimeSpan>? endPartition = null)
    {
        var progressable = new ProgressablePartitionReporting<TEntity>(query);
        progressable.OnBeginPartition = beginPartition;
        progressable.OnEndPartition = endPartition;
        return progressable;
    }

    /// <summary>
    /// Adds a callback to any directly following calls of Partition for every item thats been invoked.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="query">The source query.</param>
    /// <param name="beginItem">The callback invoked for each item before processing.</param>
    /// <param name="endItem">The callback invoked for each item after processing.</param>
    /// <returns>A queryable that can be used to partition.</returns>
    public static ProgressablePartitionReporting<TEntity> WithItemProgress<TEntity>(this IOrderedQueryable<TEntity> query, Action<TEntity, int, int>? beginItem = null, Action<TEntity, int, int, TimeSpan>? endItem = null)
    {
        var progressable = new ProgressablePartitionReporting<TEntity>(query);
        progressable.OnBeginItem = beginItem;
        progressable.OnEndItem = endItem;
        return progressable;
    }

    /// <summary>
    /// Adds a callback to any directly following calls of Partition for every partition thats been invoked.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="progressable">The source query.</param>
    /// <param name="beginPartition">The callback invoked for partition before enumerating items.</param>
    /// <param name="endPartition">The callback invoked for partition after enumerating items.</param>
    /// <returns>A queryable that can be used to partition.</returns>
    public static ProgressablePartitionReporting<TEntity> WithPartitionProgress<TEntity>(this ProgressablePartitionReporting<TEntity> progressable, Action<int>? beginPartition = null, Action<int, TimeSpan>? endPartition = null)
    {
        progressable.OnBeginPartition = beginPartition;
        progressable.OnEndPartition = endPartition;
        return progressable;
    }

    /// <summary>
    /// Adds a callback to any directly following calls of Partition for every item thats been invoked.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="progressable">The source query.</param>
    /// <param name="beginItem">The callback invoked for each item before processing.</param>
    /// <param name="endItem">The callback invoked for each item after processing.</param>
    /// <returns>A queryable that can be used to partition.</returns>
    public static ProgressablePartitionReporting<TEntity> WithItemProgress<TEntity>(this ProgressablePartitionReporting<TEntity> progressable, Action<TEntity, int, int>? beginItem = null, Action<TEntity, int, int, TimeSpan>? endItem = null)
    {
        progressable.OnBeginItem = beginItem;
        progressable.OnEndItem = endItem;
        return progressable;
    }

    /// <summary>
    /// Enumerates the source query by loading the entities in partitions in a lazy manner reading each item from the database as its requested.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="partitionInfo">The source query.</param>
    /// <param name="partitionSize">The number of elements to load per partition.</param>
    /// <param name="cancellationToken">The cancelation token.</param>
    /// <returns>A enumerable representing the whole of the query.</returns>
    public static async IAsyncEnumerable<TEntity> PartitionAsync<TEntity>(this ProgressablePartitionReporting<TEntity> partitionInfo, int partitionSize, [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        await foreach (var item in partitionInfo.Source.PartitionAsync(partitionSize, partitionInfo, cancellationToken).ConfigureAwait(false))
        {
            yield return item;
        }
    }

    /// <summary>
    /// Enumerates the source query by loading the entities in partitions directly into memory.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="partitionInfo">The source query.</param>
    /// <param name="partitionSize">The number of elements to load per partition.</param>
    /// <param name="cancellationToken">The cancelation token.</param>
    /// <returns>A enumerable representing the whole of the query.</returns>
    public static async IAsyncEnumerable<TEntity> PartitionEagerAsync<TEntity>(this ProgressablePartitionReporting<TEntity> partitionInfo, int partitionSize, [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        await foreach (var item in partitionInfo.Source.PartitionEagerAsync(partitionSize, partitionInfo, cancellationToken).ConfigureAwait(false))
        {
            yield return item;
        }
    }

    /// <summary>
    /// Enumerates the source query by loading the entities in partitions in a lazy manner reading each item from the database as its requested.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="query">The source query.</param>
    /// <param name="partitionSize">The number of elements to load per partition.</param>
    /// <param name="progressablePartition">Reporting helper.</param>
    /// <param name="cancellationToken">The cancelation token.</param>
    /// <returns>A enumerable representing the whole of the query.</returns>
    public static async IAsyncEnumerable<TEntity> PartitionAsync<TEntity>(
        this IOrderedQueryable<TEntity> query,
        int partitionSize,
        ProgressablePartitionReporting<TEntity>? progressablePartition = null,
        [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        var iterator = 0;
        int itemCounter;
        do
        {
            progressablePartition?.BeginPartition(iterator);
            itemCounter = 0;
            await foreach (var item in query
                .Skip(partitionSize * iterator)
                .Take(partitionSize)
                .AsAsyncEnumerable()
                .WithCancellation(cancellationToken)
                .ConfigureAwait(false))
            {
                progressablePartition?.BeginItem(item, iterator, itemCounter);
                yield return item;
                progressablePartition?.EndItem(item, iterator, itemCounter);
                itemCounter++;
            }

            progressablePartition?.EndPartition(iterator);
            iterator++;
        } while (itemCounter == partitionSize && !cancellationToken.IsCancellationRequested);
    }

    /// <summary>
    /// Enumerates the source query by loading the entities in partitions directly into memory.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="query">The source query.</param>
    /// <param name="partitionSize">The number of elements to load per partition.</param>
    /// <param name="progressablePartition">Reporting helper.</param>
    /// <param name="cancellationToken">The cancelation token.</param>
    /// <returns>A enumerable representing the whole of the query.</returns>
    public static async IAsyncEnumerable<TEntity> PartitionEagerAsync<TEntity>(
        this IOrderedQueryable<TEntity> query,
        int partitionSize,
        ProgressablePartitionReporting<TEntity>? progressablePartition = null,
        [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        var iterator = 0;
        int itemCounter;
        var items = ArrayPool<TEntity>.Shared.Rent(partitionSize);
        try
        {
            do
            {
                progressablePartition?.BeginPartition(iterator);
                itemCounter = 0;
                await foreach (var item in query
                    .Skip(partitionSize * iterator)
                    .Take(partitionSize)
                    .AsAsyncEnumerable()
                    .WithCancellation(cancellationToken)
                    .ConfigureAwait(false))
                {
                    items[itemCounter++] = item;
                }

                for (int i = 0; i < itemCounter; i++)
                {
                    progressablePartition?.BeginItem(items[i], iterator, itemCounter);
                    yield return items[i];
                    progressablePartition?.EndItem(items[i], iterator, itemCounter);
                }

                progressablePartition?.EndPartition(iterator);
                iterator++;
            } while (itemCounter == partitionSize && !cancellationToken.IsCancellationRequested);
        }
        finally
        {
            ArrayPool<TEntity>.Shared.Return(items);
        }
    }

    /// <summary>
    /// Adds an Index to the enumeration of the async enumerable.
    /// </summary>
    /// <typeparam name="TEntity">The entity to load.</typeparam>
    /// <param name="query">The source query.</param>
    /// <returns>The source list with an index added.</returns>
    public static async IAsyncEnumerable<(TEntity Item, int Index)> WithIndex<TEntity>(this IAsyncEnumerable<TEntity> query)
    {
        var index = 0;
        await foreach (var item in query.ConfigureAwait(false))
        {
            yield return (item, index++);
        }
    }
}