package kr.co.vividnext.sodalive.content import kr.co.vividnext.sodalive.i18n.translation.DetectedLanguage import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl import kr.co.vividnext.sodalive.i18n.translation.translationCallStop import org.springframework.stereotype.Service import org.springframework.transaction.PlatformTransactionManager import org.springframework.transaction.TransactionDefinition import org.springframework.transaction.annotation.Propagation import org.springframework.transaction.annotation.Transactional import org.springframework.transaction.support.TransactionTemplate @Service class LanguageDetectionCacheService( private val languageDetectionResultRepository: LanguageDetectionResultRepository, providers: List, properties: LanguageProviderProperties, transactionManager: PlatformTransactionManager ) { private val providers = properties.orderedProviders(properties.detectionOrder, providers) { it.providerId } private val transaction = TransactionTemplate(transactionManager).apply { propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW } @Transactional(propagation = Propagation.NOT_SUPPORTED) fun detectWithCache(query: String): DetectedLanguage? = detect(query, null) @Transactional(propagation = Propagation.NOT_SUPPORTED) fun detectWithCache(query: String, authorizeDispatch: () -> Boolean): DetectedLanguage? { return detect(query, TranslationCallControl(authorizeDispatch)) } private fun detect(query: String, control: TranslationCallControl?): DetectedLanguage? { val normalizedQuery = SourceTextNormalizer.normalize(query) if (normalizedQuery.isBlank()) return null val sourceHash = SourceTextNormalizer.hash(normalizedQuery) providers.forEach { provider -> val cached = transaction.execute { languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion( sourceHash = sourceHash, provider = provider.providerId.code, normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION ) } if (cached != null && cached.detectedLanguage.isNotBlank()) { return DetectedLanguage(cached.detectedLanguage, provider.providerId) } } providers.forEach { provider -> val detectedLanguage = try { control?.beforeCall() if (control == null) provider.detect(query) else provider.detect(query, control) } catch (error: Exception) { if (error.translationCallStop() != null) return null null }?.takeIf { it.isNotBlank() }?.lowercase() ?: return@forEach val winner = transaction.execute { languageDetectionResultRepository.insertIfAbsent( sourceHash = sourceHash, sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH), detectedLanguage = detectedLanguage, provider = provider.providerId.code, confidence = null, normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION ) requireNotNull( languageDetectionResultRepository.findDetectedLanguageForUpdate( sourceHash = sourceHash, provider = provider.providerId.code, normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION ) ) } return DetectedLanguage(requireNotNull(winner), provider.providerId) } return null } companion object { private const val MAX_SAMPLE_LENGTH = 500 } }