@@ -81,8 +81,8 @@ public async Task AddManyChanges(Guid clientId,
8181 repo . ClearChangeTracker ( ) ;
8282
8383 await using var transaction = await repo . BeginTransactionAsync ( ) ;
84- await repo . AddCommits ( commits ) ;
85- await UpdateSnapshots ( repo , commits . First ( ) , commits ) ;
84+ var updatedCommits = await repo . AddCommits ( commits ) ;
85+ await UpdateSnapshots ( repo , updatedCommits ) ;
8686 await ValidateCommits ( repo ) ;
8787 await transaction . CommitAsync ( ) ;
8888 }
@@ -119,8 +119,8 @@ private async Task Add(Commit commit)
119119 repo . ClearChangeTracker ( ) ;
120120
121121 await using var transaction = repo . IsInTransaction ? null : await repo . BeginTransactionAsync ( ) ;
122- await repo . AddCommit ( commit ) ;
123- await UpdateSnapshots ( repo , commit , [ commit ] ) ;
122+ var updatedCommits = await repo . AddCommit ( commit ) ;
123+ await UpdateSnapshots ( repo , updatedCommits ) ;
124124
125125 if ( AlwaysValidate ) await ValidateCommits ( repo ) ;
126126
@@ -156,8 +156,8 @@ async Task ISyncable.AddRangeFromSync(IEnumerable<Commit> commits)
156156
157157 await using var transaction = await repo . BeginTransactionAsync ( ) ;
158158 //don't save since UpdateSnapshots will also modify newCommits with hashes, so changes will be saved once that's done
159- await repo . AddCommits ( newCommits , false ) ;
160- await UpdateSnapshots ( repo , oldestChange , newCommits ) ;
159+ var updatedCommits = await repo . AddCommits ( newCommits , false ) ;
160+ await UpdateSnapshots ( repo , updatedCommits ) ;
161161 await ValidateCommits ( repo ) ;
162162 await transaction . CommitAsync ( ) ;
163163 }
@@ -194,13 +194,15 @@ ValueTask<bool> ISyncable.ShouldSync()
194194 return ValueTask . FromResult ( true ) ;
195195 }
196196
197- private async Task UpdateSnapshots ( CrdtRepository repo , Commit oldestAddedCommit , Commit [ ] newCommits )
197+ private async Task UpdateSnapshots ( CrdtRepository repo , SortedSet < Commit > commitsToApply )
198198 {
199+ if ( commitsToApply . Count == 0 ) return ;
200+ var oldestAddedCommit = commitsToApply . First ( ) ;
199201 await repo . DeleteStaleSnapshots ( oldestAddedCommit ) ;
200202 Dictionary < Guid , Guid ? > snapshotLookup ;
201- if ( newCommits . Length > 10 )
203+ if ( commitsToApply . Count > 10 )
202204 {
203- var entityIds = newCommits . SelectMany ( c => c . ChangeEntities . Select ( ce => ce . EntityId ) ) ;
205+ var entityIds = commitsToApply . SelectMany ( c => c . ChangeEntities . Select ( ce => ce . EntityId ) ) ;
204206 snapshotLookup = await repo . CurrentSnapshots ( )
205207 . Where ( s => entityIds . Contains ( s . EntityId ) )
206208 . Select ( s => new KeyValuePair < Guid , Guid ? > ( s . EntityId , s . Id ) )
@@ -212,7 +214,7 @@ private async Task UpdateSnapshots(CrdtRepository repo, Commit oldestAddedCommit
212214 }
213215
214216 var snapshotWorker = new SnapshotWorker ( snapshotLookup , repo , _crdtConfig . Value ) ;
215- await snapshotWorker . UpdateSnapshots ( oldestAddedCommit , newCommits ) ;
217+ await snapshotWorker . UpdateSnapshots ( commitsToApply ) ;
216218 }
217219
218220 private async Task ValidateCommits ( CrdtRepository repo )
@@ -240,8 +242,8 @@ public async Task RegenerateSnapshots()
240242 await using var repo = await _crdtRepositoryFactory . CreateRepository ( ) ;
241243 await repo . DeleteSnapshotsAndProjectedTables ( ) ;
242244 repo . ClearChangeTracker ( ) ;
243- var allCommits = await repo . CurrentCommits ( ) . AsNoTracking ( ) . ToArrayAsync ( ) ;
244- await UpdateSnapshots ( repo , allCommits . First ( ) , allCommits ) ;
245+ var allCommits = await repo . CurrentCommits ( ) . AsNoTracking ( ) . ToSortedSetAsync ( ) ;
246+ await UpdateSnapshots ( repo , allCommits ) ;
245247 }
246248
247249 public async Task < ObjectSnapshot > GetLatestSnapshotByObjectId ( Guid entityId )
@@ -296,7 +298,7 @@ public async Task<Dictionary<Guid, ObjectSnapshot>> GetSnapshotsAtCommit(Commit
296298 var repository = repo . GetScopedRepository ( commit ) ;
297299 var ( snapshots , pendingCommits ) = await repository . GetCurrentSnapshotsAndPendingCommits ( ) ;
298300
299- if ( pendingCommits . Length != 0 )
301+ if ( pendingCommits . Count != 0 )
300302 {
301303 snapshots = await SnapshotWorker . ApplyCommitsToSnapshots ( snapshots ,
302304 repository ,
@@ -331,8 +333,8 @@ public async Task<T> GetAtCommit<T>(Commit commit, Guid entityId)
331333 var newCommits = await repository . CurrentCommits ( )
332334 . Include ( c => c . ChangeEntities )
333335 . WhereAfter ( snapshot . Commit )
334- . ToArrayAsync ( ) ;
335- if ( newCommits . Length > 0 )
336+ . ToSortedSetAsync ( ) ;
337+ if ( newCommits . Count > 0 )
336338 {
337339 var snapshots = await SnapshotWorker . ApplyCommitsToSnapshots (
338340 new Dictionary < Guid , ObjectSnapshot > ( [
0 commit comments