Add method to fetch all photo paths and optimize database population logic
This commit is contained in:
parent
3e67226b67
commit
e84f597f18
|
|
@ -1,6 +1,7 @@
|
||||||
package com.example.PhotoGallery.Repos;
|
package com.example.PhotoGallery.Repos;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
@ -15,4 +16,7 @@ public interface PhotoRepo extends JpaRepository<Photo, Long> {
|
||||||
|
|
||||||
@Query("SELECT p.thumbnail_path FROM Photo p")
|
@Query("SELECT p.thumbnail_path FROM Photo p")
|
||||||
List<String> findAllThumbnailPaths();
|
List<String> findAllThumbnailPaths();
|
||||||
|
|
||||||
|
@Query("SELECT p.path FROM Photo p")
|
||||||
|
Set<String> findAllPaths();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,10 @@ import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -40,10 +42,14 @@ public class DatabasePopulation {
|
||||||
private String originalImgPath;
|
private String originalImgPath;
|
||||||
|
|
||||||
public void populateDatabase() {
|
public void populateDatabase() {
|
||||||
|
//Fetch all existing photo paths from the DB in one query.
|
||||||
|
Set<String> existingPaths = photoRepo.findAllPaths();
|
||||||
|
|
||||||
for (String path : getImagePaths()) {
|
//Get all image paths from the file system.
|
||||||
|
List<String> allImagePaths = getImagePaths();
|
||||||
|
|
||||||
if (!photoRepo.existsByPath(path)) {
|
for (String path : allImagePaths) {
|
||||||
|
if (!existingPaths.contains(path)) {
|
||||||
|
|
||||||
if (!imageCompressor.compressImage(path, compressedImgPath)) {
|
if (!imageCompressor.compressImage(path, compressedImgPath)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
|
||||||
|
|
||||||
import com.example.PhotoGallery.Models.Photo;
|
import com.example.PhotoGallery.Models.Photo;
|
||||||
import com.example.PhotoGallery.Repos.PhotoRepo;
|
import com.example.PhotoGallery.Repos.PhotoRepo;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue