github.com/levb/mattermost-server@v5.3.1+incompatible/store/layered_store.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package store 5 6 import ( 7 "context" 8 9 "github.com/mattermost/mattermost-server/einterfaces" 10 "github.com/mattermost/mattermost-server/mlog" 11 "github.com/mattermost/mattermost-server/model" 12 ) 13 14 const ( 15 ENABLE_EXPERIMENTAL_REDIS = false 16 ) 17 18 type LayeredStoreDatabaseLayer interface { 19 LayeredStoreSupplier 20 Store 21 } 22 23 type LayeredStore struct { 24 TmpContext context.Context 25 ReactionStore ReactionStore 26 RoleStore RoleStore 27 SchemeStore SchemeStore 28 DatabaseLayer LayeredStoreDatabaseLayer 29 LocalCacheLayer *LocalCacheSupplier 30 RedisLayer *RedisSupplier 31 LayerChainHead LayeredStoreSupplier 32 } 33 34 func NewLayeredStore(db LayeredStoreDatabaseLayer, metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) Store { 35 store := &LayeredStore{ 36 TmpContext: context.TODO(), 37 DatabaseLayer: db, 38 LocalCacheLayer: NewLocalCacheSupplier(metrics, cluster), 39 } 40 41 store.ReactionStore = &LayeredReactionStore{store} 42 store.RoleStore = &LayeredRoleStore{store} 43 store.SchemeStore = &LayeredSchemeStore{store} 44 45 // Setup the chain 46 if ENABLE_EXPERIMENTAL_REDIS { 47 mlog.Debug("Experimental redis enabled.") 48 store.RedisLayer = NewRedisSupplier() 49 store.RedisLayer.SetChainNext(store.DatabaseLayer) 50 store.LayerChainHead = store.RedisLayer 51 } else { 52 store.LocalCacheLayer.SetChainNext(store.DatabaseLayer) 53 store.LayerChainHead = store.LocalCacheLayer 54 } 55 56 return store 57 } 58 59 type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult 60 61 func (s *LayeredStore) RunQuery(queryFunction QueryFunction) StoreChannel { 62 storeChannel := make(StoreChannel) 63 64 go func() { 65 result := queryFunction(s.LayerChainHead) 66 storeChannel <- result.StoreResult 67 }() 68 69 return storeChannel 70 } 71 72 func (s *LayeredStore) Team() TeamStore { 73 return s.DatabaseLayer.Team() 74 } 75 76 func (s *LayeredStore) Channel() ChannelStore { 77 return s.DatabaseLayer.Channel() 78 } 79 80 func (s *LayeredStore) Post() PostStore { 81 return s.DatabaseLayer.Post() 82 } 83 84 func (s *LayeredStore) User() UserStore { 85 return s.DatabaseLayer.User() 86 } 87 88 func (s *LayeredStore) Audit() AuditStore { 89 return s.DatabaseLayer.Audit() 90 } 91 92 func (s *LayeredStore) ClusterDiscovery() ClusterDiscoveryStore { 93 return s.DatabaseLayer.ClusterDiscovery() 94 } 95 96 func (s *LayeredStore) Compliance() ComplianceStore { 97 return s.DatabaseLayer.Compliance() 98 } 99 100 func (s *LayeredStore) Session() SessionStore { 101 return s.DatabaseLayer.Session() 102 } 103 104 func (s *LayeredStore) OAuth() OAuthStore { 105 return s.DatabaseLayer.OAuth() 106 } 107 108 func (s *LayeredStore) System() SystemStore { 109 return s.DatabaseLayer.System() 110 } 111 112 func (s *LayeredStore) Webhook() WebhookStore { 113 return s.DatabaseLayer.Webhook() 114 } 115 116 func (s *LayeredStore) Command() CommandStore { 117 return s.DatabaseLayer.Command() 118 } 119 120 func (s *LayeredStore) CommandWebhook() CommandWebhookStore { 121 return s.DatabaseLayer.CommandWebhook() 122 } 123 124 func (s *LayeredStore) Preference() PreferenceStore { 125 return s.DatabaseLayer.Preference() 126 } 127 128 func (s *LayeredStore) License() LicenseStore { 129 return s.DatabaseLayer.License() 130 } 131 132 func (s *LayeredStore) Token() TokenStore { 133 return s.DatabaseLayer.Token() 134 } 135 136 func (s *LayeredStore) Emoji() EmojiStore { 137 return s.DatabaseLayer.Emoji() 138 } 139 140 func (s *LayeredStore) Status() StatusStore { 141 return s.DatabaseLayer.Status() 142 } 143 144 func (s *LayeredStore) FileInfo() FileInfoStore { 145 return s.DatabaseLayer.FileInfo() 146 } 147 148 func (s *LayeredStore) Reaction() ReactionStore { 149 return s.ReactionStore 150 } 151 152 func (s *LayeredStore) Job() JobStore { 153 return s.DatabaseLayer.Job() 154 } 155 156 func (s *LayeredStore) UserAccessToken() UserAccessTokenStore { 157 return s.DatabaseLayer.UserAccessToken() 158 } 159 160 func (s *LayeredStore) ChannelMemberHistory() ChannelMemberHistoryStore { 161 return s.DatabaseLayer.ChannelMemberHistory() 162 } 163 164 func (s *LayeredStore) Plugin() PluginStore { 165 return s.DatabaseLayer.Plugin() 166 } 167 168 func (s *LayeredStore) Role() RoleStore { 169 return s.RoleStore 170 } 171 172 func (s *LayeredStore) Scheme() SchemeStore { 173 return s.SchemeStore 174 } 175 176 func (s *LayeredStore) MarkSystemRanUnitTests() { 177 s.DatabaseLayer.MarkSystemRanUnitTests() 178 } 179 180 func (s *LayeredStore) Close() { 181 s.DatabaseLayer.Close() 182 } 183 184 func (s *LayeredStore) LockToMaster() { 185 s.DatabaseLayer.LockToMaster() 186 } 187 188 func (s *LayeredStore) UnlockFromMaster() { 189 s.DatabaseLayer.UnlockFromMaster() 190 } 191 192 func (s *LayeredStore) DropAllTables() { 193 s.DatabaseLayer.DropAllTables() 194 } 195 196 func (s *LayeredStore) TotalMasterDbConnections() int { 197 return s.DatabaseLayer.TotalMasterDbConnections() 198 } 199 200 func (s *LayeredStore) TotalReadDbConnections() int { 201 return s.DatabaseLayer.TotalReadDbConnections() 202 } 203 204 func (s *LayeredStore) TotalSearchDbConnections() int { 205 return s.DatabaseLayer.TotalSearchDbConnections() 206 } 207 208 type LayeredReactionStore struct { 209 *LayeredStore 210 } 211 212 func (s *LayeredReactionStore) Save(reaction *model.Reaction) StoreChannel { 213 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 214 return supplier.ReactionSave(s.TmpContext, reaction) 215 }) 216 } 217 218 func (s *LayeredReactionStore) Delete(reaction *model.Reaction) StoreChannel { 219 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 220 return supplier.ReactionDelete(s.TmpContext, reaction) 221 }) 222 } 223 224 func (s *LayeredReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel { 225 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 226 return supplier.ReactionGetForPost(s.TmpContext, postId) 227 }) 228 } 229 230 func (s *LayeredReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel { 231 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 232 return supplier.ReactionDeleteAllWithEmojiName(s.TmpContext, emojiName) 233 }) 234 } 235 236 func (s *LayeredReactionStore) PermanentDeleteBatch(endTime int64, limit int64) StoreChannel { 237 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 238 return supplier.ReactionPermanentDeleteBatch(s.TmpContext, endTime, limit) 239 }) 240 } 241 242 type LayeredRoleStore struct { 243 *LayeredStore 244 } 245 246 func (s *LayeredRoleStore) Save(role *model.Role) StoreChannel { 247 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 248 return supplier.RoleSave(s.TmpContext, role) 249 }) 250 } 251 252 func (s *LayeredRoleStore) Get(roleId string) StoreChannel { 253 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 254 return supplier.RoleGet(s.TmpContext, roleId) 255 }) 256 } 257 258 func (s *LayeredRoleStore) GetByName(name string) StoreChannel { 259 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 260 return supplier.RoleGetByName(s.TmpContext, name) 261 }) 262 } 263 264 func (s *LayeredRoleStore) GetByNames(names []string) StoreChannel { 265 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 266 return supplier.RoleGetByNames(s.TmpContext, names) 267 }) 268 } 269 270 func (s *LayeredRoleStore) Delete(roldId string) StoreChannel { 271 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 272 return supplier.RoleDelete(s.TmpContext, roldId) 273 }) 274 } 275 276 func (s *LayeredRoleStore) PermanentDeleteAll() StoreChannel { 277 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 278 return supplier.RolePermanentDeleteAll(s.TmpContext) 279 }) 280 } 281 282 type LayeredSchemeStore struct { 283 *LayeredStore 284 } 285 286 func (s *LayeredSchemeStore) Save(scheme *model.Scheme) StoreChannel { 287 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 288 return supplier.SchemeSave(s.TmpContext, scheme) 289 }) 290 } 291 292 func (s *LayeredSchemeStore) Get(schemeId string) StoreChannel { 293 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 294 return supplier.SchemeGet(s.TmpContext, schemeId) 295 }) 296 } 297 298 func (s *LayeredSchemeStore) GetByName(schemeName string) StoreChannel { 299 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 300 return supplier.SchemeGetByName(s.TmpContext, schemeName) 301 }) 302 } 303 304 func (s *LayeredSchemeStore) Delete(schemeId string) StoreChannel { 305 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 306 return supplier.SchemeDelete(s.TmpContext, schemeId) 307 }) 308 } 309 310 func (s *LayeredSchemeStore) GetAllPage(scope string, offset int, limit int) StoreChannel { 311 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 312 return supplier.SchemeGetAllPage(s.TmpContext, scope, offset, limit) 313 }) 314 } 315 316 func (s *LayeredSchemeStore) PermanentDeleteAll() StoreChannel { 317 return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { 318 return supplier.SchemePermanentDeleteAll(s.TmpContext) 319 }) 320 }