summaryrefslogtreecommitdiffstats
path: root/store/sql_channel_store.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2016-12-23 10:32:30 -0500
committerGitHub <noreply@github.com>2016-12-23 10:32:30 -0500
commit217cdf447a995fd8f2700b14ba790360ccaeabf6 (patch)
treeca0b01c4cd869fbadd1d5a831b5775ca9dc066dc /store/sql_channel_store.go
parentffd6ccde2ed4b1d374d0835c5638a189df60a679 (diff)
downloadchat-217cdf447a995fd8f2700b14ba790360ccaeabf6.tar.gz
chat-217cdf447a995fd8f2700b14ba790360ccaeabf6.tar.bz2
chat-217cdf447a995fd8f2700b14ba790360ccaeabf6.zip
PLT-5073 Improve performance of /channels/view endpoint (#4881)
* Improve performance of /channels/view endpoint * Fix store unit test
Diffstat (limited to 'store/sql_channel_store.go')
-rw-r--r--store/sql_channel_store.go25
1 files changed, 19 insertions, 6 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go
index a961781dd..47c172f77 100644
--- a/store/sql_channel_store.go
+++ b/store/sql_channel_store.go
@@ -6,6 +6,7 @@ package store
import (
"database/sql"
"fmt"
+ "strconv"
"strings"
l4g "github.com/alecthomas/log4go"
@@ -13,7 +14,6 @@ import (
"github.com/mattermost/platform/einterfaces"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
- "strconv"
)
const (
@@ -961,13 +961,24 @@ func (s SqlChannelStore) SetLastViewedAt(channelId string, userId string, newLas
return storeChannel
}
-func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) StoreChannel {
+func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
var query string
+ props := make(map[string]interface{})
+
+ idQuery := ""
+ for index, channelId := range channelIds {
+ if len(idQuery) > 0 {
+ idQuery += " OR "
+ }
+
+ props["channelId"+strconv.Itoa(index)] = channelId
+ idQuery += "ChannelId = :channelId" + strconv.Itoa(index)
+ }
if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES {
query = `UPDATE
@@ -982,7 +993,7 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto
WHERE
Channels.Id = ChannelMembers.ChannelId
AND UserId = :UserId
- AND ChannelId = :ChannelId`
+ AND (` + idQuery + `)`
} else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL {
query = `UPDATE
ChannelMembers, Channels
@@ -994,12 +1005,14 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto
WHERE
Channels.Id = ChannelMembers.ChannelId
AND UserId = :UserId
- AND ChannelId = :ChannelId`
+ AND (` + idQuery + `)`
}
- _, err := s.GetMaster().Exec(query, map[string]interface{}{"ChannelId": channelId, "UserId": userId})
+ props["UserId"] = userId
+
+ _, err := s.GetMaster().Exec(query, props)
if err != nil {
- result.Err = model.NewLocAppError("SqlChannelStore.UpdateLastViewedAt", "store.sql_channel.update_last_viewed_at.app_error", nil, "channel_id="+channelId+", user_id="+userId+", "+err.Error())
+ result.Err = model.NewLocAppError("SqlChannelStore.UpdateLastViewedAt", "store.sql_channel.update_last_viewed_at.app_error", nil, "channel_ids="+strings.Join(channelIds, ",")+", user_id="+userId+", "+err.Error())
}
storeChannel <- result