Skip to content

Commit f7d6db0

Browse files
committed
Added sync progress definition.
1 parent 80b7dd3 commit f7d6db0

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncImplementation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ async Task HandleInstruction(Instruction instruction)
493493
Options.Remote.InvalidateCredentials();
494494
break;
495495
case FetchCredentials:
496-
await Options.Remote.PrefetchCredentials();
496+
Options.Remote.InvalidateCredentials();
497497
break;
498498
case CloseSyncStream:
499499
CancellationTokenSource?.Cancel();
Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,117 @@
11
namespace PowerSync.Common.DB.Crud;
22

3-
public class SyncProgress
3+
/// <summary>
4+
/// Provides realtime progress on how PowerSync is downloading rows.
5+
///
6+
/// The reported progress always reflects the status towards th end of a sync iteration (after
7+
/// which a consistent snapshot of all buckets is available locally).
8+
///
9+
/// In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets)
10+
/// operation takes place between syncs), it's possible for the returned numbers to be slightly
11+
/// inaccurate. For this reason, the sync progress should be seen as an approximation of progress.
12+
/// The information returned is good enough to build progress bars, but not exact enough to track
13+
/// individual download counts.
14+
///
15+
/// Also note that data is downloaded in bulk, which means that individual counters are unlikely
16+
/// to be updated one-by-one.
17+
/// </summary>
18+
public class SyncProgress : ProgressWithOperations
419
{
520
public static readonly int FULL_SYNC_PRIORITY = 2147483647;
21+
22+
private InternalProgressInformation internalProgress;
23+
24+
public SyncProgress(InternalProgressInformation progress)
25+
{
26+
this.internalProgress = progress;
27+
var untilCompletion = UntilPriority(FULL_SYNC_PRIORITY);
28+
29+
TotalOperations = untilCompletion.TotalOperations;
30+
DownloadedOperations = untilCompletion.DownloadedOperations;
31+
DownloadedFraction = untilCompletion.DownloadedFraction;
32+
}
33+
34+
private ProgressWithOperations UntilPriority(int priority)
35+
{
36+
var total = 0;
37+
var downloaded = 0;
38+
39+
foreach (var progress in internalProgress.Buckets.Values)
40+
{
41+
// Include higher-priority buckets, which are represented by lower numbers.
42+
if (progress.Priority <= priority)
43+
{
44+
downloaded += progress.SinceLast;
45+
total += progress.TargetCount - progress.AtLast;
46+
}
47+
}
48+
49+
return new ProgressWithOperations
50+
{
51+
TotalOperations = total,
52+
DownloadedOperations = downloaded,
53+
DownloadedFraction = total == 0 ? 1.0 : (double)downloaded / total
54+
};
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Represents progress information for sync operations.
60+
/// </summary>
61+
public class InternalProgressInformation
62+
{
63+
/// <summary>
64+
/// Dictionary mapping bucket names to their progress information.
65+
/// </summary>
66+
public Dictionary<string, BucketProgressInfo> Buckets { get; set; } = new();
67+
}
68+
69+
/// <summary>
70+
/// Represents progress information for a single bucket.
71+
/// </summary>
72+
public class BucketProgressInfo
73+
{
74+
/// <summary>
75+
/// Priority of the associated buckets
76+
/// </summary>
77+
public int Priority { get; set; }
78+
79+
/// <summary>
80+
/// Total ops at last completed sync, or 0
81+
/// </summary>
82+
public int AtLast { get; set; }
83+
84+
/// <summary>
85+
/// Total ops since the last completed sync
86+
/// </summary>
87+
public int SinceLast { get; set; }
88+
89+
/// <summary>
90+
/// Total opcount for next checkpoint as indicated by service
91+
/// </summary>
92+
public int TargetCount { get; set; }
93+
}
94+
95+
/// <summary>
96+
/// Information about a progressing download made by the PowerSync SDK.
97+
///
98+
/// </summary>
99+
public class ProgressWithOperations
100+
{
101+
/// <summary>
102+
/// The total number of operations to download for the current sync iteration to complete.
103+
/// </summary>
104+
public int TotalOperations { get; set; }
105+
106+
/// <summary>
107+
/// The numnber of operations that have already been downloaded.
108+
/// </summary>
109+
public int DownloadedOperations { get; set; }
110+
111+
/// <summary>
112+
/// This will be a number between 0.0 and 1.0 (inclusive).
113+
///
114+
/// When this number reaches 1.0, all changes have been received from the sync service.
115+
/// </summary>
116+
public double DownloadedFraction { get; set; }
6117
}

0 commit comments

Comments
 (0)