From d151c56e774f367bb4c095db8a00feeae0b02608 Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Wed, 28 Dec 2016 15:35:27 +0100 Subject: [PATCH] implementing on the fly indexing --- backend/middlewares/GalleryMWs.ts | 1 + backend/model/mysql/GalleryManager.ts | 46 ++++++++++++++++--- .../model/mysql/enitites/DirectoryEntity.ts | 3 ++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/backend/middlewares/GalleryMWs.ts b/backend/middlewares/GalleryMWs.ts index 6c309762..40f74255 100644 --- a/backend/middlewares/GalleryMWs.ts +++ b/backend/middlewares/GalleryMWs.ts @@ -24,6 +24,7 @@ export class GalleryMWs { ObjectManagerRepository.getInstance().getGalleryManager().listDirectory(directoryName, (err, directory: DirectoryDTO) => { if (err || !directory) { + console.error(err); return next(new Error(ErrorCodes.GENERAL_ERROR, err)); } diff --git a/backend/model/mysql/GalleryManager.ts b/backend/model/mysql/GalleryManager.ts index 921fd62f..86d41092 100644 --- a/backend/model/mysql/GalleryManager.ts +++ b/backend/model/mysql/GalleryManager.ts @@ -30,8 +30,7 @@ export class GalleryManager implements IGalleryManager { .leftJoinAndSelect("directory.photos", "photos") .getOne(); - - if (dir) { + if (dir && dir.scanned == true) { if (dir.photos) { for (let i = 0; i < dir.photos.length; i++) { dir.photos[i].directory = dir; @@ -41,7 +40,11 @@ export class GalleryManager implements IGalleryManager { dir.photos[i].metadata.size = JSON.parse(dir.photos[i].metadata.size); } } - return cb(null, dir); + + cb(null, dir); //WARNING: only on the fly indexing should happen after this point + + //on the fly updating + this.indexDirectory(relativeDirectoryName, cb); } return this.indexDirectory(relativeDirectoryName, cb); @@ -60,15 +63,44 @@ export class GalleryManager implements IGalleryManager { let directoryRepository = connection.getRepository(DirectoryEntity); let photosRepository = connection.getRepository(PhotoEntity); - let parentDir = await directoryRepository.persist(scannedDirectory); + + let parentDir = await directoryRepository.createQueryBuilder("directory") + .where("directory.name = :name AND directory.path = :path", { + name: scannedDirectory.name, + path: scannedDirectory.path + }).getOne(); + + if (!!parentDir) { + parentDir.scanned = true; + parentDir.lastUpdate = Date.now(); + parentDir = await directoryRepository.persist(parentDir); + } else { + (scannedDirectory).scanned = true; + parentDir = await directoryRepository.persist(scannedDirectory); + } for (let i = 0; i < scannedDirectory.directories.length; i++) { + if ((await directoryRepository.createQueryBuilder("directory") + .where("directory.name = :name AND directory.path = :path", { + name: scannedDirectory.directories[i].name, + path: scannedDirectory.directories[i].path + }).getCount()) > 0) { + continue; + } scannedDirectory.directories[i].parent = parentDir; - await directoryRepository.persist(scannedDirectory.directories[i]); + (scannedDirectory.directories[i]).scanned = false; + await directoryRepository.persist(scannedDirectory.directories[i]); } for (let i = 0; i < scannedDirectory.photos.length; i++) { - + //TODO: load as batch + if ((await photosRepository.createQueryBuilder("photo") + .where("photo.name = :name AND photo.directory = :dir", { + name: scannedDirectory.photos[i].name, + dir: parentDir.id + }).getCount()) > 0) { + continue; + } //typeorm not supports recursive embended: TODO:fix it scannedDirectory.photos[i].directory = null; let photo = Utils.clone(scannedDirectory.photos[i]); @@ -81,7 +113,7 @@ export class GalleryManager implements IGalleryManager { await photosRepository.persist(photo); } - return cb(null, parentDir); + return cb(null, scannedDirectory); }).catch((error) => { diff --git a/backend/model/mysql/enitites/DirectoryEntity.ts b/backend/model/mysql/enitites/DirectoryEntity.ts index a8fffbd1..9682bfbd 100644 --- a/backend/model/mysql/enitites/DirectoryEntity.ts +++ b/backend/model/mysql/enitites/DirectoryEntity.ts @@ -22,6 +22,9 @@ export class DirectoryEntity implements DirectoryDTO { @Column('number') public lastUpdate: number; + @Column({type: 'smallint', length: 1}) + public scanned: boolean; + @ManyToOne(type => DirectoryEntity, directory => directory.directories) public parent: DirectoryEntity;