summaryrefslogtreecommitdiffstats
path: root/models/issue_comment_list.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2018-12-13 23:55:43 +0800
committertechknowlogick <hello@techknowlogick.com>2018-12-13 10:55:43 -0500
commitb3b7598ec6846d53d7deb2c84781b06e22c93044 (patch)
tree53c85943b23867b20e2b31742fbe06fcda088ccd /models/issue_comment_list.go
parent49ea6e0deb7ecef327b0c2f41920f75c6aaf80d3 (diff)
downloadgitea-b3b7598ec6846d53d7deb2c84781b06e22c93044.tar.gz
gitea-b3b7598ec6846d53d7deb2c84781b06e22c93044.zip
Improve performance of dashboard (#4977)
Diffstat (limited to 'models/issue_comment_list.go')
-rw-r--r--models/issue_comment_list.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/models/issue_comment_list.go b/models/issue_comment_list.go
new file mode 100644
index 0000000000..35c1253f6b
--- /dev/null
+++ b/models/issue_comment_list.go
@@ -0,0 +1,58 @@
+// Copyright 2018 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package models
+
+// CommentList defines a list of comments
+type CommentList []*Comment
+
+func (comments CommentList) getPosterIDs() []int64 {
+ commentIDs := make(map[int64]struct{}, len(comments))
+ for _, comment := range comments {
+ if _, ok := commentIDs[comment.PosterID]; !ok {
+ commentIDs[comment.PosterID] = struct{}{}
+ }
+ }
+ return keysInt64(commentIDs)
+}
+
+// LoadPosters loads posters from database
+func (comments CommentList) LoadPosters() error {
+ return comments.loadPosters(x)
+}
+
+func (comments CommentList) loadPosters(e Engine) error {
+ if len(comments) == 0 {
+ return nil
+ }
+
+ posterIDs := comments.getPosterIDs()
+ posterMaps := make(map[int64]*User, len(posterIDs))
+ var left = len(posterIDs)
+ for left > 0 {
+ var limit = defaultMaxInSize
+ if left < limit {
+ limit = left
+ }
+ err := e.
+ In("id", posterIDs[:limit]).
+ Find(&posterMaps)
+ if err != nil {
+ return err
+ }
+ left = left - limit
+ posterIDs = posterIDs[limit:]
+ }
+
+ for _, comment := range comments {
+ if comment.PosterID <= 0 {
+ continue
+ }
+ var ok bool
+ if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
+ comment.Poster = NewGhostUser()
+ }
+ }
+ return nil
+}