1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"net/http"
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
)
const (
EMOJI_CACHE_SIZE = 5000
EMOJI_CACHE_SEC = 1800 // 30 mins
)
var emojiCache *utils.Cache = utils.NewLru(EMOJI_CACHE_SIZE)
type SqlEmojiStore struct {
SqlStore
metrics einterfaces.MetricsInterface
}
func NewSqlEmojiStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.EmojiStore {
s := &SqlEmojiStore{
SqlStore: sqlStore,
metrics: metrics,
}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.Emoji{}, "Emoji").SetKeys(false, "Id")
table.ColMap("Id").SetMaxSize(26)
table.ColMap("CreatorId").SetMaxSize(26)
table.ColMap("Name").SetMaxSize(64)
table.SetUniqueTogether("Name", "DeleteAt")
}
return s
}
func (es SqlEmojiStore) CreateIndexesIfNotExists() {
es.CreateIndexIfNotExists("idx_emoji_update_at", "Emoji", "UpdateAt")
es.CreateIndexIfNotExists("idx_emoji_create_at", "Emoji", "CreateAt")
es.CreateIndexIfNotExists("idx_emoji_delete_at", "Emoji", "DeleteAt")
}
func (es SqlEmojiStore) Save(emoji *model.Emoji) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
emoji.PreSave()
if result.Err = emoji.IsValid(); result.Err != nil {
return
}
if err := es.GetMaster().Insert(emoji); err != nil {
result.Err = model.NewAppError("SqlEmojiStore.Save", "store.sql_emoji.save.app_error", nil, "id="+emoji.Id+", "+err.Error(), http.StatusInternalServerError)
} else {
result.Data = emoji
}
})
}
func (es SqlEmojiStore) Get(id string, allowFromCache bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if allowFromCache {
if cacheItem, ok := emojiCache.Get(id); ok {
if es.metrics != nil {
es.metrics.IncrementMemCacheHitCounter("Emoji")
}
result.Data = cacheItem.(*model.Emoji)
return
} else {
if es.metrics != nil {
es.metrics.IncrementMemCacheMissCounter("Emoji")
}
}
} else {
if es.metrics != nil {
es.metrics.IncrementMemCacheMissCounter("Emoji")
}
}
var emoji *model.Emoji
if err := es.GetReplica().SelectOne(&emoji,
`SELECT
*
FROM
Emoji
WHERE
Id = :Id
AND DeleteAt = 0`, map[string]interface{}{"Id": id}); err != nil {
result.Err = model.NewAppError("SqlEmojiStore.Get", "store.sql_emoji.get.app_error", nil, "id="+id+", "+err.Error(), http.StatusNotFound)
} else {
result.Data = emoji
if allowFromCache {
emojiCache.AddWithExpiresInSecs(id, emoji, EMOJI_CACHE_SEC)
}
}
})
}
func (es SqlEmojiStore) GetByName(name string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var emoji *model.Emoji
if err := es.GetReplica().SelectOne(&emoji,
`SELECT
*
FROM
Emoji
WHERE
Name = :Name
AND DeleteAt = 0`, map[string]interface{}{"Name": name}); err != nil {
result.Err = model.NewAppError("SqlEmojiStore.GetByName", "store.sql_emoji.get_by_name.app_error", nil, "name="+name+", "+err.Error(), http.StatusInternalServerError)
} else {
result.Data = emoji
}
})
}
func (es SqlEmojiStore) GetList(offset, limit int) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var emoji []*model.Emoji
if _, err := es.GetReplica().Select(&emoji,
`SELECT
*
FROM
Emoji
WHERE
DeleteAt = 0
LIMIT :Limit OFFSET :Offset`, map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil {
result.Err = model.NewAppError("SqlEmojiStore.GetList", "store.sql_emoji.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = emoji
}
})
}
func (es SqlEmojiStore) Delete(id string, time int64) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if sqlResult, err := es.GetMaster().Exec(
`Update
Emoji
SET
DeleteAt = :DeleteAt,
UpdateAt = :UpdateAt
WHERE
Id = :Id
AND DeleteAt = 0`, map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": id}); err != nil {
result.Err = model.NewAppError("SqlEmojiStore.Delete", "store.sql_emoji.delete.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
} else if rows, _ := sqlResult.RowsAffected(); rows == 0 {
result.Err = model.NewAppError("SqlEmojiStore.Delete", "store.sql_emoji.delete.no_results", nil, "id="+id+", err="+err.Error(), http.StatusBadRequest)
}
emojiCache.Remove(id)
})
}
|