π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob] Simplify batch insert for files
Neeraj Gupta3f721abc4dd8
diff --git a/mobile/lib/db/files_db.dart b/mobile/lib/db/files_db.dartindex c734044c4c..8f1fe5a1e2 100644--- a/mobile/lib/db/files_db.dart+++ b/mobile/lib/db/files_db.dart@@ -15,7 +15,6 @@ import 'package:photos/models/location/location.dart';import "package:photos/models/metadata/common_keys.dart";import "package:photos/services/filter/db_filters.dart";import 'package:photos/utils/file_uploader_util.dart';-import "package:photos/utils/primitive_wrapper.dart";import "package:photos/utils/sqlite_util.dart";import 'package:sqlite_async/sqlite_async.dart';@@ -468,73 +467,62 @@ class FilesDB {final startTime = DateTime.now();final db = await sqliteAsyncDB;- ///Strong batch counter in an object so that it gets passed by reference- ///Primitives are passed by value- final genIdNotNullbatchCounter = PrimitiveWrapper(0);- final genIdNullbatchCounter = PrimitiveWrapper(0);- final genIdNullParameterSets = <List<Object?>>[];- final genIdNotNullParameterSets = <List<Object?>>[];-- final genIdNullcolumnNames =- _columnNames.where((element) => element != columnGeneratedID);-- for (EnteFile file in files) {- final fileGenIdIsNull = file.generatedID == null;-- if (!fileGenIdIsNull) {- await _batchAndInsertFile(- file,- conflictAlgorithm,- db,- genIdNotNullParameterSets,- genIdNotNullbatchCounter,- isGenIdNull: fileGenIdIsNull,- );+ final withIdParams = <List<Object?>>[];+ const withIdColumnNames = _columnNames;+ final withoutIdParams = <List<Object?>>[];+ final withoutIdColumns =+ _columnNames.where((column) => column != columnGeneratedID).toList();++ // Sort files into appropriate parameter sets+ for (final file in files) {+ if (file.generatedID == null) {+ withoutIdParams.add(_getParameterSetForFile(file));++ if (withoutIdParams.length == 400) {+ await _insertBatch(+ conflictAlgorithm,+ withoutIdColumns,+ db,+ withoutIdParams,+ );+ withoutIdParams.clear();+ }} else {- await _batchAndInsertFile(- file,- conflictAlgorithm,- db,- genIdNullParameterSets,- genIdNullbatchCounter,- isGenIdNull: fileGenIdIsNull,- );+ withIdParams.add(_getParameterSetForFile(file));+ if (withIdParams.length == 400) {+ await _insertBatch(+ conflictAlgorithm,+ withIdColumnNames,+ db,+ withIdParams,+ );+ withIdParams.clear();+ }}}- if (genIdNotNullbatchCounter.value > 0) {+ // Insert any remaining files+ if (withIdParams.isNotEmpty) {await _insertBatch(conflictAlgorithm,- _columnNames,+ withIdColumnNames,db,- genIdNotNullParameterSets,+ withIdParams,);- genIdNotNullbatchCounter.value = 0;- genIdNotNullParameterSets.clear();}- if (genIdNullbatchCounter.value > 0) {++ if (withoutIdParams.isNotEmpty) {await _insertBatch(conflictAlgorithm,- genIdNullcolumnNames,+ withoutIdColumns,db,- genIdNullParameterSets,+ withoutIdParams,);- genIdNullbatchCounter.value = 0;- genIdNullParameterSets.clear();}- final endTime = DateTime.now();- final duration = Duration(- microseconds:- endTime.microsecondsSinceEpoch - startTime.microsecondsSinceEpoch,- );+ final duration = DateTime.now().difference(startTime);_logger.info(- "Batch insert of " +- files.length.toString() +- " took " +- duration.inMilliseconds.toString() +- "ms.",- );+ "Batch insert of ${files.length} took ${duration.inMilliseconds}ms.");}Future<void> insert(EnteFile file) async {@@ -1943,28 +1931,6 @@ class FilesDB {return values;}- Future<void> _batchAndInsertFile(- EnteFile file,- SqliteAsyncConflictAlgorithm conflictAlgorithm,- SqliteDatabase db,- List<List<Object?>> parameterSets,- PrimitiveWrapper batchCounter, {- required bool isGenIdNull,- }) async {- parameterSets.add(_getParameterSetForFile(file));- batchCounter.value++;-- final columnNames = isGenIdNull- ? _columnNames.where((column) => column != columnGeneratedID)- : _columnNames;- if (batchCounter.value == 400) {- _logger.info("Inserting batch with genIdNull: $isGenIdNull");- await _insertBatch(conflictAlgorithm, columnNames, db, parameterSets);- batchCounter.value = 0;- parameterSets.clear();- }- }-Future<void> _insertBatch(SqliteAsyncConflictAlgorithm conflictAlgorithm,Iterable<String> columnNames,diff --git a/mobile/lib/utils/primitive_wrapper.dart b/mobile/lib/utils/primitive_wrapper.dartdeleted file mode 100644index 20ea9bbb6e..0000000000--- a/mobile/lib/utils/primitive_wrapper.dart+++ /dev/null@@ -1,6 +0,0 @@-///This is useful when you want to pass a primitive by reference.--class PrimitiveWrapper {- var value;- PrimitiveWrapper(this.value);-}