-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #575 from woowacourse-teams/refactor/539-batch
batch insert main merge
- Loading branch information
Showing
17 changed files
with
354 additions
and
86 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...omain/cafe/CustomFunctionContributor.java → ...afe/config/CustomFunctionContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
server/src/main/java/com/project/yozmcafe/domain/cafe/UnViewedCafeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.project.yozmcafe.domain.cafe; | ||
|
||
import com.project.yozmcafe.domain.member.Member; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class UnViewedCafeRepository { | ||
|
||
private static final int INSERT_BATCH_SIZE = 110; | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UnViewedCafeRepository(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
@Transactional | ||
public void saveUnViewedCafes(final List<Cafe> cafes, final Member member) { | ||
for (int i = 0; i < cafes.size(); i += INSERT_BATCH_SIZE) { | ||
List<Cafe> subItems = cafes.subList(i, Math.min(i + INSERT_BATCH_SIZE, cafes.size())); | ||
batchInsert(subItems, member.getId()); | ||
} | ||
} | ||
|
||
private void batchInsert(final List<Cafe> cafes, final String memberId) { | ||
final String sql = "INSERT INTO un_viewed_cafe (cafe_id, member_id) VALUES (?, ?)"; | ||
|
||
final List<Object[]> batchArguments = cafes.stream() | ||
.map(cafe -> new Object[]{cafe.getId(), memberId}) | ||
.toList(); | ||
|
||
jdbcTemplate.batchUpdate(sql, batchArguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/src/main/java/com/project/yozmcafe/service/CafeRefillEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.project.yozmcafe.service; | ||
|
||
import com.project.yozmcafe.domain.member.Member; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
public class CafeRefillEvent extends ApplicationEvent { | ||
|
||
private final Member member; | ||
|
||
public CafeRefillEvent(final Object source, final Member member) { | ||
super(source); | ||
this.member = member; | ||
} | ||
|
||
public Member getMember() { | ||
return member; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
server/src/main/java/com/project/yozmcafe/service/CafeRefillEventPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.project.yozmcafe.service; | ||
|
||
import com.project.yozmcafe.domain.member.Member; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CafeRefillEventPublisher { | ||
|
||
private final ApplicationEventPublisher eventPublisher; | ||
|
||
public CafeRefillEventPublisher(final ApplicationEventPublisher eventPublisher) { | ||
this.eventPublisher = eventPublisher; | ||
} | ||
|
||
public void publishCafeRefillEvent(final Member member) { | ||
eventPublisher.publishEvent(new CafeRefillEvent(this, member)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
server/src/main/java/com/project/yozmcafe/service/UnViewedCafeRefillListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.project.yozmcafe.service; | ||
|
||
import com.project.yozmcafe.domain.member.Member; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
@Component | ||
public class UnViewedCafeRefillListener { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(UnViewedCafeRefillListener.class); | ||
|
||
private final UnViewedCafeService unViewedCafeService; | ||
|
||
public UnViewedCafeRefillListener(final UnViewedCafeService unViewedCafeService) { | ||
this.unViewedCafeService = unViewedCafeService; | ||
} | ||
|
||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
public void listen(final CafeRefillEvent event) { | ||
final Member member = event.getMember(); | ||
try { | ||
unViewedCafeService.refillUnViewedCafes(member); | ||
} catch (Exception e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
server/src/main/resources/META-INF/services/org.hibernate.boot.model.FunctionContributor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
com.project.yozmcafe.domain.cafe.CustomFunctionContributor | ||
com.project.yozmcafe.config.CustomFunctionContributor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
server/src/test/java/com/project/yozmcafe/domain/cafe/UnViewedCafeRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.project.yozmcafe.domain.cafe; | ||
|
||
import com.project.yozmcafe.BaseTest; | ||
import com.project.yozmcafe.domain.member.Member; | ||
import com.project.yozmcafe.domain.member.MemberRepository; | ||
import com.project.yozmcafe.fixture.Fixture; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UnViewedCafeRepositoryTest extends BaseTest { | ||
|
||
@Autowired | ||
private UnViewedCafeRepository unViewedCafeRepository; | ||
|
||
@Autowired | ||
private CafeRepository cafeRepository; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@Test | ||
@DisplayName("unViewedCafe를 batch insert한다.") | ||
void batchInsertUnViewedCafes() { | ||
// given | ||
final List<Cafe> cafes = new ArrayList<>(); | ||
for (int i = 0; i < 200; i++) { | ||
cafes.add(Fixture.getCafe("name", "address", 0)); | ||
} | ||
final List<Cafe> savedCafes = cafeRepository.saveAll(cafes); | ||
final Member member = memberRepository.save(new Member("id", "name", "image")); | ||
|
||
//when | ||
unViewedCafeRepository.saveUnViewedCafes(savedCafes, member); | ||
|
||
// then | ||
final Member refilledMember = memberRepository.findWithUnViewedCafesById(member.getId()).get(); | ||
assertThat(refilledMember.getUnViewedCafes()).hasSize(200); | ||
} | ||
} |
Oops, something went wrong.