Skip to content

Commit

Permalink
refactor(server): filter assets by people using a subquery instead of…
Browse files Browse the repository at this point in the history
… a cte (#15768)
  • Loading branch information
dav-wolff authored Jan 31, 2025
1 parent 098bab7 commit 1b141d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
31 changes: 15 additions & 16 deletions server/src/entities/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,20 @@ export function withFacesAndPeople(eb: ExpressionBuilder<DB, 'assets'>) {
.as('faces');
}

/** Adds a `has_people` CTE that can be inner joined on to filter out assets */
export function hasPeopleCte(db: Kysely<DB>, personIds: string[]) {
return db.with('has_people', (qb) =>
qb
.selectFrom('asset_faces')
.select('assetId')
.where('personId', '=', anyUuid(personIds!))
.groupBy('assetId')
.having((eb) => eb.fn.count('personId').distinct(), '=', personIds.length),
export function hasPeople<O>(qb: SelectQueryBuilder<DB, 'assets', O>, personIds: string[]) {
return qb.innerJoin(
(eb) =>
eb
.selectFrom('asset_faces')
.select('assetId')
.where('personId', '=', anyUuid(personIds!))
.groupBy('assetId')
.having((eb) => eb.fn.count('personId').distinct(), '=', personIds.length)
.as('has_people'),
(join) => join.onRef('has_people.assetId', '=', 'assets.id'),
);
}

export function hasPeople(db: Kysely<DB>, personIds?: string[]) {
return personIds && personIds.length > 0
? hasPeopleCte(db, personIds).selectFrom('assets').innerJoin('has_people', 'has_people.assetId', 'assets.id')
: db.selectFrom('assets');
}

export function withOwner(eb: ExpressionBuilder<DB, 'assets'>) {
return jsonObjectFrom(eb.selectFrom('users').selectAll().whereRef('users.id', '=', 'assets.ownerId')).as('owner');
}
Expand Down Expand Up @@ -326,8 +322,11 @@ const joinDeduplicationPlugin = new DeduplicateJoinsPlugin();
export function searchAssetBuilder(kysely: Kysely<DB>, options: AssetSearchBuilderOptions) {
options.isArchived ??= options.withArchived ? undefined : false;
options.withDeleted ||= !!(options.trashedAfter || options.trashedBefore);
return hasPeople(kysely.withPlugin(joinDeduplicationPlugin), options.personIds)
return kysely
.withPlugin(joinDeduplicationPlugin)
.selectFrom('assets')
.selectAll('assets')
.$if(!!options.personIds && options.personIds.length > 0, (qb) => hasPeople(qb, options.personIds!))
.$if(!!options.createdBefore, (qb) => qb.where('assets.createdAt', '<=', options.createdBefore!))
.$if(!!options.createdAfter, (qb) => qb.where('assets.createdAt', '>=', options.createdAfter!))
.$if(!!options.updatedBefore, (qb) => qb.where('assets.updatedAt', '<=', options.updatedBefore!))
Expand Down
13 changes: 5 additions & 8 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Chunked, ChunkedArray, DummyValue, GenerateSql } from 'src/decorators';
import {
AssetEntity,
hasPeople,
hasPeopleCte,
searchAssetBuilder,
truncatedDate,
withAlbums,
Expand Down Expand Up @@ -576,7 +575,7 @@ export class AssetRepository implements IAssetRepository {
@GenerateSql({ params: [{ size: TimeBucketSize.MONTH }] })
async getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]> {
return (
((options.personId ? hasPeopleCte(this.db, [options.personId]) : this.db) as Kysely<DB>)
this.db
.with('assets', (qb) =>
qb
.selectFrom('assets')
Expand All @@ -589,11 +588,7 @@ export class AssetRepository implements IAssetRepository {
.innerJoin('albums_assets_assets', 'assets.id', 'albums_assets_assets.assetsId')
.where('albums_assets_assets.albumsId', '=', asUuid(options.albumId!)),
)
.$if(!!options.personId, (qb) =>
qb.innerJoin(sql.table('has_people').as('has_people'), (join) =>
join.onRef(sql`has_people."assetId"`, '=', 'assets.id'),
),
)
.$if(!!options.personId, (qb) => hasPeople(qb, [options.personId!]))
.$if(!!options.withStacked, (qb) =>
qb
.leftJoin('asset_stack', (join) =>
Expand Down Expand Up @@ -628,10 +623,12 @@ export class AssetRepository implements IAssetRepository {

@GenerateSql({ params: [DummyValue.TIME_BUCKET, { size: TimeBucketSize.MONTH, withStacked: true }] })
async getTimeBucket(timeBucket: string, options: TimeBucketOptions): Promise<AssetEntity[]> {
return hasPeople(this.db, options.personId ? [options.personId] : undefined)
return this.db
.selectFrom('assets')
.selectAll('assets')
.$call(withExif)
.$if(!!options.albumId, (qb) => withAlbums(qb, { albumId: options.albumId }))
.$if(!!options.personId, (qb) => hasPeople(qb, [options.personId!]))
.$if(!!options.userIds, (qb) => qb.where('assets.ownerId', '=', anyUuid(options.userIds!)))
.$if(options.isArchived !== undefined, (qb) => qb.where('assets.isArchived', '=', options.isArchived!))
.$if(options.isFavorite !== undefined, (qb) => qb.where('assets.isFavorite', '=', options.isFavorite!))
Expand Down

0 comments on commit 1b141d5

Please sign in to comment.