| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using System.Linq; |
| | 4 | |
|
| | 5 | | namespace Jellyfin.Database.Implementations; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Wrapper for progress reporting on Partition helpers. |
| | 9 | | /// </summary> |
| | 10 | | /// <typeparam name="TEntity">The entity to load.</typeparam> |
| | 11 | | public class ProgressablePartitionReporting<TEntity> |
| | 12 | | { |
| | 13 | | private readonly IOrderedQueryable<TEntity> _source; |
| | 14 | |
|
| 0 | 15 | | private readonly Stopwatch _partitionTime = new(); |
| | 16 | |
|
| 0 | 17 | | private readonly Stopwatch _itemTime = new(); |
| | 18 | |
|
| | 19 | | internal ProgressablePartitionReporting(IOrderedQueryable<TEntity> source) |
| | 20 | | { |
| 0 | 21 | | _source = source; |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | internal Action<TEntity, int, int>? OnBeginItem { get; set; } |
| | 25 | |
|
| | 26 | | internal Action<int>? OnBeginPartition { get; set; } |
| | 27 | |
|
| | 28 | | internal Action<TEntity, int, int, TimeSpan>? OnEndItem { get; set; } |
| | 29 | |
|
| | 30 | | internal Action<int, TimeSpan>? OnEndPartition { get; set; } |
| | 31 | |
|
| 0 | 32 | | internal IOrderedQueryable<TEntity> Source => _source; |
| | 33 | |
|
| | 34 | | internal void BeginItem(TEntity entity, int iteration, int itemIndex) |
| | 35 | | { |
| 0 | 36 | | _itemTime.Restart(); |
| 0 | 37 | | OnBeginItem?.Invoke(entity, iteration, itemIndex); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | internal void BeginPartition(int iteration) |
| | 41 | | { |
| 0 | 42 | | _partitionTime.Restart(); |
| 0 | 43 | | OnBeginPartition?.Invoke(iteration); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | internal void EndItem(TEntity entity, int iteration, int itemIndex) |
| | 47 | | { |
| 0 | 48 | | OnEndItem?.Invoke(entity, iteration, itemIndex, _itemTime.Elapsed); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | internal void EndPartition(int iteration) |
| | 52 | | { |
| 0 | 53 | | OnEndPartition?.Invoke(iteration, _partitionTime.Elapsed); |
| 0 | 54 | | } |
| | 55 | | } |