From e84f597f181d05a6ef75f50820f7c466beb2a3f5 Mon Sep 17 00:00:00 2001 From: Kiyan Date: Sat, 4 Oct 2025 19:06:25 +0200 Subject: [PATCH] Add method to fetch all photo paths and optimize database population logic --- .../java/com/example/PhotoGallery/Repos/PhotoRepo.java | 4 ++++ .../PhotoGallery/Services/DatabasePopulation.java | 10 ++++++++-- .../example/PhotoGallery/Services/PhotoService.java | 1 - 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/PhotoGallery/Repos/PhotoRepo.java b/src/main/java/com/example/PhotoGallery/Repos/PhotoRepo.java index 80d876c..38360c4 100644 --- a/src/main/java/com/example/PhotoGallery/Repos/PhotoRepo.java +++ b/src/main/java/com/example/PhotoGallery/Repos/PhotoRepo.java @@ -1,6 +1,7 @@ package com.example.PhotoGallery.Repos; import java.util.List; +import java.util.Set; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -15,4 +16,7 @@ public interface PhotoRepo extends JpaRepository { @Query("SELECT p.thumbnail_path FROM Photo p") List findAllThumbnailPaths(); + + @Query("SELECT p.path FROM Photo p") + Set findAllPaths(); } diff --git a/src/main/java/com/example/PhotoGallery/Services/DatabasePopulation.java b/src/main/java/com/example/PhotoGallery/Services/DatabasePopulation.java index 7394580..c189d60 100644 --- a/src/main/java/com/example/PhotoGallery/Services/DatabasePopulation.java +++ b/src/main/java/com/example/PhotoGallery/Services/DatabasePopulation.java @@ -6,8 +6,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.stream.Collectors; import org.json.simple.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -40,10 +42,14 @@ public class DatabasePopulation { private String originalImgPath; public void populateDatabase() { + //Fetch all existing photo paths from the DB in one query. + Set existingPaths = photoRepo.findAllPaths(); - for (String path : getImagePaths()) { + //Get all image paths from the file system. + List allImagePaths = getImagePaths(); - if (!photoRepo.existsByPath(path)) { + for (String path : allImagePaths) { + if (!existingPaths.contains(path)) { if (!imageCompressor.compressImage(path, compressedImgPath)) { continue; diff --git a/src/main/java/com/example/PhotoGallery/Services/PhotoService.java b/src/main/java/com/example/PhotoGallery/Services/PhotoService.java index 709ea04..c70fdad 100644 --- a/src/main/java/com/example/PhotoGallery/Services/PhotoService.java +++ b/src/main/java/com/example/PhotoGallery/Services/PhotoService.java @@ -4,7 +4,6 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.ui.Model; import com.example.PhotoGallery.Models.Photo; import com.example.PhotoGallery.Repos.PhotoRepo;