Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

タイムライン取得時のロジックを高速化した #1934

Merged
merged 10 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/data/objectbox-models/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"name": "accountIdAndNoteId",
"indexId": "1:7288350540838608646",
"type": 9,
"flags": 2080
"flags": 34848
},
{
"id": "5:7710661194467523389",
Expand Down
7 changes: 6 additions & 1 deletion modules/data/objectbox-models/default.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"entities": [
{
"id": "1:4355718382021751829",
"lastPropertyId": "54:1696546822899785376",
"lastPropertyId": "55:68607803971520561",
"name": "NoteRecord",
"properties": [
{
Expand Down Expand Up @@ -286,6 +286,11 @@
"id": "54:1696546822899785376",
"name": "customEmojiUrlAndCachePathMap",
"type": 13
},
{
"id": "55:68607803971520561",
"name": "misskeyIsRequireNyaize",
"type": 1
}
],
"relations": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.pantasystem.milktea.model.account.Account
import net.pantasystem.milktea.model.channel.Channel
import net.pantasystem.milktea.model.drive.FileProperty
import net.pantasystem.milktea.model.emoji.CustomEmojiAspectRatioDataSource
import net.pantasystem.milktea.model.image.ImageCache
import net.pantasystem.milktea.model.image.ImageCacheRepository
import net.pantasystem.milktea.model.instance.InstanceInfoService
import net.pantasystem.milktea.model.instance.InstanceInfoType
Expand All @@ -26,15 +27,48 @@ class NoteDTOEntityConverter @Inject constructor(
private val instanceInfoService: InstanceInfoService,
) {

suspend fun convert(account: Account, noteDTO: NoteDTO, instanceInfoType: InstanceInfoType? = null): Note {
val emojis = (noteDTO.emojiList + (noteDTO.reactionEmojiList))

suspend fun convertAll(
account: Account,
noteDTOs: List<NoteDTO>,
instanceInfoType: InstanceInfoType? = null
): List<Note> {
val emojis = noteDTOs.flatMap {
it.emojiList + (it.reactionEmojiList)
}
val instanceInfo = instanceInfoType?.takeIf {
it.uri == account.normalizedInstanceUri
} ?: instanceInfoService.find(account.normalizedInstanceUri).getOrNull()
val aspects = customEmojiAspectRatioDataSource.findIn(emojis.mapNotNull {
it.url ?: it.uri
}).getOrElse {
emptyList()
}.associate {
it.uri to it.aspectRatio
}
val fileCaches = imageCacheRepository.findBySourceUrls(emojis.mapNotNull {
it.url ?: it.uri
}).associateBy {
it.sourceUrl
}

return noteDTOs.map {
convert(
account = account,
noteDTO = it,
instanceInfoType = instanceInfo,
aspects = aspects,
fileCaches = fileCaches
)
}
}

suspend fun convert(
account: Account,
noteDTO: NoteDTO,
instanceInfoType: InstanceInfoType? = null
): Note {
val emojis = noteDTO.emojiList

val isRequireNyaize = (instanceInfo?.isRequirePerformNyaizeFrontend ?: false)
&& (noteDTO.user.isCat ?: false)
val aspects = customEmojiAspectRatioDataSource.findIn(emojis.mapNotNull {
it.url ?: it.uri
}).getOrElse {
Expand All @@ -47,6 +81,26 @@ class NoteDTOEntityConverter @Inject constructor(
}).associateBy {
it.sourceUrl
}
val info = instanceInfoType ?: instanceInfoService.find(account.normalizedInstanceUri).getOrNull()
return convert(
account = account,
noteDTO = noteDTO,
instanceInfoType = info,
aspects = aspects,
fileCaches = fileCaches
)
}

private fun convert(
account: Account,
noteDTO: NoteDTO,
instanceInfoType: InstanceInfoType? = null,
aspects: Map<String, Float>,
fileCaches: Map<String, ImageCache>
): Note {
val emojis = noteDTO.emojiList
val isRequireNyaize = (instanceInfoType?.isRequirePerformNyaizeFrontend ?: false)
&& (noteDTO.user.isCat ?: false)
val visibility = Visibility(
noteDTO.visibility ?: NoteVisibilityType.Public,
isLocalOnly = noteDTO.localOnly ?: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.pantasystem.milktea.data.infrastructure

import net.pantasystem.milktea.api.misskey.groups.GroupDTO
import net.pantasystem.milktea.api.misskey.list.UserListDTO
import net.pantasystem.milktea.api.misskey.notes.NoteDTO
import net.pantasystem.milktea.model.account.Account
import net.pantasystem.milktea.model.drive.FileProperty
import net.pantasystem.milktea.model.group.Group
Expand Down Expand Up @@ -41,4 +42,11 @@ data class NoteRelationEntities(
val notes: List<Note>,
val users: List<User>,
val files: List<FileProperty>
)

data class NoteDTOUnpacked(
val note: NoteDTO,
val notes: List<NoteDTO>,
val users: List<User>,
val files: List<FileProperty>
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.pantasystem.milktea.data.converters.FilePropertyDTOEntityConverter
import net.pantasystem.milktea.data.converters.NoteDTOEntityConverter
import net.pantasystem.milktea.data.converters.TootDTOEntityConverter
import net.pantasystem.milktea.data.converters.UserDTOEntityConverter
import net.pantasystem.milktea.data.infrastructure.NoteRelationEntities
import net.pantasystem.milktea.data.infrastructure.NoteDTOUnpacked
import net.pantasystem.milktea.data.infrastructure.toEntities
import net.pantasystem.milktea.model.account.Account
import net.pantasystem.milktea.model.drive.FileProperty
Expand Down Expand Up @@ -40,18 +40,99 @@ class NoteDataSourceAdder @Inject constructor(
}


suspend fun addNoteDtoToDataSource(account: Account, noteDTO: NoteDTO, skipExists: Boolean = false, instanceType: InstanceInfoType? = null): Note {
suspend fun addNoteDtoListToDataSource(
account: Account,
noteDTOs: List<NoteDTO>,
skipExists: Boolean = false,
instanceType: InstanceInfoType? = null
): List<Note.Id> {
val info = instanceType?.takeIf {
it.uri == account.normalizedInstanceUri
} ?: instanceInfoService.find(account.normalizedInstanceUri).getOrNull()
val entities = noteDTOs.map {
it.toEntities(
account,
userDTOEntityConverter,
filePropertyDTOEntityConverter,
info,
)
}
val notes = entities.flatMap {
it.notes
} + entities.map {
it.note
}
if (skipExists) {
userDataSource.addAll(
entities.flatMap {
it.users
}.filterNot {
userDataSource.get(it.id).isSuccess
}
).onFailure {
logger.error("UserDataSourceへの追加に失敗", it)
}
noteDataSource.addAll(
noteDTOEntityConverter.convertAll(
account,
notes.filterNot {
noteDataSource.exists(Note.Id(account.accountId, it.id))
},
info,
)
).onFailure {
logger.error("NoteDataSourceへの追加に失敗", it)
}
filePropertyDataSource.addAll(
entities.flatMap {
it.files
}.filterNot {
filePropertyDataSource.find(it.id).isSuccess
}
).onFailure {
logger.error("FilePropertyDataSourceへの追加に失敗", it)
}
} else {
userDataSource.addAll(entities.flatMap {
it.users
}).onFailure {
logger.error("UserDataSourceへの追加に失敗", it)
}
noteDataSource.addAll(noteDTOEntityConverter.convertAll(
account,
notes,
info,
)).onFailure {
logger.error("NoteDataSourceへの追加に失敗", it)
}
filePropertyDataSource.addAll(entities.flatMap {
it.files
}).onFailure {
logger.error("FilePropertyDataSourceへの追加に失敗", it)
}
}
return entities.map {
Note.Id(account.accountId, it.note.id)
}
}

suspend fun addNoteDtoToDataSource(
account: Account,
noteDTO: NoteDTO,
skipExists: Boolean = false,
instanceType: InstanceInfoType? = null
): Note {
val info = instanceType?.takeIf {
it.uri == account.normalizedInstanceUri
} ?: instanceInfoService.find(account.normalizedInstanceUri).getOrNull()
val entities =
noteDTO.toEntities(
account,
userDTOEntityConverter,
noteDTOEntityConverter,
filePropertyDTOEntityConverter,
info,
)
val willReturnNote = noteDTOEntityConverter.convert(account, entities.note)
if (skipExists) {
userDataSource.addAll(
entities.users.filterNot {
Expand All @@ -61,14 +142,17 @@ class NoteDataSourceAdder @Inject constructor(
logger.error("UserDataSourceへの追加に失敗", it)
}
noteDataSource.addAll(
entities.notes.filterNot {
noteDataSource.exists(it.id)
}
noteDTOEntityConverter.convertAll(
account,
entities.notes.filterNot {
noteDataSource.exists(Note.Id(account.accountId, it.id))
}
)
).onFailure {
logger.error("NoteDataSourceへの追加に失敗", it)
}
if (!noteDataSource.exists(entities.note.id)) {
noteDataSource.add(entities.note).onFailure {
if (!noteDataSource.exists(Note.Id(account.accountId, entities.note.id))) {
noteDataSource.add(willReturnNote).onFailure {
logger.error("NoteDataSourceへの追加に失敗", it)
}
}
Expand All @@ -83,18 +167,25 @@ class NoteDataSourceAdder @Inject constructor(
userDataSource.addAll(entities.users).onFailure {
logger.error("UserDataSourceへの追加に失敗", it)
}
noteDataSource.addAll(entities.notes).onFailure {
noteDataSource.addAll(noteDTOEntityConverter.convertAll(
account,
entities.notes
)).onFailure {
logger.error("NoteDataSourceへの追加に失敗", it)
}
filePropertyDataSource.addAll(entities.files).onFailure {
logger.error("FilePropertyDataSourceへの追加に失敗", it)
}
}

return entities.note
return willReturnNote
}

suspend fun addTootStatusDtoIntoDataSource(account: Account, status: TootStatusDTO, skipExists: Boolean = false): Note {
suspend fun addTootStatusDtoIntoDataSource(
account: Account,
status: TootStatusDTO,
skipExists: Boolean = false
): Note {
val entities = status.toEntities(tootDTOEntityConverter, account)
if (skipExists) {
userDataSource.addAll(
Expand Down Expand Up @@ -145,10 +236,9 @@ class NoteDataSourceAdder @Inject constructor(
suspend fun NoteDTO.toEntities(
account: Account,
userDTOEntityConverter: UserDTOEntityConverter,
noteDTOEntityConverter: NoteDTOEntityConverter,
filePropertyDTOEntityConverter: FilePropertyDTOEntityConverter,
instanceType: InstanceInfoType?,
): NoteRelationEntities {
): NoteDTOUnpacked {
val dtoList = mutableListOf<NoteDTO>()
dtoList.add(this)

Expand All @@ -161,9 +251,8 @@ suspend fun NoteDTO.toEntities(
dtoList.add(reNote!!)
}

val note = noteDTOEntityConverter.convert(account, this, instanceType)
val users = mutableListOf<User>()
val notes = mutableListOf<Note>()
val notes = mutableListOf<NoteDTO>()
val files = mutableListOf<FileProperty>()

pickEntities(
Expand All @@ -172,12 +261,11 @@ suspend fun NoteDTO.toEntities(
users,
files,
userDTOEntityConverter,
noteDTOEntityConverter,
filePropertyDTOEntityConverter,
instanceType,
)
return NoteRelationEntities(
note = note,
return NoteDTOUnpacked(
note = this,
notes = notes,
users = users,
files = files
Expand All @@ -186,19 +274,16 @@ suspend fun NoteDTO.toEntities(

private suspend fun NoteDTO.pickEntities(
account: Account,
notes: MutableList<Note>,
notes: MutableList<NoteDTO>,
users: MutableList<User>,
files: MutableList<FileProperty>,
userDTOEntityConverter: UserDTOEntityConverter,
noteDTOEntityConverter: NoteDTOEntityConverter,
filePropertyDTOEntityConverter: FilePropertyDTOEntityConverter,
instanceType: InstanceInfoType?,
) {
) {
val (note, user) = this.toNoteAndUser(
account,
userDTOEntityConverter,
noteDTOEntityConverter,
instanceType,
)
notes.add(note)
users.add(user)
Expand All @@ -214,7 +299,6 @@ private suspend fun NoteDTO.pickEntities(
users,
files,
userDTOEntityConverter,
noteDTOEntityConverter,
filePropertyDTOEntityConverter,
instanceType,
)
Expand All @@ -227,7 +311,6 @@ private suspend fun NoteDTO.pickEntities(
users,
files,
userDTOEntityConverter,
noteDTOEntityConverter,
filePropertyDTOEntityConverter,
instanceType,
)
Expand All @@ -237,11 +320,8 @@ private suspend fun NoteDTO.pickEntities(
suspend fun NoteDTO.toNoteAndUser(
account: Account,
userDTOEntityConverter: UserDTOEntityConverter,
noteDTOEntityConverter: NoteDTOEntityConverter,
instanceType: InstanceInfoType?,
): Pair<Note, User> {
val note = noteDTOEntityConverter.convert(account, this, instanceType)
): Pair<NoteDTO, User> {
val user = userDTOEntityConverter.convert(account, user, false)
return note to user
return this to user
}

Loading