From d3466d7efe6a1eb9c8d3fc2a75befd3b56fde0f3 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 25 Feb 2025 05:30:46 +0530 Subject: [PATCH] [mob] Insert collection_files --- mobile/lib/db/remote/db.dart | 47 +++++++++++++++++++++++++++++ mobile/lib/db/remote/migration.dart | 2 ++ 2 files changed, 49 insertions(+) diff --git a/mobile/lib/db/remote/db.dart b/mobile/lib/db/remote/db.dart index eb42d917bf..314bb3b90b 100644 --- a/mobile/lib/db/remote/db.dart +++ b/mobile/lib/db/remote/db.dart @@ -6,6 +6,7 @@ import "package:flutter/foundation.dart"; import "package:path/path.dart"; import "package:path_provider/path_provider.dart"; import "package:photos/db/remote/migration.dart"; +import "package:photos/models/api/diff/diff.dart"; import "package:photos/models/collection/collection.dart"; import "package:sqlite_async/sqlite_async.dart"; @@ -57,6 +58,52 @@ class RemoteDB { ); } + Future insertCollectionFilesDiff( + List collections, + ) async { + if (collections.isEmpty) return; + final stopwatch = Stopwatch()..start(); + await Future.forEach(collections.slices(_batchInsertMaxCount), + (slice) async { + final List> values = slice + .map( + (e) => [ + e.collectionID, + e.fileID, + e.encFileKey, + e.encFileKeyNonce, + e.createdAt, + e.updatedAt, + e.isDeleted ? 1 : 0, + ], + ) + .toList(); + await _sqliteDB.executeBatch( + 'INSERT OR REPLACE INTO collection_files ($collectionFilesColumns) values(?, ?, ?, ?, ?, ?, ?)', + values, + ); + }); + debugPrint( + '$runtimeType insertCollectionFilesDiff complete in ${stopwatch.elapsed.inMilliseconds}ms for ${collections.length}', + ); + } + + Future deleteCollectionFilesDiff( + List items, + ) async { + if (items.isEmpty) return; + final int collectionID = items.first.collectionID; + final stopwatch = Stopwatch()..start(); + await Future.forEach(items.slices(_batchInsertMaxCount), (slice) async { + await _sqliteDB.execute( + 'DELETE FROM collection_files WHERE file_id IN (${slice.map((e) => e.fileID).join(',')}) AND collection_id = $collectionID', + ); + }); + debugPrint( + '$runtimeType deleteCollectionFilesDiff complete in ${stopwatch.elapsed.inMilliseconds}ms for ${items.length}', + ); + } + Future deleteEntries(Set ids, RemoteTable table) async { if (ids.isEmpty) return; final stopwatch = Stopwatch()..start(); diff --git a/mobile/lib/db/remote/migration.dart b/mobile/lib/db/remote/migration.dart index db4d95f2b6..2c11052a47 100644 --- a/mobile/lib/db/remote/migration.dart +++ b/mobile/lib/db/remote/migration.dart @@ -7,6 +7,8 @@ const collectionColumns = 'mmd_ver, pub_mmd_encoded_json, pub_mmd_ver, shared_mmd_json, ' 'shared_mmd_ver'; +const collectionFilesColumns = + 'collection_id, file_id, enc_key, enc_key_nonce, created_at, updated_at, is_deleted'; String collectionValuePlaceHolder = collectionColumns.split(',').map((_) => '?').join(',');