Migrate Channel.GetByNames to sync by default (#11202)

Этот коммит содержится в:
Rodrigo Villablanca Vásquez
2019-06-17 11:03:04 -04:00
коммит произвёл Christopher Speller
родитель 27a88b7868
Коммит 4d223ba3a2
5 изменённых файлов: 76 добавлений и 71 удалений

Просмотреть файл

@@ -1156,63 +1156,60 @@ func (s SqlChannelStore) GetByName(teamId string, name string, allowFromCache bo
return s.getByName(teamId, name, false, allowFromCache)
}
func (s SqlChannelStore) GetByNames(teamId string, names []string, allowFromCache bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var channels []*model.Channel
func (s SqlChannelStore) GetByNames(teamId string, names []string, allowFromCache bool) ([]*model.Channel, *model.AppError) {
var channels []*model.Channel
if allowFromCache {
var misses []string
visited := make(map[string]struct{})
for _, name := range names {
if _, ok := visited[name]; ok {
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)
}
if allowFromCache {
var misses []string
visited := make(map[string]struct{})
for _, name := range names {
if _, ok := visited[name]; ok {
continue
}
names = misses
}
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`
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 {
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 {
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)
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter("Channel By Name")
}
misses = append(misses, name)
}
}
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 {