mirror of
https://github.com/immich-app/immich
synced 2025-10-17 18:19:27 +00:00
* download only backup selected assets * android impl * fix tests * limit concurrent hashing to 16 * extension cleanup * optimized hashing * hash only selected albums * remove concurrency limit * address review comments * log more info on failure * add native cancellation * small batch size on ios, large on android * fix: get correct resources * cleanup getResource * ios better hash cancellation * handle graceful cancellation android * do not trigger multiple hashing ops * ios: fix circular reference, improve cancellation * kotlin: more cancellation checks * no need to create result * cancel previous task * avoid race condition * ensure cancellation gets called * fix cancellation not happening --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> Co-authored-by: Alex <alex.tran1502@gmail.com>
99 lines
3.4 KiB
Dart
99 lines
3.4 KiB
Dart
import 'package:collection/collection.dart';
|
|
import 'package:drift/drift.dart';
|
|
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
|
import 'package:immich_mobile/infrastructure/entities/local_album.entity.dart';
|
|
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.dart';
|
|
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.drift.dart';
|
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
|
|
|
class DriftLocalAssetRepository extends DriftDatabaseRepository {
|
|
final Drift _db;
|
|
const DriftLocalAssetRepository(this._db) : super(_db);
|
|
|
|
SingleOrNullSelectable<LocalAsset?> _assetSelectable(String id) {
|
|
final query = _db.localAssetEntity.select().addColumns([_db.remoteAssetEntity.id]).join([
|
|
leftOuterJoin(
|
|
_db.remoteAssetEntity,
|
|
_db.localAssetEntity.checksum.equalsExp(_db.remoteAssetEntity.checksum),
|
|
useColumns: false,
|
|
),
|
|
])..where(_db.localAssetEntity.id.equals(id));
|
|
|
|
return query.map((row) {
|
|
final asset = row.readTable(_db.localAssetEntity).toDto();
|
|
return asset.copyWith(remoteId: row.read(_db.remoteAssetEntity.id));
|
|
});
|
|
}
|
|
|
|
Future<LocalAsset?> get(String id) => _assetSelectable(id).getSingleOrNull();
|
|
|
|
Future<List<LocalAsset?>> getByChecksum(String checksum) {
|
|
final query = _db.localAssetEntity.select()..where((lae) => lae.checksum.equals(checksum));
|
|
|
|
return query.map((row) => row.toDto()).get();
|
|
}
|
|
|
|
Stream<LocalAsset?> watch(String id) => _assetSelectable(id).watchSingleOrNull();
|
|
|
|
Future<void> updateHashes(Map<String, String> hashes) {
|
|
if (hashes.isEmpty) {
|
|
return Future.value();
|
|
}
|
|
|
|
return _db.batch((batch) async {
|
|
for (final entry in hashes.entries) {
|
|
batch.update(
|
|
_db.localAssetEntity,
|
|
LocalAssetEntityCompanion(checksum: Value(entry.value)),
|
|
where: (e) => e.id.equals(entry.key),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> delete(List<String> ids) {
|
|
if (ids.isEmpty) {
|
|
return Future.value();
|
|
}
|
|
|
|
return _db.batch((batch) {
|
|
for (final slice in ids.slices(32000)) {
|
|
batch.deleteWhere(_db.localAssetEntity, (e) => e.id.isIn(slice));
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<LocalAsset?> getById(String id) {
|
|
final query = _db.localAssetEntity.select()..where((lae) => lae.id.equals(id));
|
|
|
|
return query.map((row) => row.toDto()).getSingleOrNull();
|
|
}
|
|
|
|
Future<int> getCount() {
|
|
return _db.managers.localAssetEntity.count();
|
|
}
|
|
|
|
Future<int> getHashedCount() {
|
|
return _db.managers.localAssetEntity.filter((e) => e.checksum.isNull().not()).count();
|
|
}
|
|
|
|
Future<List<LocalAlbum>> getSourceAlbums(String localAssetId, {BackupSelection? backupSelection}) {
|
|
final query = _db.localAlbumEntity.select()
|
|
..where(
|
|
(lae) => existsQuery(
|
|
_db.localAlbumAssetEntity.selectOnly()
|
|
..addColumns([_db.localAlbumAssetEntity.albumId])
|
|
..where(
|
|
_db.localAlbumAssetEntity.albumId.equalsExp(lae.id) &
|
|
_db.localAlbumAssetEntity.assetId.equals(localAssetId),
|
|
),
|
|
),
|
|
)
|
|
..orderBy([(lae) => OrderingTerm.asc(lae.name)]);
|
|
if (backupSelection != null) {
|
|
query.where((lae) => lae.backupSelection.equalsValue(backupSelection));
|
|
}
|
|
return query.map((localAlbum) => localAlbum.toDto()).get();
|
|
}
|
|
}
|