Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
fix(perf): optimize select all query for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
l3d00m committed Aug 11, 2024
1 parent c728f67 commit e2a9c90
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/main/java/org/pvh/repository/GlassesRepository.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.pvh.repository;

import org.pvh.model.entity.Glasses;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface GlassesRepository extends JpaRepository<Glasses, Long>, CustomGlassesRepository, JpaSpecificationExecutor<Glasses> {

Page<Glasses> findByDispensedAndGlassesTypeAndLocation(boolean dispensed, String location, String glassesType, Pageable pageable);
@Query(value = "SELECT distinct g from Glasses g " +
"join fetch g.od join fetch g.os join fetch g.dispense " +
"where g.dispensed = :dispensed and g.location = :location ")
List<Glasses> findByDispensedAndLocation(boolean dispensed, String location);

List<Glasses> findByGlassesTypeContaining(String glassesType, Sort sort);

Expand Down
47 changes: 18 additions & 29 deletions src/main/java/org/pvh/rest/GlassesRestController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@

package org.pvh.rest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.pvh.error.NoSkusLeftException;
import org.pvh.error.PVHException;
import org.pvh.model.dto.ChangeValueDTO;
Expand Down Expand Up @@ -41,23 +32,13 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@RestController
@CrossOrigin(exposedHeaders = "errors, content-type")
Expand Down Expand Up @@ -131,6 +112,14 @@ public ResponseEntity<Map<String, Object>> getAllGlassesPaginated(
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
@GetMapping(path = "/{location}/all")
public Collection<GlassesResponseDTO> getAllGlasses(@PathVariable("location") String location) {
List<Glasses> pageGlasses = mainService.findByDispensedAndLocation(false, location);

return pageGlasses.stream().map(a -> GlassesMapperImpl.getInstance().glassesToGlassesResponseDTO(a)).toList();
}

@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
@GetMapping(path = "/{location}.csv")
@ResponseBody
Expand Down Expand Up @@ -349,4 +338,4 @@ public ResponseEntity<Void> addUnsuccessfulSearch(@PathVariable("location") Stri
}


}
}
12 changes: 6 additions & 6 deletions src/main/java/org/pvh/service/MainServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.pvh.service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.pvh.error.NoSkusLeftException;
import org.pvh.model.entity.Dispense;
import org.pvh.model.entity.Glasses;
Expand All @@ -23,6 +19,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class MainServiceImpl implements MainService {

Expand Down Expand Up @@ -134,7 +134,7 @@ public Page<Glasses> findByDispensedAndLocation(boolean dispensed, String locati

@Override
public List<Glasses> findByDispensedAndLocation(boolean dispensed, String location) throws DataAccessException {
return glassesRepository.findAll(Specification.where(GlassesSpecs.hasLocation(location)).and(GlassesSpecs.isDispensed(dispensed)));
return glassesRepository.findByDispensedAndLocation(dispensed, location);
}

@Override
Expand All @@ -158,4 +158,4 @@ public UnsuccessfulSearch saveUnsuccessfulSearch(UnsuccessfulSearch search) {
return unsuccessfulSearchRepository.save(search);
}

}
}

0 comments on commit e2a9c90

Please sign in to comment.