-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix(server): DriveFile related N+1 query when call note packMany #10133
Merged
+66
−58
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14a77e5
fix(server): DriveFile related N+1 query when call note packMany
rinsuki 2f529c1
Update packages/backend/src/misc/is-not-null.ts
rinsuki 6bc08bd
ignore lint
rinsuki c713c91
途中でやめたやつが混入していた
rinsuki 84899eb
fix: 順番通りである必要がある場所で順番通りになっていなかった
rinsuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ import type { Note } from '@/models/entities/Note.js'; | |
import type { NoteReaction } from '@/models/entities/NoteReaction.js'; | ||
import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository, DriveFilesRepository } from '@/models/index.js'; | ||
import { bindThis } from '@/decorators.js'; | ||
import { isNotNull } from '@/misc/is-not-null.js'; | ||
import type { OnModuleInit } from '@nestjs/common'; | ||
import type { CustomEmojiService } from '../CustomEmojiService.js'; | ||
import type { ReactionService } from '../ReactionService.js'; | ||
|
@@ -257,6 +258,7 @@ export class NoteEntityService implements OnModuleInit { | |
skipHide?: boolean; | ||
_hint_?: { | ||
myReactions: Map<Note['id'], NoteReaction | null>; | ||
packedFiles: Map<Note['fileIds'][number], Packed<'DriveFile'>>; | ||
}; | ||
}, | ||
): Promise<Packed<'Note'>> { | ||
|
@@ -284,6 +286,7 @@ export class NoteEntityService implements OnModuleInit { | |
const reactionEmojiNames = Object.keys(note.reactions) | ||
.filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ | ||
.map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', '')); | ||
const packedFiles = options?._hint_?.packedFiles; | ||
|
||
const packed: Packed<'Note'> = await awaitAll({ | ||
id: note.id, | ||
|
@@ -304,7 +307,7 @@ export class NoteEntityService implements OnModuleInit { | |
emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined, | ||
tags: note.tags.length > 0 ? note.tags : undefined, | ||
fileIds: note.fileIds, | ||
files: this.driveFileEntityService.packMany(note.fileIds), | ||
files: packedFiles != null ? note.fileIds.map(fi => packedFiles.get(fi)).filter(isNotNull) : this.driveFileEntityService.packManyByIds(note.fileIds), | ||
replyId: note.replyId, | ||
renoteId: note.renoteId, | ||
channelId: note.channelId ?? undefined, | ||
|
@@ -388,11 +391,14 @@ export class NoteEntityService implements OnModuleInit { | |
} | ||
|
||
await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes)); | ||
const fileIds = notes.flatMap(n => n.fileIds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 多分ここでrenote(あとreply?)も考慮する必要がある There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. もしくは、_hint_に希望するファイルが含まれてなかったらスキップするのではなくて諦めてDBにfindしてくるようにするとか |
||
const packedFiles = new Map(fileIds.length === 0 ? [] : (await this.driveFileEntityService.packManyByIds(fileIds)).map(r => [r.id, r])); | ||
|
||
return await Promise.all(notes.map(n => this.pack(n, me, { | ||
...options, | ||
_hint_: { | ||
myReactions: myReactionsMap, | ||
packedFiles, | ||
}, | ||
}))); | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// we are using {} as "any non-nullish value" as expected | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export function isNotNull<T extends {}>(input: T | undefined | null): input is T { | ||
return input != null; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここって
fileIds
の順序にソートしなくても良い?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ〜〜、確かにした方がいいな