1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-09-16 09:16:27 +02:00

Fix PersonManager's person loading logic #1015

This commit is contained in:
Patrik J. Braun
2025-09-09 22:29:21 +02:00
parent ee745678a3
commit 8337eeafa4
3 changed files with 30 additions and 12 deletions

View File

@@ -153,7 +153,7 @@ export class BenchmarkRunner {
await this.setupDB(); await this.setupDB();
const queryParser = new SearchQueryParser(defaultQueryKeywords); const queryParser = new SearchQueryParser(defaultQueryKeywords);
const names = (await ObjectManagers.getInstance().PersonManager.getAll(this.session)).sort((a, b) => b.count - a.count); const names = (await ObjectManagers.getInstance().PersonManager.getAll(this.session)).sort((a, b) => b.cache.count - a.cache.count);
const queries: { query: SearchQueryDTO, description: string }[] = TextSearchQueryTypes.map(t => { const queries: { query: SearchQueryDTO, description: string }[] = TextSearchQueryTypes.map(t => {
const q = { const q = {
type: t, text: 'a' type: t, text: 'a'

View File

@@ -307,7 +307,7 @@ export class PersonManager implements IObjectManager {
const connection = await SQLConnection.getConnection(); const connection = await SQLConnection.getConnection();
const personRepository = connection.getRepository(PersonEntry); const personRepository = connection.getRepository(PersonEntry);
this.personsCache = this.personsCache || {}; this.personsCache = this.personsCache || {};
this.personsCache[session.user.projectionKey] = await personRepository const persons = await personRepository
.createQueryBuilder('person') .createQueryBuilder('person')
.leftJoin('person.cache', 'cache', 'cache.projectionKey = :pk AND cache.valid = 1', {pk: session.user.projectionKey}) .leftJoin('person.cache', 'cache', 'cache.projectionKey = :pk AND cache.valid = 1', {pk: session.user.projectionKey})
.leftJoin('cache.sampleRegion', 'sampleRegion') .leftJoin('cache.sampleRegion', 'sampleRegion')
@@ -324,6 +324,14 @@ export class PersonManager implements IObjectManager {
'directory.name' 'directory.name'
]) ])
.getMany(); .getMany();
// Fix cache property: convert from array to single object
this.personsCache[session.user.projectionKey] = persons.map(person => {
if (person.cache && Array.isArray(person.cache) && person.cache.length > 0) {
person.cache = person.cache[0];
}
return person;
});
} }
} }

View File

@@ -98,6 +98,7 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
const selected = Utils.clone(await pm.get(DBTestHelper.defaultSession, 'Boba Fett')); const selected = Utils.clone(await pm.get(DBTestHelper.defaultSession, 'Boba Fett'));
expect(selected).to.not.be.undefined; expect(selected).to.not.be.undefined;
expect(selected.cache).to.be.not.undefined; expect(selected.cache).to.be.not.undefined;
expect(selected.cache.count).to.be.greaterThan(0);
delete selected.cache; delete selected.cache;
delete person.cache; delete person.cache;
expect(selected).to.deep.equal(person); expect(selected).to.deep.equal(person);
@@ -115,6 +116,13 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
expect(person).to.have.property('id'); expect(person).to.have.property('id');
expect(person).to.have.property('name'); expect(person).to.have.property('name');
expect(person).to.have.property('isFavourite'); expect(person).to.have.property('isFavourite');
// Check that cache is a single object, not an array
if (person.cache) {
expect(Array.isArray(person.cache)).to.be.false;
expect(person.cache).to.have.property('count');
expect(person.cache.count).to.be.a('number');
}
}); });
}); });
@@ -130,7 +138,7 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
const count = await pm.countFaces(); const count = await pm.countFaces();
expect(count).to.be.a('number'); expect(count).to.be.a('number');
expect(count).to.be.greaterThan(0); expect(count).to.be.equal(9);
}); });
it('should update person', async () => { it('should update person', async () => {
@@ -232,7 +240,9 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
const personsWithDefault = await pm.getAll(DBTestHelper.defaultSession); const personsWithDefault = await pm.getAll(DBTestHelper.defaultSession);
expect(personsWithProjection).to.not.be.undefined; expect(personsWithProjection).to.not.be.undefined;
expect(personsWithProjection.length).to.equal(10);
expect(personsWithDefault).to.not.be.undefined; expect(personsWithDefault).to.not.be.undefined;
expect(personsWithDefault.length).to.equal(10);
// With projection, some persons might have different counts or be filtered out // With projection, some persons might have different counts or be filtered out
// The exact behavior depends on which photos match the projection // The exact behavior depends on which photos match the projection
@@ -245,7 +255,7 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
// Get persons with default session first // Get persons with default session first
const personsDefault = await pm.getAll(DBTestHelper.defaultSession); const personsDefault = await pm.getAll(DBTestHelper.defaultSession);
expect(personsDefault.length).to.be.greaterThan(0); expect(personsDefault.length).to.be.equal(10);
// Create projection session // Create projection session
const projectionSession = await createProjectionSession({ const projectionSession = await createProjectionSession({
@@ -254,8 +264,8 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
matchType: TextSearchQueryMatchTypes.like matchType: TextSearchQueryMatchTypes.like
}); });
// Get persons with projection session // Trigger cache filling
const personsProjection = await pm.getAll(projectionSession); await pm.getAll(projectionSession);
// Verify that cache entries exist for both projection keys // Verify that cache entries exist for both projection keys
const defaultCacheEntries = await connection.getRepository(ProjectedPersonCacheEntity) const defaultCacheEntries = await connection.getRepository(ProjectedPersonCacheEntity)
@@ -263,8 +273,8 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
const projectionCacheEntries = await connection.getRepository(ProjectedPersonCacheEntity) const projectionCacheEntries = await connection.getRepository(ProjectedPersonCacheEntity)
.count({where: {projectionKey: projectionSession.user.projectionKey}}); .count({where: {projectionKey: projectionSession.user.projectionKey}});
expect(defaultCacheEntries).to.be.greaterThan(0); expect(defaultCacheEntries).to.be.equal(10);
expect(projectionCacheEntries).to.be.greaterThan(0); expect(projectionCacheEntries).to.be.equal(10);
}); });
it('should get single person with projection session', async () => { it('should get single person with projection session', async () => {
@@ -298,7 +308,7 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
let validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity) let validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity)
.count({where: {valid: true}}); .count({where: {valid: true}});
expect(validCacheCount).to.be.greaterThan(0); expect(validCacheCount).to.be.equal(20);
// Reset previews // Reset previews
await pm.resetPreviews(); await pm.resetPreviews();
@@ -318,7 +328,7 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
let validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity) let validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity)
.count({where: {valid: true}}); .count({where: {valid: true}});
expect(validCacheCount).to.be.greaterThan(0); expect(validCacheCount).to.be.equal(10);
// Trigger cache invalidation for the directory // Trigger cache invalidation for the directory
await pm.onNewDataVersion(dir); await pm.onNewDataVersion(dir);
@@ -353,12 +363,12 @@ describe('PersonManager', (sqlHelper: DBTestHelper) => {
// Access persons - this should rebuild cache // Access persons - this should rebuild cache
const persons = await pm.getAll(DBTestHelper.defaultSession); const persons = await pm.getAll(DBTestHelper.defaultSession);
expect(persons).to.not.be.undefined; expect(persons).to.not.be.undefined;
expect(persons.length).to.be.greaterThan(0); expect(persons.length).to.be.equal(10);
// Verify cache is now valid // Verify cache is now valid
validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity) validCacheCount = await connection.getRepository(ProjectedPersonCacheEntity)
.count({where: {valid: true}}); .count({where: {valid: true}});
expect(validCacheCount).to.be.greaterThan(0); expect(validCacheCount).to.be.equal(10);
}); });
}); });