refactor(specifications): replace dynamic array creation with static empty arrays for improved performance (#883)

This commit is contained in:
brios
2026-04-26 19:16:26 +02:00
committed by GitHub
parent ed35da2f22
commit 2cfcc64ce6
5 changed files with 18 additions and 18 deletions
@@ -1051,7 +1051,7 @@ public class AppBookService {
specs.add(AppBookSpecification.withProgress(userId, true)); specs.add(AppBookSpecification.withProgress(userId, true));
} }
return AppBookSpecification.combine(specs.toArray(new Specification[0])); return AppBookSpecification.combine(specs.toArray(Specification[]::new));
} }
private String getSortField(String sortBy) { private String getSortField(String sortBy) {
@@ -1118,7 +1118,7 @@ public class AppBookService {
specs.add(AppBookSpecification.inLibrary(libraryId)); specs.add(AppBookSpecification.inLibrary(libraryId));
} }
return AppBookSpecification.combine(specs.toArray(new Specification[0])); return AppBookSpecification.combine(specs.toArray(Specification[]::new));
} }
private AppPageResponse<AppBookSummary> buildPageResponse( private AppPageResponse<AppBookSummary> buildPageResponse(
@@ -373,7 +373,7 @@ public class AppSeriesService {
specs.add(AppBookSpecification.inLibrary(libraryId)); specs.add(AppBookSpecification.inLibrary(libraryId));
} }
return AppBookSpecification.combine(specs.toArray(new Specification[0])); return AppBookSpecification.combine(specs.toArray(Specification[]::new));
} }
private Map<Long, UserBookProgressEntity> getProgressMap(Long userId, Set<Long> bookIds) { private Map<Long, UserBookProgressEntity> getProgressMap(Long userId, Set<Long> bookIds) {
@@ -274,7 +274,7 @@ public class AppBookSpecification {
.where(cb.equal(bfRoot.get("bookType"), ft)); .where(cb.equal(bfRoot.get("bookType"), ft));
predicates.add(root.get("id").in(sub)); predicates.add(root.get("id").in(sub));
} }
return cb.and(predicates.toArray(new Predicate[0])); return cb.and(predicates.toArray(Predicate[]::new));
} }
Subquery<Long> sub = query.subquery(Long.class); Subquery<Long> sub = query.subquery(Long.class);
@@ -581,7 +581,7 @@ public class AppBookSpecification {
default -> throw new APIException("Invalid ageRating bucket ID: " + id, HttpStatus.BAD_REQUEST); default -> throw new APIException("Invalid ageRating bucket ID: " + id, HttpStatus.BAD_REQUEST);
}); });
} }
Predicate combined = cb.or(predicates.toArray(new Predicate[0])); Predicate combined = cb.or(predicates.toArray(Predicate[]::new));
return "not".equals(mode) ? cb.not(combined) : combined; return "not".equals(mode) ? cb.not(combined) : combined;
}; };
} }
@@ -613,7 +613,7 @@ public class AppBookSpecification {
default -> throw new APIException("Invalid matchScore bucket ID: " + id, HttpStatus.BAD_REQUEST); default -> throw new APIException("Invalid matchScore bucket ID: " + id, HttpStatus.BAD_REQUEST);
}); });
} }
Predicate combined = cb.or(predicates.toArray(new Predicate[0])); Predicate combined = cb.or(predicates.toArray(Predicate[]::new));
return "not".equals(mode) ? cb.not(combined) : combined; return "not".equals(mode) ? cb.not(combined) : combined;
}; };
} }
@@ -663,7 +663,7 @@ public class AppBookSpecification {
sub.select(cb.literal(1L)) sub.select(cb.literal(1L))
.where(cb.equal(bfJoin.get("id"), minIdSub), .where(cb.equal(bfJoin.get("id"), minIdSub),
cb.or(predicates.toArray(new Predicate[0]))); cb.or(predicates.toArray(Predicate[]::new)));
return "not".equals(mode) ? cb.not(cb.exists(sub)) : cb.exists(sub); return "not".equals(mode) ? cb.not(cb.exists(sub)) : cb.exists(sub);
}; };
@@ -728,7 +728,7 @@ public class AppBookSpecification {
default -> throw new APIException("Invalid " + fieldName + " bucket ID: " + id, HttpStatus.BAD_REQUEST); default -> throw new APIException("Invalid " + fieldName + " bucket ID: " + id, HttpStatus.BAD_REQUEST);
}); });
} }
Predicate combined = cb.or(predicates.toArray(new Predicate[0])); Predicate combined = cb.or(predicates.toArray(Predicate[]::new));
return "not".equals(mode) ? cb.not(combined) : combined; return "not".equals(mode) ? cb.not(combined) : combined;
}; };
} }
@@ -752,7 +752,7 @@ public class AppBookSpecification {
default -> throw new APIException("Invalid pageCount bucket ID: " + id, HttpStatus.BAD_REQUEST); default -> throw new APIException("Invalid pageCount bucket ID: " + id, HttpStatus.BAD_REQUEST);
}); });
} }
Predicate combined = cb.or(predicates.toArray(new Predicate[0])); Predicate combined = cb.or(predicates.toArray(Predicate[]::new));
return "not".equals(mode) ? cb.not(combined) : combined; return "not".equals(mode) ? cb.not(combined) : combined;
}; };
} }
@@ -823,7 +823,7 @@ public class AppBookSpecification {
sub.select(cb.literal(1L)).where(cb.equal(subShelves.get("id"), id)); sub.select(cb.literal(1L)).where(cb.equal(subShelves.get("id"), id));
predicates.add(cb.exists(sub)); predicates.add(cb.exists(sub));
} }
return cb.and(predicates.toArray(new Predicate[0])); return cb.and(predicates.toArray(Predicate[]::new));
} }
Subquery<Long> sub = query.subquery(Long.class); Subquery<Long> sub = query.subquery(Long.class);
@@ -875,13 +875,13 @@ public class AppBookSpecification {
where.add(cb.equal(mappingRoot.get("role"), role)); where.add(cb.equal(mappingRoot.get("role"), role));
} }
sub.select(cb.literal(1L)).where(where.toArray(new Predicate[0])); sub.select(cb.literal(1L)).where(where.toArray(Predicate[]::new));
predicates.add(cb.exists(sub)); predicates.add(cb.exists(sub));
} }
Predicate combined = "and".equals(mode) Predicate combined = "and".equals(mode)
? cb.and(predicates.toArray(new Predicate[0])) ? cb.and(predicates.toArray(Predicate[]::new))
: cb.or(predicates.toArray(new Predicate[0])); : cb.or(predicates.toArray(Predicate[]::new));
return "not".equals(mode) ? cb.not(combined) : combined; return "not".equals(mode) ? cb.not(combined) : combined;
}; };
} }
@@ -953,7 +953,7 @@ public class AppBookSpecification {
); );
predicates.add(cb.exists(sub)); predicates.add(cb.exists(sub));
} }
return cb.and(predicates.toArray(new Predicate[0])); return cb.and(predicates.toArray(Predicate[]::new));
} }
// OR or NOT: single EXISTS subquery with IN clause // OR or NOT: single EXISTS subquery with IN clause
@@ -85,7 +85,7 @@ public class SecurityConfig {
.csrf(AbstractHttpConfigurer::disable) .csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth .authorizeHttpRequests(auth -> auth
.requestMatchers(unauthenticatedEndpoints.toArray(new String[0])).permitAll() .requestMatchers(unauthenticatedEndpoints.toArray(String[]::new)).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
) )
.httpBasic(basic -> basic .httpBasic(basic -> basic
@@ -88,8 +88,8 @@ public class BookRuleEvaluatorService {
} }
return group.getJoin() == org.booklore.model.dto.JoinType.AND return group.getJoin() == org.booklore.model.dto.JoinType.AND
? cb.and(predicates.toArray(new Predicate[0])) ? cb.and(predicates.toArray(Predicate[]::new))
: cb.or(predicates.toArray(new Predicate[0])); : cb.or(predicates.toArray(Predicate[]::new));
} }
private Predicate buildRulePredicate(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin, Long userId) { private Predicate buildRulePredicate(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin, Long userId) {
@@ -870,7 +870,7 @@ public class BookRuleEvaluatorService {
}) })
.toList(); .toList();
return cb.and(predicates.toArray(new Predicate[0])); return cb.and(predicates.toArray(Predicate[]::new));
} else { } else {
Subquery<Long> subquery = query.subquery(Long.class); Subquery<Long> subquery = query.subquery(Long.class);
Root<BookEntity> subRoot = subquery.from(BookEntity.class); Root<BookEntity> subRoot = subquery.from(BookEntity.class);