Skip to content

Commit

Permalink
feat: Increase performance by bulk inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
iamareebjamal committed Sep 14, 2017
1 parent 3ad1ea5 commit cd84ce3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
27 changes: 26 additions & 1 deletion src/main/java/in/ac/amu/zhcet/service/core/StudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import in.ac.amu.zhcet.data.model.user.Type;
import in.ac.amu.zhcet.data.model.user.UserAuth;
import in.ac.amu.zhcet.data.repository.StudentRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StudentService {

Expand Down Expand Up @@ -48,7 +52,7 @@ public Student getByFacultyNumber(String userId) {
return studentRepository.getByFacultyNumber(userId);
}

private static void initializeStudent(Student student) {
private static Student initializeStudent(Student student) {
student.getUser().setType(Type.STUDENT);

if (student.getUser().getUserId() == null)
Expand All @@ -59,6 +63,8 @@ private static void initializeStudent(Student student) {
student.getUser().setPassword(student.getFacultyNumber());

student.getUser().setPassword(UserAuth.PASSWORD_ENCODER.encode(student.getUser().getPassword()));

return student;
}

@Transactional
Expand All @@ -69,6 +75,25 @@ public void register(Student student) {
studentRepository.save(student);
}

@Transactional
public void register(Set<Student> students) {
log.info("Initializing Students");
List<Student> studentList = students.parallelStream()
.map(StudentService::initializeStudent)
.collect(Collectors.toList());
log.info("Students Initialized");
log.info("Saving Users");
List<UserAuth> userAuths = students.parallelStream()
.map(Student::getUser)
.collect(Collectors.toList());
log.info("Extracted Users");
userService.save(userAuths);
log.info("Saved Users");
log.info("Saving students");
studentRepository.save(studentList);
log.info("Saved Students");
}

@Transactional
public void save(Student student) {
studentRepository.save(student);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/in/ac/amu/zhcet/service/core/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public void save(UserAuth userAuth) {
userRepository.save(userAuth);
}

public void save(List<UserAuth> userAuths) {
userRepository.save(userAuths);
}

public UserAuth findById(String id) {
return userRepository.findByUserId(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public Confirmation<Student, String> confirmUpload(UploadResult<StudentUpload> u

@Transactional
public void registerStudents(Confirmation<Student, String> confirmation) {
for (Student student : confirmation.getData().keySet()) {
studentService.register(student);
}
studentService.register(confirmation.getData().keySet());
}
}

0 comments on commit cd84ce3

Please sign in to comment.