Skip to content

Commit

Permalink
⚡ (postservice) fix category query huge return size in bytes; do not …
Browse files Browse the repository at this point in the history
…relate to posts and comments
  • Loading branch information
Tobias-Pe committed Jul 31, 2024
1 parent 2702492 commit 6610a83
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.hm.peslalz.thesis.postservice.controller;

import edu.hm.peslalz.thesis.postservice.entity.Category;
import edu.hm.peslalz.thesis.postservice.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.log4j.Log4j2;
Expand All @@ -22,7 +21,7 @@ public CategoryController(CategoryService categoryService) {

@Operation(description = "Get all categories")
@GetMapping
public Page<Category> getCategories(@RequestParam(defaultValue = "0") int page) {
public Page<String> getCategories(@RequestParam(defaultValue = "0") int page) {
log.info("Get categories | Page: {}",page);
return categoryService.getCategories(page);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@
import edu.hm.peslalz.thesis.postservice.entity.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;

import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, String> {

@Override
@EntityGraph(attributePaths = {"posts", "posts.comments"})
@NonNull
List<Category> findAllById(@NonNull Iterable<String> strings);

@Query("select c.name from Category c")
Page<String> findAllNames(Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package edu.hm.peslalz.thesis.postservice.service;

import edu.hm.peslalz.thesis.postservice.entity.Category;
import edu.hm.peslalz.thesis.postservice.repository.CategoryRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class CategoryService {
CategoryRepository categoryRepository;
Expand All @@ -18,10 +14,8 @@ public CategoryService(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}

public Page<Category> getCategories(@RequestParam(defaultValue = "0") int page) {
public Page<String> getCategories(@RequestParam(defaultValue = "0") int page) {
// don't page in memory --> page fetch only id's and then load the joined data
Page<String> allNames = categoryRepository.findAllNames(PageRequest.of(page, 50));
List<Category> joinedData = categoryRepository.findAllById(allNames.toSet());
return new PageImpl<>(joinedData, allNames.getPageable(), allNames.getTotalElements());
return categoryRepository.findAllNames(PageRequest.of(page, 50));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ void scenario() throws IOException {
Assertions.assertThat(comment.getLikes()).isEqualTo(1);

postController.createPost(1, "NewzFromMyLaif", Set.of("life", "blog"), null);
List<Category> categories = categoryController.getCategories(0).getContent();
List<String> categories = categoryController.getCategories(0).getContent();
Assertions.assertThat(categories).hasSize(3);
Set<Post> blogPosts = categories.stream().filter(category -> "blog".equals(category.getName())).findFirst().get().getPosts();
Assertions.assertThat(blogPosts).hasSize(2);
}

@Test
Expand Down

0 comments on commit 6610a83

Please sign in to comment.