Migrate Channel.GetByNames to sync by default (#11202)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
27a88b7868
Коммит
4d223ba3a2
@@ -1190,16 +1190,16 @@ func (a *App) GetChannelByName(channelName, teamId string, includeDeleted bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model.Channel, *model.AppError) {
|
func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model.Channel, *model.AppError) {
|
||||||
result := <-a.Srv.Store.Channel().GetByNames(teamId, channelNames, true)
|
channels, err := a.Srv.Store.Channel().GetByNames(teamId, channelNames, true)
|
||||||
if result.Err != nil {
|
if err != nil {
|
||||||
if result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
if err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||||
result.Err.StatusCode = http.StatusNotFound
|
err.StatusCode = http.StatusNotFound
|
||||||
return nil, result.Err
|
return nil, err
|
||||||
}
|
}
|
||||||
result.Err.StatusCode = http.StatusBadRequest
|
err.StatusCode = http.StatusBadRequest
|
||||||
return nil, result.Err
|
return nil, err
|
||||||
}
|
}
|
||||||
return result.Data.([]*model.Channel), nil
|
return channels, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeDeleted bool) (*model.Channel, *model.AppError) {
|
func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeDeleted bool) (*model.Channel, *model.AppError) {
|
||||||
|
|||||||
@@ -1156,63 +1156,60 @@ func (s SqlChannelStore) GetByName(teamId string, name string, allowFromCache bo
|
|||||||
return s.getByName(teamId, name, false, allowFromCache)
|
return s.getByName(teamId, name, false, allowFromCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) GetByNames(teamId string, names []string, allowFromCache bool) store.StoreChannel {
|
func (s SqlChannelStore) GetByNames(teamId string, names []string, allowFromCache bool) ([]*model.Channel, *model.AppError) {
|
||||||
return store.Do(func(result *store.StoreResult) {
|
var channels []*model.Channel
|
||||||
var channels []*model.Channel
|
|
||||||
|
|
||||||
if allowFromCache {
|
if allowFromCache {
|
||||||
var misses []string
|
var misses []string
|
||||||
visited := make(map[string]struct{})
|
visited := make(map[string]struct{})
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
if _, ok := visited[name]; ok {
|
if _, ok := visited[name]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
visited[name] = struct{}{}
|
|
||||||
if cacheItem, ok := channelByNameCache.Get(teamId + name); ok {
|
|
||||||
if s.metrics != nil {
|
|
||||||
s.metrics.IncrementMemCacheHitCounter("Channel By Name")
|
|
||||||
}
|
|
||||||
channels = append(channels, cacheItem.(*model.Channel))
|
|
||||||
} else {
|
|
||||||
if s.metrics != nil {
|
|
||||||
s.metrics.IncrementMemCacheMissCounter("Channel By Name")
|
|
||||||
}
|
|
||||||
misses = append(misses, name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
names = misses
|
visited[name] = struct{}{}
|
||||||
}
|
if cacheItem, ok := channelByNameCache.Get(teamId + name); ok {
|
||||||
|
if s.metrics != nil {
|
||||||
if len(names) > 0 {
|
s.metrics.IncrementMemCacheHitCounter("Channel By Name")
|
||||||
props := map[string]interface{}{}
|
}
|
||||||
var namePlaceholders []string
|
channels = append(channels, cacheItem.(*model.Channel))
|
||||||
for _, name := range names {
|
|
||||||
key := fmt.Sprintf("Name%v", len(namePlaceholders))
|
|
||||||
props[key] = name
|
|
||||||
namePlaceholders = append(namePlaceholders, ":"+key)
|
|
||||||
}
|
|
||||||
|
|
||||||
var query string
|
|
||||||
if teamId == "" {
|
|
||||||
query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND DeleteAt = 0`
|
|
||||||
} else {
|
} else {
|
||||||
props["TeamId"] = teamId
|
if s.metrics != nil {
|
||||||
query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND TeamId = :TeamId AND DeleteAt = 0`
|
s.metrics.IncrementMemCacheMissCounter("Channel By Name")
|
||||||
}
|
}
|
||||||
|
misses = append(misses, name)
|
||||||
var dbChannels []*model.Channel
|
|
||||||
if _, err := s.GetReplica().Select(&dbChannels, query, props); err != nil && err != sql.ErrNoRows {
|
|
||||||
result.Err = model.NewAppError("SqlChannelStore.GetByName", "store.sql_channel.get_by_name.existing.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, channel := range dbChannels {
|
|
||||||
channelByNameCache.AddWithExpiresInSecs(teamId+channel.Name, channel, CHANNEL_CACHE_SEC)
|
|
||||||
channels = append(channels, channel)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
names = misses
|
||||||
|
}
|
||||||
|
|
||||||
result.Data = channels
|
if len(names) > 0 {
|
||||||
})
|
props := map[string]interface{}{}
|
||||||
|
var namePlaceholders []string
|
||||||
|
for _, name := range names {
|
||||||
|
key := fmt.Sprintf("Name%v", len(namePlaceholders))
|
||||||
|
props[key] = name
|
||||||
|
namePlaceholders = append(namePlaceholders, ":"+key)
|
||||||
|
}
|
||||||
|
|
||||||
|
var query string
|
||||||
|
if teamId == "" {
|
||||||
|
query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND DeleteAt = 0`
|
||||||
|
} else {
|
||||||
|
props["TeamId"] = teamId
|
||||||
|
query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND TeamId = :TeamId AND DeleteAt = 0`
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbChannels []*model.Channel
|
||||||
|
if _, err := s.GetReplica().Select(&dbChannels, query, props); err != nil && err != sql.ErrNoRows {
|
||||||
|
return nil, model.NewAppError("SqlChannelStore.GetByName", "store.sql_channel.get_by_name.existing.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
for _, channel := range dbChannels {
|
||||||
|
channelByNameCache.AddWithExpiresInSecs(teamId+channel.Name, channel, CHANNEL_CACHE_SEC)
|
||||||
|
channels = append(channels, channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return channels, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string, allowFromCache bool) store.StoreChannel {
|
func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string, allowFromCache bool) store.StoreChannel {
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ type ChannelStore interface {
|
|||||||
PermanentDeleteByTeam(teamId string) StoreChannel
|
PermanentDeleteByTeam(teamId string) StoreChannel
|
||||||
PermanentDelete(channelId string) StoreChannel
|
PermanentDelete(channelId string) StoreChannel
|
||||||
GetByName(team_id string, name string, allowFromCache bool) StoreChannel
|
GetByName(team_id string, name string, allowFromCache bool) StoreChannel
|
||||||
GetByNames(team_id string, names []string, allowFromCache bool) StoreChannel
|
GetByNames(team_id string, names []string, allowFromCache bool) ([]*model.Channel, *model.AppError)
|
||||||
GetByNameIncludeDeleted(team_id string, name string, allowFromCache bool) StoreChannel
|
GetByNameIncludeDeleted(team_id string, name string, allowFromCache bool) StoreChannel
|
||||||
GetDeletedByName(team_id string, name string) StoreChannel
|
GetDeletedByName(team_id string, name string) StoreChannel
|
||||||
GetDeleted(team_id string, offset int, limit int) StoreChannel
|
GetDeleted(team_id string, offset int, limit int) StoreChannel
|
||||||
|
|||||||
@@ -716,9 +716,9 @@ func testChannelStoreGetByNames(t *testing.T, ss store.Store) {
|
|||||||
{"", []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}},
|
{"", []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}},
|
||||||
{"asd", []string{o1.Name, "foo", o2.Name, o2.Name}, nil},
|
{"asd", []string{o1.Name, "foo", o2.Name, o2.Name}, nil},
|
||||||
} {
|
} {
|
||||||
r := <-ss.Channel().GetByNames(tc.TeamId, tc.Names, true)
|
var channels []*model.Channel
|
||||||
require.Nil(t, r.Err)
|
channels, err = ss.Channel().GetByNames(tc.TeamId, tc.Names, true)
|
||||||
channels := r.Data.([]*model.Channel)
|
require.Nil(t, err)
|
||||||
var ids []string
|
var ids []string
|
||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
ids = append(ids, channel.Id)
|
ids = append(ids, channel.Id)
|
||||||
@@ -734,9 +734,8 @@ func testChannelStoreGetByNames(t *testing.T, ss store.Store) {
|
|||||||
err = ss.Channel().Delete(o2.Id, model.GetMillis())
|
err = ss.Channel().Delete(o2.Id, model.GetMillis())
|
||||||
require.Nil(t, err, "channel should have been deleted")
|
require.Nil(t, err, "channel should have been deleted")
|
||||||
|
|
||||||
r := <-ss.Channel().GetByNames(o1.TeamId, []string{o1.Name}, false)
|
channels, err := ss.Channel().GetByNames(o1.TeamId, []string{o1.Name}, false)
|
||||||
require.Nil(t, r.Err)
|
require.Nil(t, err)
|
||||||
channels := r.Data.([]*model.Channel)
|
|
||||||
assert.Len(t, channels, 0)
|
assert.Len(t, channels, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -300,19 +300,28 @@ func (_m *ChannelStore) GetByNameIncludeDeleted(team_id string, name string, all
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByNames provides a mock function with given fields: team_id, names, allowFromCache
|
// GetByNames provides a mock function with given fields: team_id, names, allowFromCache
|
||||||
func (_m *ChannelStore) GetByNames(team_id string, names []string, allowFromCache bool) store.StoreChannel {
|
func (_m *ChannelStore) GetByNames(team_id string, names []string, allowFromCache bool) ([]*model.Channel, *model.AppError) {
|
||||||
ret := _m.Called(team_id, names, allowFromCache)
|
ret := _m.Called(team_id, names, allowFromCache)
|
||||||
|
|
||||||
var r0 store.StoreChannel
|
var r0 []*model.Channel
|
||||||
if rf, ok := ret.Get(0).(func(string, []string, bool) store.StoreChannel); ok {
|
if rf, ok := ret.Get(0).(func(string, []string, bool) []*model.Channel); ok {
|
||||||
r0 = rf(team_id, names, allowFromCache)
|
r0 = rf(team_id, names, allowFromCache)
|
||||||
} else {
|
} else {
|
||||||
if ret.Get(0) != nil {
|
if ret.Get(0) != nil {
|
||||||
r0 = ret.Get(0).(store.StoreChannel)
|
r0 = ret.Get(0).([]*model.Channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return r0
|
var r1 *model.AppError
|
||||||
|
if rf, ok := ret.Get(1).(func(string, []string, bool) *model.AppError); ok {
|
||||||
|
r1 = rf(team_id, names, allowFromCache)
|
||||||
|
} else {
|
||||||
|
if ret.Get(1) != nil {
|
||||||
|
r1 = ret.Get(1).(*model.AppError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChannelCounts provides a mock function with given fields: teamId, userId
|
// GetChannelCounts provides a mock function with given fields: teamId, userId
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user