From fb7ed27012f7cf27d108a47caf2f183e18c53286 Mon Sep 17 00:00:00 2001 From: Leane Schlundt Date: Fri, 7 Jun 2024 08:57:51 +0200 Subject: [PATCH] Remove only maia annotations of deleted images --- src/Jobs/DetectionJob.php | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Jobs/DetectionJob.php b/src/Jobs/DetectionJob.php index 6ac6c7a..626cb24 100644 --- a/src/Jobs/DetectionJob.php +++ b/src/Jobs/DetectionJob.php @@ -11,7 +11,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Queue; use Biigle\Image; -use Biigle\ImageAnnotation; +use Illuminate\Support\Arr; abstract class DetectionJob implements ShouldQueue { @@ -219,13 +219,25 @@ protected function createMaiaAnnotations(array $annotations) return $this->createMaiaAnnotation($annotation); }, $annotations); - // Remove annotations of already deleted images. - foreach ($maiaAnnotations as $idx => $a) { - if (Image::where('id', '=', $a['image_id'])->doesntExist()) { - ImageAnnotation::where('image_id', '=', $a['image_id'])->delete(); - unset($maiaAnnotations[$idx]); + // Remove maia annotations of deleted images + $deletedImgId = []; + $maiaAnnotations = Arr::where($maiaAnnotations, function ($a) use ($deletedImgId) { + $imgId = $a['image_id']; + if(Arr::has($deletedImgId, $imgId)) { + if($deletedImgId[$imgId]) { + return false; + } + } else { + $isDeleted = Image::where('id', '=', $imgId)->doesntExist(); + if($isDeleted) { + $deletedImgId[$imgId] = true; + return false; + } else { + $deletedImgId[$imgId] = false; + } } - } + return true; + }); // Chunk the insert because PDO's maximum number of query parameters is // 65535. Each annotation has 7 parameters so we can store roughly 9000