test: bench inserts

This commit is contained in:
shenlong-tanwen
2025-02-26 12:56:25 +05:30
parent 0e8b19e269
commit 65f5daa32c
11 changed files with 804 additions and 4 deletions
@@ -88,6 +88,16 @@ class AssetRepository with LogMixin implements IAssetRepository {
Future<void> deleteIds(Iterable<int> ids) async {
await _db.asset.deleteWhere((row) => row.id.isIn(ids));
}
@override
Future<bool> upsert(Asset asset) async {
final row = _toEntity(asset);
await _db.asset.insertOne(
row,
onConflict: DoUpdate((_) => row, target: [_db.asset.hash]),
);
return true;
}
}
AssetCompanion _toEntity(Asset asset) {
@@ -98,8 +108,8 @@ AssetCompanion _toEntity(Asset asset) {
height: Value(asset.height),
width: Value(asset.width),
type: asset.type,
createdTime: asset.createdTime,
modifiedTime: Value(asset.modifiedTime),
createdTime: asset.createdTime.toUtc(),
modifiedTime: Value(asset.modifiedTime.toUtc()),
duration: Value(asset.duration),
localId: Value(asset.localId),
remoteId: Value(asset.remoteId),
@@ -0,0 +1,136 @@
import 'package:immich_mobile/domain/entities/asset_isar.entity.dart' as entity;
import 'package:immich_mobile/domain/interfaces/asset.interface.dart';
import 'package:immich_mobile/domain/models/asset.model.dart';
import 'package:isar/isar.dart';
class AssetIsarRepository implements IAssetRepository {
final Isar db;
const AssetIsarRepository({required this.db});
@override
Future<bool> deleteAll() async {
await db.writeTxn(() async {
await db.assets.clear();
});
return true;
}
@override
Future<void> deleteIds(Iterable<int> ids) async {
await db.writeTxn(() async {
await db.assets.deleteAll(ids.toList());
});
}
@override
Future<List<Asset>> getAll({int? offset, int? limit}) async {
return await db.assets
.where()
.offset(offset ?? 0)
.limit(limit ?? 100)
.findAll()
.then((value) => value.map(_toModel).toList());
}
@override
Future<List<Asset>> getForHashes(Iterable<String> hashes) async {
return await db.assets
.where()
.filter()
.anyOf(hashes, (asset, hash) => asset.hashEqualTo(hash))
.findAll()
.then((value) => value.map(_toModel).toList());
}
@override
Future<List<Asset>> getForLocalIds(Iterable<String> localIds) async {
return await db.assets
.where()
.filter()
.anyOf(localIds, (asset, localId) => asset.localIdEqualTo(localId))
.findAll()
.then((value) => value.map(_toModel).toList());
}
@override
Future<List<Asset>> getForRemoteIds(Iterable<String> remoteIds) async {
return await db.assets
.where()
.filter()
.anyOf(remoteIds, (asset, remoteId) => asset.remoteIdEqualTo(remoteId))
.findAll()
.then((value) => value.map(_toModel).toList());
}
@override
Future<bool> upsertAll(Iterable<Asset> assets) async {
await db.writeTxn(() async {
await db.assets.putAll(assets.toEntity());
});
return true;
}
@override
Future<bool> upsert(Asset assets) async {
await db.writeTxn(() async {
await db.assets.put(assets.toEntity());
});
return true;
}
}
Asset _toModel(entity.Asset entity) {
return Asset(
id: entity.id,
name: entity.name,
hash: entity.hash,
height: entity.height,
width: entity.width,
type: entity.type,
createdTime: entity.createdTime,
modifiedTime: entity.modifiedTime,
duration: entity.duration,
localId: entity.localId,
remoteId: entity.remoteId,
livePhotoVideoId: entity.livePhotoVideoId,
);
}
extension on Asset {
entity.Asset toEntity() {
return entity.Asset(
id: id,
name: name,
hash: hash,
height: height,
width: width,
type: type,
createdTime: createdTime,
modifiedTime: modifiedTime,
duration: duration,
localId: localId,
remoteId: remoteId,
livePhotoVideoId: livePhotoVideoId,
);
}
}
extension on Iterable<Asset> {
List<entity.Asset> toEntity() {
return map((asset) => entity.Asset(
id: asset.id,
name: asset.name,
hash: asset.hash,
height: asset.height,
width: asset.width,
type: asset.type,
createdTime: asset.createdTime,
modifiedTime: asset.modifiedTime,
duration: asset.duration,
localId: asset.localId,
remoteId: asset.remoteId,
livePhotoVideoId: asset.livePhotoVideoId,
)).toList();
}
}
@@ -57,4 +57,44 @@ class RenderListRepository with LogMixin implements IRenderListRepository {
return RenderList(elements: elements, modifiedTime: modified);
});
}
@override
Future<RenderList> getAll() async {
final assetCountExp = _db.asset.id.count();
final createdTimeExp = _db.asset.createdTime;
final modifiedTimeExp = _db.asset.modifiedTime.max();
final monthYearExp = createdTimeExp.strftime('%m-%Y');
final query = _db.asset.selectOnly()
..addColumns([assetCountExp, createdTimeExp, modifiedTimeExp])
..groupBy([monthYearExp])
..orderBy([OrderingTerm.desc(createdTimeExp)]);
int lastAssetOffset = 0;
DateTime recentModifiedTime = DateTime(1);
final elements = await query.expand((row) {
final createdTime = row.read<DateTime>(createdTimeExp)!;
final assetCount = row.read(assetCountExp)!;
final modifiedTime = row.read(modifiedTimeExp)!;
final assetOffset = lastAssetOffset;
lastAssetOffset += assetCount;
// Get the recent modifed time. This is used to prevent unnecessary grid updates
if (modifiedTime.isAfter(recentModifiedTime)) {
recentModifiedTime = modifiedTime;
}
return [
RenderListMonthHeaderElement(date: createdTime),
RenderListAssetElement(
date: createdTime,
assetCount: assetCount,
assetOffset: assetOffset,
),
];
}).get();
return RenderList(elements: elements, modifiedTime: recentModifiedTime);
}
}