[mob][photos] Optimize getting uploaded file IDs associated with personID and clusterID when applying face filters

This commit is contained in:
ashilkn
2024-10-17 21:20:40 +05:30
parent 2f54acab58
commit e616071395
2 changed files with 35 additions and 29 deletions

View File

@@ -982,21 +982,34 @@ class MLDataDB {
}
}
Future<List<int>> getFileIDsFromFace(List<String> faceIDs) async {
final fileIDS = <int>[];
String inParam = "";
for (String faceID in faceIDs) {
inParam += "'$faceID', ";
}
inParam = inParam.substring(0, inParam.length - 2);
Future<List<int>> getFileIDsOfPersonID(String personID) async {
final db = await instance.asyncDB;
final result = await db.getAll(
'SELECT $fileIDColumn FROM $facesTable WHERE $faceIDColumn IN ($inParam)',
'''
SELECT DISTINCT $facesTable.$fileIDColumn
FROM $clusterPersonTable
JOIN $faceClustersTable ON $clusterPersonTable.$clusterIDColumn = $faceClustersTable.$clusterIDColumn
JOIN $facesTable ON $faceClustersTable.$faceIDColumn = $facesTable.$faceIDColumn
WHERE $clusterPersonTable.$personIdColumn = ?
''',
[personID],
);
for (var row in result) {
fileIDS.add(row[fileIDColumn] as int);
}
return fileIDS;
return [for (final row in result) row[fileIDColumn]];
}
Future<List<int>> getFileIDsOfClusterID(String clusterID) async {
final db = await instance.asyncDB;
final result = await db.getAll(
'''
SELECT DISTINCT $facesTable.$fileIDColumn
FROM $faceClustersTable
JOIN $facesTable ON $faceClustersTable.$faceIDColumn = $facesTable.$faceIDColumn
WHERE $faceClustersTable.$clusterIDColumn = ?
''',
[clusterID],
);
return [for (final row in result) row[fileIDColumn]];
}
}

View File

@@ -33,30 +33,23 @@ Future<List<EnteFile>> getFilteredFiles(
for (HierarchicalSearchFilter filter in filters) {
if (filter is FaceFilter && filter.getMatchedUploadedIDs().isEmpty) {
try {
final stopwatch = Stopwatch()..start();
if (filter.personId != null) {
//getFilesForPerson
final personClusterIDs = await MLDataDB.instance.getPersonClusterIDs(
final fileIDs = await MLDataDB.instance.getFileIDsOfPersonID(
filter.personId!,
);
final cluterToFaceIDs =
await MLDataDB.instance.getClusterToFaceIDs(personClusterIDs);
final faceIDs = <String>[];
for (Iterable<String> faceIDsIterable in cluterToFaceIDs.values) {
faceIDs.addAll(faceIDsIterable);
}
final fileIDs = await MLDataDB.instance.getFileIDsFromFace(faceIDs);
filter.matchedUploadedIDs.addAll(fileIDs);
} else if (filter.clusterId != null) {
//getFilesForCluster
final cluterToFaceIDs =
await MLDataDB.instance.getClusterToFaceIDs({filter.clusterId!});
final faceIDs = cluterToFaceIDs.values.expand((e) => e).toList();
final fileIDs = await MLDataDB.instance.getFileIDsFromFace(faceIDs);
final fileIDs = await MLDataDB.instance.getFileIDsOfClusterID(
filter.clusterId!,
);
filter.matchedUploadedIDs.addAll(fileIDs);
}
log(
"Time taken to get files for person/cluster ${filter.personId ?? filter.clusterId}: ${stopwatch.elapsedMilliseconds}ms",
);
stopwatch.stop();
} catch (e) {
log("Error in face filter: $e");
}