From 395fcca80d0d171e70782dc8d3f8aae1782a8b51 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Mon, 14 Jul 2025 08:21:10 -0700 Subject: [PATCH] refactor(api/services): don't scan library files twice on rescan --- .../service/library/LibraryProcessingService.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index 7f8c028ec..6c6c20e04 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -57,13 +57,14 @@ public class LibraryProcessingService { public void rescanLibrary(long libraryId) throws IOException { LibraryEntity libraryEntity = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + libraryEntity.getName())); - deleteRemovedBooks(detectDeletedBookIds(libraryEntity)); - processLibraryFiles(detectNewBookPaths(libraryEntity)); + List libraryFiles = getLibraryFiles(libraryEntity); + deleteRemovedBooks(detectDeletedBookIds(libraryFiles, libraryEntity)); + processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity)); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName())); } - public List detectDeletedBookIds(LibraryEntity libraryEntity) throws IOException { - Set currentFileNames = getLibraryFiles(libraryEntity).stream() + public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) throws IOException { + Set currentFileNames = libraryFiles.stream() .map(LibraryFile::getFileName) .collect(Collectors.toSet()); @@ -74,11 +75,11 @@ public class LibraryProcessingService { .collect(Collectors.toList()); } - public List detectNewBookPaths(LibraryEntity libraryEntity) throws IOException { + public static List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) throws IOException { Set existingFileNames = libraryEntity.getBookEntities().stream() .map(BookEntity::getFileName) .collect(Collectors.toSet()); - return getLibraryFiles(libraryEntity).stream() + return libraryFiles.stream() .filter(file -> !existingFileNames.contains(file.getFileName())) .collect(Collectors.toList()); }