Skip to content

Commit

Permalink
db에서 한번에 읽어오기
Browse files Browse the repository at this point in the history
  • Loading branch information
asp345 committed Aug 17, 2024
1 parent da8e840 commit 748fc6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SnuttLectureSyncJobConfig(
private val semesterLectureRepository: SemesterLectureRepository,
private val lectureRepository: LectureRepository,
private val snuttLectureIdMapRepository: SnuttLectureIdMapRepository,
private val ratingSyncJob: Job,
) {
companion object {
private const val JOB_NAME = "SYNC_JOB"
Expand Down Expand Up @@ -78,6 +79,7 @@ class SnuttLectureSyncJobConfig(
),
),
)
.next(ratingSyncJobStep(jobRepository))
.build()
}

Expand All @@ -93,6 +95,7 @@ class SnuttLectureSyncJobConfig(
.toMutableMap()
return JobBuilder(JOB_NAME, jobRepository)
.start(customReaderStep(jobRepository, Query()))
.next(ratingSyncJobStep(jobRepository))
.build()
}

Expand Down Expand Up @@ -169,6 +172,11 @@ class SnuttLectureSyncJobConfig(
snuttLectureIdMapRepository.saveAll(items.map { it.snuttLectureIdMap })
}
}

private fun ratingSyncJobStep(jobRepository: JobRepository): Step =
StepBuilder(SnuttRatingSyncJobConfig.RATING_SYNC_JOB_NAME, jobRepository)
.job(ratingSyncJob)
.build()
}

data class SyncProcessResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import org.springframework.orm.jpa.JpaTransactionManager
class SnuttRatingSyncJobConfig(
private val entityManagerFactory: EntityManagerFactory,
private val mongoTemplate: MongoTemplate,
lectureRepository: LectureRepository,
) {
companion object {
private const val JOB_NAME = "RATING_SYNC_JOB"
private const val CUSTOM_READER_JOB_STEP = JOB_NAME + "_STEP"
private const val CHUNK_SIZE = 1000
const val RATING_SYNC_JOB_NAME = "RATING_SYNC_JOB"
private const val CUSTOM_READER_JOB_STEP = RATING_SYNC_JOB_NAME + "_STEP"
private const val CHUNK_SIZE = 10000
}

private var lectureIdtoLectureRatingMap: MutableMap<Long, LectureRatingDao> = mutableMapOf()
private val lectureIdtoLectureRatingMap = lectureRepository.findAllRatings().associateBy { it.id }

@Bean
fun ratingSyncJob(job: JobRepository, jobRepository: JobRepository, lectureRepository: LectureRepository): Job {
lectureIdtoLectureRatingMap = lectureRepository.findAllRatings().associateBy { it.id }.toMutableMap()
return JobBuilder(JOB_NAME, jobRepository)
fun ratingSyncJob(job: JobRepository, jobRepository: JobRepository): Job {
return JobBuilder(RATING_SYNC_JOB_NAME, jobRepository)
.start(customReaderStep(jobRepository))
.build()
}
Expand All @@ -63,7 +63,7 @@ class SnuttRatingSyncJobConfig(
JpaPagingItemReaderBuilder<SnuttLectureIdMap>()
.name("snuttLectureIdMapReader")
.entityManagerFactory(entityManagerFactory)
.queryString("SELECT s FROM SnuttLectureIdMap s")
.queryString("SELECT s FROM SnuttLectureIdMap s JOIN FETCH s.semesterLecture")
.pageSize(CHUNK_SIZE)
.build()

Expand All @@ -76,11 +76,12 @@ class SnuttRatingSyncJobConfig(
return ItemWriter { items ->
val bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "lectures")
items.forEach {
(snuttId, evInfo) ->
bulkOps.updateOne(
Query(Criteria.where("_id").`is`(it.first)),
Update().set("evInfo.evId", it.second?.id)
.set("evInfo.avgRating", it.second?.avgRating)
.set("evInfo.count", it.second?.count),
Query(Criteria.where("_id").`is`(snuttId)),
Update().set("evInfo.evId", evInfo?.id)
.set("evInfo.avgRating", evInfo?.avgRating)
.set("evInfo.count", evInfo?.count),
)
}
bulkOps.execute()
Expand Down

0 comments on commit 748fc6f

Please sign in to comment.