From 98e685f07cc49182b1450170975684bda14e32de Mon Sep 17 00:00:00 2001 From: Shota Gvinepadze Date: Wed, 16 Nov 2022 13:42:26 +0400 Subject: [PATCH] Move Thread.GetPosts to PostStore.GetPostsByThread (#21633) Co-authored-by: Mattermod --- app/post.go | 2 +- store/opentracinglayer/opentracinglayer.go | 36 +++++++++---------- store/retrylayer/retrylayer.go | 42 +++++++++++----------- store/sqlstore/post_store.go | 17 +++++++++ store/sqlstore/thread_store.go | 17 --------- store/store.go | 2 +- store/storetest/mocks/PostStore.go | 23 ++++++++++++ store/storetest/mocks/ThreadStore.go | 23 ------------ store/timerlayer/timerlayer.go | 32 ++++++++--------- 9 files changed, 97 insertions(+), 97 deletions(-) diff --git a/app/post.go b/app/post.go index ff6cab3397..78d14b16ef 100644 --- a/app/post.go +++ b/app/post.go @@ -1685,7 +1685,7 @@ func (a *App) countThreadMentions(c request.CTX, user *model.User, post *model.P true, // Assume channel mentions are always allowed for simplicity ) - posts, nErr := a.Srv().Store().Thread().GetPosts(post.Id, timestamp) + posts, nErr := a.Srv().Store().Post().GetPostsByThread(post.Id, timestamp) if nErr != nil { return 0, model.NewAppError("countMentionsFromPost", "app.channel.count_posts_since.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr) } diff --git a/store/opentracinglayer/opentracinglayer.go b/store/opentracinglayer/opentracinglayer.go index 614a3933fe..b500b0cb53 100644 --- a/store/opentracinglayer/opentracinglayer.go +++ b/store/opentracinglayer/opentracinglayer.go @@ -6089,6 +6089,24 @@ func (s *OpenTracingLayerPostStore) GetPostsByIds(postIds []string) ([]*model.Po return result, err } +func (s *OpenTracingLayerPostStore) GetPostsByThread(threadID string, since int64) ([]*model.Post, error) { + origCtx := s.Root.Store.Context() + span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "PostStore.GetPostsByThread") + s.Root.Store.SetContext(newCtx) + defer func() { + s.Root.Store.SetContext(origCtx) + }() + + defer span.Finish() + result, err := s.PostStore.GetPostsByThread(threadID, since) + if err != nil { + span.LogFields(spanlog.Error(err)) + ext.Error.Set(span, true) + } + + return result, err +} + func (s *OpenTracingLayerPostStore) GetPostsCreatedAt(channelID string, timestamp int64) ([]*model.Post, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "PostStore.GetPostsCreatedAt") @@ -9836,24 +9854,6 @@ func (s *OpenTracingLayerThreadStore) GetMembershipsForUser(userId string, teamI return result, err } -func (s *OpenTracingLayerThreadStore) GetPosts(threadID string, since int64) ([]*model.Post, error) { - origCtx := s.Root.Store.Context() - span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ThreadStore.GetPosts") - s.Root.Store.SetContext(newCtx) - defer func() { - s.Root.Store.SetContext(origCtx) - }() - - defer span.Finish() - result, err := s.ThreadStore.GetPosts(threadID, since) - if err != nil { - span.LogFields(spanlog.Error(err)) - ext.Error.Set(span, true) - } - - return result, err -} - func (s *OpenTracingLayerThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) (map[string]*model.TeamUnread, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ThreadStore.GetTeamsUnreadForUser") diff --git a/store/retrylayer/retrylayer.go b/store/retrylayer/retrylayer.go index 03043d870e..3e6fcb0b31 100644 --- a/store/retrylayer/retrylayer.go +++ b/store/retrylayer/retrylayer.go @@ -6900,6 +6900,27 @@ func (s *RetryLayerPostStore) GetPostsByIds(postIds []string) ([]*model.Post, er } +func (s *RetryLayerPostStore) GetPostsByThread(threadID string, since int64) ([]*model.Post, error) { + + tries := 0 + for { + result, err := s.PostStore.GetPostsByThread(threadID, since) + if err == nil { + return result, nil + } + if !isRepeatableError(err) { + return result, err + } + tries++ + if tries >= 3 { + err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures") + return result, err + } + timepkg.Sleep(100 * timepkg.Millisecond) + } + +} + func (s *RetryLayerPostStore) GetPostsCreatedAt(channelID string, timestamp int64) ([]*model.Post, error) { tries := 0 @@ -11244,27 +11265,6 @@ func (s *RetryLayerThreadStore) GetMembershipsForUser(userId string, teamID stri } -func (s *RetryLayerThreadStore) GetPosts(threadID string, since int64) ([]*model.Post, error) { - - tries := 0 - for { - result, err := s.ThreadStore.GetPosts(threadID, since) - if err == nil { - return result, nil - } - if !isRepeatableError(err) { - return result, err - } - tries++ - if tries >= 3 { - err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures") - return result, err - } - timepkg.Sleep(100 * timepkg.Millisecond) - } - -} - func (s *RetryLayerThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) (map[string]*model.TeamUnread, error) { tries := 0 diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index a11a3ee347..127e566861 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -1400,6 +1400,23 @@ func (s *SqlPostStore) GetPostsAfter(options model.GetPostsOptions, sanitizeOpti return s.getPostsAround(false, options, sanitizeOptions) } +func (s *SqlPostStore) GetPostsByThread(threadId string, since int64) ([]*model.Post, error) { + query := s.getQueryBuilder(). + Select("*"). + From("Posts"). + Where(sq.Eq{"RootId": threadId}). + Where(sq.Eq{"DeleteAt": 0}). + Where(sq.GtOrEq{"CreateAt": since}) + + result := []*model.Post{} + err := s.GetReplicaX().SelectBuilder(&result, query) + if err != nil { + return nil, errors.Wrap(err, "failed to fetch thread posts") + } + + return result, nil +} + func (s *SqlPostStore) getPostsAround(before bool, options model.GetPostsOptions, sanitizeOptions map[string]bool) (*model.PostList, error) { if options.Page < 0 { return nil, store.NewErrInvalidInput("Post", "", options.Page) diff --git a/store/sqlstore/thread_store.go b/store/sqlstore/thread_store.go index 502be5e354..e314e82093 100644 --- a/store/sqlstore/thread_store.go +++ b/store/sqlstore/thread_store.go @@ -779,23 +779,6 @@ func (s *SqlThreadStore) MaintainMembership(userId, postId string, opts store.Th return membership, err } -func (s *SqlThreadStore) GetPosts(threadId string, since int64) ([]*model.Post, error) { - query := s.getQueryBuilder(). - Select("*"). - From("Posts"). - Where(sq.Eq{"RootId": threadId}). - Where(sq.Eq{"DeleteAt": 0}). - Where(sq.GtOrEq{"CreateAt": since}) - - result := []*model.Post{} - err := s.GetReplicaX().SelectBuilder(&result, query) - if err != nil { - return nil, errors.Wrap(err, "failed to fetch thread posts") - } - - return result, nil -} - // PermanentDeleteBatchForRetentionPolicies deletes a batch of records which are affected by // the global or a granular retention policy. // See `genericPermanentDeleteBatchForRetentionPolicies` for details. diff --git a/store/store.go b/store/store.go index 805c972e27..77092a2aed 100644 --- a/store/store.go +++ b/store/store.go @@ -325,7 +325,6 @@ type ThreadStore interface { GetThreadsForUser(userId, teamID string, opts model.GetUserThreadsOpts) ([]*model.ThreadResponse, error) GetThreadForUser(threadMembership *model.ThreadMembership, extended bool) (*model.ThreadResponse, error) GetTeamsUnreadForUser(userID string, teamIDs []string) (map[string]*model.TeamUnread, error) - GetPosts(threadID string, since int64) ([]*model.Post, error) MarkAllAsRead(userID string, threadIds []string) error MarkAllAsReadByTeam(userID, teamID string) error @@ -364,6 +363,7 @@ type PostStore interface { GetPostsBefore(options model.GetPostsOptions, sanitizeOptions map[string]bool) (*model.PostList, error) GetPostsAfter(options model.GetPostsOptions, sanitizeOptions map[string]bool) (*model.PostList, error) GetPostsSince(options model.GetPostsSinceOptions, allowFromCache bool, sanitizeOptions map[string]bool) (*model.PostList, error) + GetPostsByThread(threadID string, since int64) ([]*model.Post, error) GetPostAfterTime(channelID string, timestamp int64, collapsedThreads bool) (*model.Post, error) GetPostIdAfterTime(channelID string, timestamp int64, collapsedThreads bool) (string, error) GetPostIdBeforeTime(channelID string, timestamp int64, collapsedThreads bool) (string, error) diff --git a/store/storetest/mocks/PostStore.go b/store/storetest/mocks/PostStore.go index ef455c82b3..91e39efb45 100644 --- a/store/storetest/mocks/PostStore.go +++ b/store/storetest/mocks/PostStore.go @@ -603,6 +603,29 @@ func (_m *PostStore) GetPostsByIds(postIds []string) ([]*model.Post, error) { return r0, r1 } +// GetPostsByThread provides a mock function with given fields: threadID, since +func (_m *PostStore) GetPostsByThread(threadID string, since int64) ([]*model.Post, error) { + ret := _m.Called(threadID, since) + + var r0 []*model.Post + if rf, ok := ret.Get(0).(func(string, int64) []*model.Post); ok { + r0 = rf(threadID, since) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*model.Post) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string, int64) error); ok { + r1 = rf(threadID, since) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetPostsCreatedAt provides a mock function with given fields: channelID, timestamp func (_m *PostStore) GetPostsCreatedAt(channelID string, timestamp int64) ([]*model.Post, error) { ret := _m.Called(channelID, timestamp) diff --git a/store/storetest/mocks/ThreadStore.go b/store/storetest/mocks/ThreadStore.go index bfacea3709..19d82db686 100644 --- a/store/storetest/mocks/ThreadStore.go +++ b/store/storetest/mocks/ThreadStore.go @@ -119,29 +119,6 @@ func (_m *ThreadStore) GetMembershipsForUser(userId string, teamID string) ([]*m return r0, r1 } -// GetPosts provides a mock function with given fields: threadID, since -func (_m *ThreadStore) GetPosts(threadID string, since int64) ([]*model.Post, error) { - ret := _m.Called(threadID, since) - - var r0 []*model.Post - if rf, ok := ret.Get(0).(func(string, int64) []*model.Post); ok { - r0 = rf(threadID, since) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]*model.Post) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(string, int64) error); ok { - r1 = rf(threadID, since) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // GetTeamsUnreadForUser provides a mock function with given fields: userID, teamIDs func (_m *ThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) (map[string]*model.TeamUnread, error) { ret := _m.Called(userID, teamIDs) diff --git a/store/timerlayer/timerlayer.go b/store/timerlayer/timerlayer.go index deb0dbc224..16f6044d24 100644 --- a/store/timerlayer/timerlayer.go +++ b/store/timerlayer/timerlayer.go @@ -5508,6 +5508,22 @@ func (s *TimerLayerPostStore) GetPostsByIds(postIds []string) ([]*model.Post, er return result, err } +func (s *TimerLayerPostStore) GetPostsByThread(threadID string, since int64) ([]*model.Post, error) { + start := time.Now() + + result, err := s.PostStore.GetPostsByThread(threadID, since) + + elapsed := float64(time.Since(start)) / float64(time.Second) + if s.Root.Metrics != nil { + success := "false" + if err == nil { + success = "true" + } + s.Root.Metrics.ObserveStoreMethodDuration("PostStore.GetPostsByThread", success, elapsed) + } + return result, err +} + func (s *TimerLayerPostStore) GetPostsCreatedAt(channelID string, timestamp int64) ([]*model.Post, error) { start := time.Now() @@ -8849,22 +8865,6 @@ func (s *TimerLayerThreadStore) GetMembershipsForUser(userId string, teamID stri return result, err } -func (s *TimerLayerThreadStore) GetPosts(threadID string, since int64) ([]*model.Post, error) { - start := time.Now() - - result, err := s.ThreadStore.GetPosts(threadID, since) - - elapsed := float64(time.Since(start)) / float64(time.Second) - if s.Root.Metrics != nil { - success := "false" - if err == nil { - success = "true" - } - s.Root.Metrics.ObserveStoreMethodDuration("ThreadStore.GetPosts", success, elapsed) - } - return result, err -} - func (s *TimerLayerThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) (map[string]*model.TeamUnread, error) { start := time.Now()