Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mobile): assert lists are sorted for diffing #13180

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions mobile/lib/services/sync.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ class SyncService {
Future<bool> _syncUsersFromServer(List<User> users) async {
users.sortBy((u) => u.id);
final dbUsers = await _userRepository.getAll(sortBy: UserSort.id);
assert(dbUsers.isSortedBy((u) => u.id), "dbUsers not sorted!");
final List<int> toDelete = [];
final List<User> toUpsert = [];
final changes = diffSortedListsSync(
Expand Down Expand Up @@ -322,8 +321,6 @@ class SyncService {
ownerId: isShared ? null : me.isarId,
sortBy: AlbumSort.remoteId,
);
assert(dbAlbums.isSortedBy((e) => e.remoteId!), "dbAlbums not sorted!");

final List<Asset> toDelete = [];
final List<Asset> existing = [];

Expand Down Expand Up @@ -512,7 +509,6 @@ class SyncService {
await _albumRepository.getAll(remote: false, sortBy: AlbumSort.localId);
final List<Asset> deleteCandidates = [];
final List<Asset> existing = [];
assert(inDb.isSorted((a, b) => a.localId!.compareTo(b.localId!)), "sort!");
final bool anyChanges = await diffSortedLists(
onDevice,
inDb,
Expand Down
34 changes: 20 additions & 14 deletions mobile/lib/utils/diff.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import 'dart:async';

import 'package:collection/collection.dart';

/// Efficiently compares two sorted lists in O(n), calling the given callback
/// for each item.
/// Return `true` if there are any differences found, else `false`
Future<bool> diffSortedLists<A, B>(
List<A> la,
List<B> lb, {
required int Function(A a, B b) compare,
required FutureOr<bool> Function(A a, B b) both,
required FutureOr<void> Function(A a) onlyFirst,
required FutureOr<void> Function(B b) onlySecond,
Future<bool> diffSortedLists<T>(
List<T> la,
List<T> lb, {
required int Function(T a, T b) compare,
required FutureOr<bool> Function(T a, T b) both,
required FutureOr<void> Function(T a) onlyFirst,
required FutureOr<void> Function(T b) onlySecond,
}) async {
assert(la.isSorted(compare), "first argument must be sorted");
assert(lb.isSorted(compare), "second argument must be sorted");
bool diff = false;
int i = 0, j = 0;
for (; i < la.length && j < lb.length;) {
Expand Down Expand Up @@ -38,14 +42,16 @@ Future<bool> diffSortedLists<A, B>(
/// Efficiently compares two sorted lists in O(n), calling the given callback
/// for each item.
/// Return `true` if there are any differences found, else `false`
bool diffSortedListsSync<A, B>(
List<A> la,
List<B> lb, {
required int Function(A a, B b) compare,
required bool Function(A a, B b) both,
required void Function(A a) onlyFirst,
required void Function(B b) onlySecond,
bool diffSortedListsSync<T>(
List<T> la,
List<T> lb, {
required int Function(T a, T b) compare,
required bool Function(T a, T b) both,
required void Function(T a) onlyFirst,
required void Function(T b) onlySecond,
}) {
assert(la.isSorted(compare), "first argument must be sorted");
assert(lb.isSorted(compare), "second argument must be sorted");
bool diff = false;
int i = 0, j = 0;
for (; i < la.length && j < lb.length;) {
Expand Down
Loading