test: bench inserts
This commit is contained in:
163
mobile-v2/lib/domain/entities/asset_isar.entity.dart
Normal file
163
mobile-v2/lib/domain/entities/asset_isar.entity.dart
Normal file
@@ -0,0 +1,163 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:immich_mobile/domain/models/asset.model.dart';
|
||||
import 'package:immich_mobile/utils/collection_util.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
part 'asset_isar.entity.g.dart';
|
||||
|
||||
@Collection()
|
||||
class Asset {
|
||||
Id get isarId => id ?? Isar.autoIncrement;
|
||||
final int? id;
|
||||
final String name;
|
||||
final String hash;
|
||||
final int? height;
|
||||
final int? width;
|
||||
@Enumerated(EnumType.ordinal)
|
||||
final AssetType type;
|
||||
final DateTime createdTime;
|
||||
final DateTime modifiedTime;
|
||||
final int duration;
|
||||
|
||||
// local only
|
||||
final String? localId;
|
||||
|
||||
// remote only
|
||||
final String? remoteId;
|
||||
final String? livePhotoVideoId;
|
||||
|
||||
bool get isRemote => remoteId != null;
|
||||
bool get isLocal => localId != null;
|
||||
bool get isMerged => isRemote && isLocal;
|
||||
bool get isImage => type == AssetType.image;
|
||||
|
||||
const Asset({
|
||||
this.id,
|
||||
required this.name,
|
||||
required this.hash,
|
||||
this.height,
|
||||
this.width,
|
||||
required this.type,
|
||||
required this.createdTime,
|
||||
required this.modifiedTime,
|
||||
required this.duration,
|
||||
this.localId,
|
||||
this.remoteId,
|
||||
this.livePhotoVideoId,
|
||||
});
|
||||
|
||||
Asset copyWith({
|
||||
int? id,
|
||||
String? name,
|
||||
String? hash,
|
||||
int? height,
|
||||
int? width,
|
||||
AssetType? type,
|
||||
DateTime? createdTime,
|
||||
DateTime? modifiedTime,
|
||||
int? duration,
|
||||
ValueGetter<String?>? localId,
|
||||
ValueGetter<String?>? remoteId,
|
||||
String? livePhotoVideoId,
|
||||
}) {
|
||||
return Asset(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
hash: hash ?? this.hash,
|
||||
height: height ?? this.height,
|
||||
width: width ?? this.width,
|
||||
type: type ?? this.type,
|
||||
createdTime: createdTime ?? this.createdTime,
|
||||
modifiedTime: modifiedTime ?? this.modifiedTime,
|
||||
duration: duration ?? this.duration,
|
||||
localId: localId == null ? this.localId : localId(),
|
||||
remoteId: remoteId == null ? this.remoteId : remoteId(),
|
||||
livePhotoVideoId: livePhotoVideoId ?? this.livePhotoVideoId,
|
||||
);
|
||||
}
|
||||
|
||||
Asset merge(Asset newAsset) {
|
||||
final existingAsset = this;
|
||||
assert(existingAsset.id != null, "Existing asset must be from the db");
|
||||
|
||||
final oldestCreationTime =
|
||||
existingAsset.createdTime.isBefore(newAsset.createdTime)
|
||||
? existingAsset.createdTime
|
||||
: newAsset.createdTime;
|
||||
|
||||
if (newAsset.modifiedTime.isAfter(existingAsset.modifiedTime)) {
|
||||
return newAsset.copyWith(
|
||||
id: newAsset.id ?? existingAsset.id,
|
||||
height: newAsset.height ?? existingAsset.height,
|
||||
width: newAsset.width ?? existingAsset.width,
|
||||
createdTime: oldestCreationTime,
|
||||
localId: () => existingAsset.localId ?? newAsset.localId,
|
||||
remoteId: () => existingAsset.remoteId ?? newAsset.remoteId,
|
||||
);
|
||||
}
|
||||
|
||||
return existingAsset.copyWith(
|
||||
height: existingAsset.height ?? newAsset.height,
|
||||
width: existingAsset.width ?? newAsset.width,
|
||||
createdTime: oldestCreationTime,
|
||||
localId: () => existingAsset.localId ?? newAsset.localId,
|
||||
remoteId: () => existingAsset.remoteId ?? newAsset.remoteId,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => """
|
||||
{
|
||||
"id": "${id ?? "-"}",
|
||||
"remoteId": "${remoteId ?? "-"}",
|
||||
"localId": "${localId ?? "-"}",
|
||||
"name": "$name",
|
||||
"hash": "$hash",
|
||||
"height": ${height ?? "-"},
|
||||
"width": ${width ?? "-"},
|
||||
"type": "$type",
|
||||
"createdTime": "$createdTime",
|
||||
"modifiedTime": "$modifiedTime",
|
||||
"duration": "$duration",
|
||||
"livePhotoVideoId": "${livePhotoVideoId ?? "-"}",
|
||||
}""";
|
||||
|
||||
@override
|
||||
bool operator ==(covariant Asset other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other.id == id &&
|
||||
other.name == name &&
|
||||
other.hash == hash &&
|
||||
other.height == height &&
|
||||
other.width == width &&
|
||||
other.type == type &&
|
||||
other.createdTime == createdTime &&
|
||||
other.modifiedTime == modifiedTime &&
|
||||
other.duration == duration &&
|
||||
other.localId == localId &&
|
||||
other.remoteId == remoteId &&
|
||||
other.livePhotoVideoId == livePhotoVideoId;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return id.hashCode ^
|
||||
name.hashCode ^
|
||||
hash.hashCode ^
|
||||
height.hashCode ^
|
||||
width.hashCode ^
|
||||
type.hashCode ^
|
||||
createdTime.hashCode ^
|
||||
modifiedTime.hashCode ^
|
||||
duration.hashCode ^
|
||||
localId.hashCode ^
|
||||
remoteId.hashCode ^
|
||||
livePhotoVideoId.hashCode;
|
||||
}
|
||||
|
||||
static int compareByHash(Asset a, Asset b) => a.hash.compareTo(b.hash);
|
||||
|
||||
static int compareByLocalId(Asset a, Asset b) =>
|
||||
CollectionUtil.compareToNullable(a.localId, b.localId);
|
||||
}
|
||||
@@ -3,6 +3,9 @@ import 'dart:async';
|
||||
import 'package:immich_mobile/domain/models/asset.model.dart';
|
||||
|
||||
abstract interface class IAssetRepository {
|
||||
/// Batch upsert asset
|
||||
Future<bool> upsert(Asset assets);
|
||||
|
||||
/// Batch upsert asset
|
||||
Future<bool> upsertAll(Iterable<Asset> assets);
|
||||
|
||||
|
||||
@@ -3,4 +3,6 @@ import 'package:immich_mobile/domain/models/render_list.model.dart';
|
||||
abstract interface class IRenderListRepository {
|
||||
/// Streams the [RenderList] for the main timeline
|
||||
Stream<RenderList> watchAll();
|
||||
|
||||
Future<RenderList> getAll();
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
136
mobile-v2/lib/domain/repositories/asset_isar.repository.dart
Normal file
136
mobile-v2/lib/domain/repositories/asset_isar.repository.dart
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user