Skip to content

Commit cee7266

Browse files
perf: Make import quicker by caching imported translations, and using them when checking for conflicts instead of using ImportDataManager, which queries the database
1 parent 9e5e03a commit cee7266

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

backend/data/src/main/kotlin/io/tolgee/service/dataImport/CoreImportFilesProcessor.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class CoreImportFilesProcessor(
6565
val errors = mutableListOf<ErrorResponseBody>()
6666
val warnings = mutableListOf<ErrorResponseBody>()
6767

68+
private val importedTranslations =
69+
mutableMapOf<ImportLanguage, MutableMap<ImportKey, MutableList<ImportTranslation>>>()
70+
6871
fun processFiles(files: Collection<ImportFileDto>?) {
6972
errors.addAll(processFilesRecursive(files))
7073
renderPossibleNamespacesWarning()
@@ -330,9 +333,17 @@ class CoreImportFilesProcessor(
330333
}
331334

332335
translationsByKeys.forEach { (key, translations) ->
333-
translations.forEach {
334-
it.key = key
335-
processTranslation(it)
336+
translations.forEach { translation ->
337+
importedTranslations.putIfAbsent(translation.language, mutableMapOf())
338+
importedTranslations.getValue(translation.language).putIfAbsent(key, mutableListOf())
339+
importedTranslations.getValue(translation.language).getValue(key).add(translation)
340+
}
341+
}
342+
343+
translationsByKeys.forEach { (key, translations) ->
344+
translations.forEach { translation ->
345+
translation.key = key
346+
processTranslation(translation)
336347
}
337348
}
338349

@@ -371,9 +382,10 @@ class CoreImportFilesProcessor(
371382
var isCollision = false
372383
val issues =
373384
mutableListOf<Pair<FileIssueType, Map<FileIssueParamType, String>>>()
374-
val storedTranslations =
375-
importDataManager
376-
.getStoredTranslations(newTranslation.key, newTranslation.language)
385+
val storedTranslations = importedTranslations[newTranslation.language]
386+
?.get(newTranslation.key)
387+
?: emptyList()
388+
377389
if (storedTranslations.isNotEmpty()) {
378390
isCollision = true
379391
storedTranslations.forEach { collision ->

backend/data/src/main/kotlin/io/tolgee/service/dataImport/ImportDataManager.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,6 @@ class ImportDataManager(
108108
namespaceService.getAllInProject(import.project.id).map { it.name to it }.toMap(mutableMapOf())
109109
}
110110

111-
/**
112-
* Returns list of translations provided for a language and a key.
113-
* It returns collection since translations could collide, when a user uploads a file with different values
114-
* for a key
115-
*/
116-
fun getStoredTranslations(
117-
key: ImportKey,
118-
language: ImportLanguage,
119-
): MutableList<ImportTranslation> {
120-
this.populateStoredTranslations(language)
121-
val languageData = this.storedTranslations[language]!!
122-
123-
return languageData[key] ?: let {
124-
languageData[key] = mutableListOf()
125-
languageData[key]!!
126-
}
127-
}
128-
129111
fun getStoredTranslations(language: ImportLanguage): List<ImportTranslation> {
130112
return this.populateStoredTranslations(language).flatMap { it.value }
131113
}

0 commit comments

Comments
 (0)