github.com/mad-app/mattermost-server@v5.11.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  	GroupStore      GroupStore
    33  }
    34  
    35  func NewLayeredStore(db LayeredStoreDatabaseLayer, metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) Store {
    36  	store := &LayeredStore{
    37  		TmpContext:      context.TODO(),
    38  		DatabaseLayer:   db,
    39  		LocalCacheLayer: NewLocalCacheSupplier(metrics, cluster),
    40  	}
    41  
    42  	store.ReactionStore = &LayeredReactionStore{store}
    43  	store.RoleStore = &LayeredRoleStore{store}
    44  	store.SchemeStore = &LayeredSchemeStore{store}
    45  	store.GroupStore = &LayeredGroupStore{store}
    46  
    47  	// Setup the chain
    48  	if ENABLE_EXPERIMENTAL_REDIS {
    49  		mlog.Debug("Experimental redis enabled.")
    50  		store.RedisLayer = NewRedisSupplier()
    51  		store.RedisLayer.SetChainNext(store.DatabaseLayer)
    52  		store.LayerChainHead = store.RedisLayer
    53  	} else {
    54  		store.LocalCacheLayer.SetChainNext(store.DatabaseLayer)
    55  		store.LayerChainHead = store.LocalCacheLayer
    56  	}
    57  
    58  	return store
    59  }
    60  
    61  type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult
    62  
    63  func (s *LayeredStore) RunQuery(queryFunction QueryFunction) StoreChannel {
    64  	storeChannel := make(StoreChannel)
    65  
    66  	go func() {
    67  		result := queryFunction(s.LayerChainHead)
    68  		storeChannel <- result.StoreResult
    69  	}()
    70  
    71  	return storeChannel
    72  }
    73  
    74  func (s *LayeredStore) Team() TeamStore {
    75  	return s.DatabaseLayer.Team()
    76  }
    77  
    78  func (s *LayeredStore) Channel() ChannelStore {
    79  	return s.DatabaseLayer.Channel()
    80  }
    81  
    82  func (s *LayeredStore) Post() PostStore {
    83  	return s.DatabaseLayer.Post()
    84  }
    85  
    86  func (s *LayeredStore) User() UserStore {
    87  	return s.DatabaseLayer.User()
    88  }
    89  
    90  func (s *LayeredStore) Bot() BotStore {
    91  	return s.DatabaseLayer.Bot()
    92  }
    93  
    94  func (s *LayeredStore) Audit() AuditStore {
    95  	return s.DatabaseLayer.Audit()
    96  }
    97  
    98  func (s *LayeredStore) ClusterDiscovery() ClusterDiscoveryStore {
    99  	return s.DatabaseLayer.ClusterDiscovery()
   100  }
   101  
   102  func (s *LayeredStore) Compliance() ComplianceStore {
   103  	return s.DatabaseLayer.Compliance()
   104  }
   105  
   106  func (s *LayeredStore) Session() SessionStore {
   107  	return s.DatabaseLayer.Session()
   108  }
   109  
   110  func (s *LayeredStore) OAuth() OAuthStore {
   111  	return s.DatabaseLayer.OAuth()
   112  }
   113  
   114  func (s *LayeredStore) System() SystemStore {
   115  	return s.DatabaseLayer.System()
   116  }
   117  
   118  func (s *LayeredStore) Webhook() WebhookStore {
   119  	return s.DatabaseLayer.Webhook()
   120  }
   121  
   122  func (s *LayeredStore) Command() CommandStore {
   123  	return s.DatabaseLayer.Command()
   124  }
   125  
   126  func (s *LayeredStore) CommandWebhook() CommandWebhookStore {
   127  	return s.DatabaseLayer.CommandWebhook()
   128  }
   129  
   130  func (s *LayeredStore) Preference() PreferenceStore {
   131  	return s.DatabaseLayer.Preference()
   132  }
   133  
   134  func (s *LayeredStore) License() LicenseStore {
   135  	return s.DatabaseLayer.License()
   136  }
   137  
   138  func (s *LayeredStore) Token() TokenStore {
   139  	return s.DatabaseLayer.Token()
   140  }
   141  
   142  func (s *LayeredStore) Emoji() EmojiStore {
   143  	return s.DatabaseLayer.Emoji()
   144  }
   145  
   146  func (s *LayeredStore) Status() StatusStore {
   147  	return s.DatabaseLayer.Status()
   148  }
   149  
   150  func (s *LayeredStore) FileInfo() FileInfoStore {
   151  	return s.DatabaseLayer.FileInfo()
   152  }
   153  
   154  func (s *LayeredStore) Reaction() ReactionStore {
   155  	return s.ReactionStore
   156  }
   157  
   158  func (s *LayeredStore) Job() JobStore {
   159  	return s.DatabaseLayer.Job()
   160  }
   161  
   162  func (s *LayeredStore) UserAccessToken() UserAccessTokenStore {
   163  	return s.DatabaseLayer.UserAccessToken()
   164  }
   165  
   166  func (s *LayeredStore) ChannelMemberHistory() ChannelMemberHistoryStore {
   167  	return s.DatabaseLayer.ChannelMemberHistory()
   168  }
   169  
   170  func (s *LayeredStore) Plugin() PluginStore {
   171  	return s.DatabaseLayer.Plugin()
   172  }
   173  
   174  func (s *LayeredStore) Role() RoleStore {
   175  	return s.RoleStore
   176  }
   177  
   178  func (s *LayeredStore) TermsOfService() TermsOfServiceStore {
   179  	return s.DatabaseLayer.TermsOfService()
   180  }
   181  
   182  func (s *LayeredStore) UserTermsOfService() UserTermsOfServiceStore {
   183  	return s.DatabaseLayer.UserTermsOfService()
   184  }
   185  
   186  func (s *LayeredStore) Scheme() SchemeStore {
   187  	return s.SchemeStore
   188  }
   189  
   190  func (s *LayeredStore) Group() GroupStore {
   191  	return s.GroupStore
   192  }
   193  
   194  func (s *LayeredStore) LinkMetadata() LinkMetadataStore {
   195  	return s.DatabaseLayer.LinkMetadata()
   196  }
   197  
   198  func (s *LayeredStore) MarkSystemRanUnitTests() {
   199  	s.DatabaseLayer.MarkSystemRanUnitTests()
   200  }
   201  
   202  func (s *LayeredStore) Close() {
   203  	s.DatabaseLayer.Close()
   204  }
   205  
   206  func (s *LayeredStore) LockToMaster() {
   207  	s.DatabaseLayer.LockToMaster()
   208  }
   209  
   210  func (s *LayeredStore) UnlockFromMaster() {
   211  	s.DatabaseLayer.UnlockFromMaster()
   212  }
   213  
   214  func (s *LayeredStore) DropAllTables() {
   215  	defer s.LocalCacheLayer.Invalidate()
   216  	s.DatabaseLayer.DropAllTables()
   217  }
   218  
   219  func (s *LayeredStore) TotalMasterDbConnections() int {
   220  	return s.DatabaseLayer.TotalMasterDbConnections()
   221  }
   222  
   223  func (s *LayeredStore) TotalReadDbConnections() int {
   224  	return s.DatabaseLayer.TotalReadDbConnections()
   225  }
   226  
   227  func (s *LayeredStore) TotalSearchDbConnections() int {
   228  	return s.DatabaseLayer.TotalSearchDbConnections()
   229  }
   230  
   231  type LayeredReactionStore struct {
   232  	*LayeredStore
   233  }
   234  
   235  func (s *LayeredReactionStore) Save(reaction *model.Reaction) StoreChannel {
   236  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   237  		return supplier.ReactionSave(s.TmpContext, reaction)
   238  	})
   239  }
   240  
   241  func (s *LayeredReactionStore) Delete(reaction *model.Reaction) StoreChannel {
   242  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   243  		return supplier.ReactionDelete(s.TmpContext, reaction)
   244  	})
   245  }
   246  
   247  func (s *LayeredReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel {
   248  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   249  		return supplier.ReactionGetForPost(s.TmpContext, postId)
   250  	})
   251  }
   252  
   253  func (s *LayeredReactionStore) BulkGetForPosts(postIds []string) StoreChannel {
   254  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   255  		return supplier.ReactionsBulkGetForPosts(s.TmpContext, postIds)
   256  	})
   257  }
   258  
   259  func (s *LayeredReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel {
   260  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   261  		return supplier.ReactionDeleteAllWithEmojiName(s.TmpContext, emojiName)
   262  	})
   263  }
   264  
   265  func (s *LayeredReactionStore) PermanentDeleteBatch(endTime int64, limit int64) StoreChannel {
   266  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   267  		return supplier.ReactionPermanentDeleteBatch(s.TmpContext, endTime, limit)
   268  	})
   269  }
   270  
   271  type LayeredRoleStore struct {
   272  	*LayeredStore
   273  }
   274  
   275  func (s *LayeredRoleStore) Save(role *model.Role) StoreChannel {
   276  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   277  		return supplier.RoleSave(s.TmpContext, role)
   278  	})
   279  }
   280  
   281  func (s *LayeredRoleStore) Get(roleId string) StoreChannel {
   282  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   283  		return supplier.RoleGet(s.TmpContext, roleId)
   284  	})
   285  }
   286  
   287  func (s *LayeredRoleStore) GetAll() StoreChannel {
   288  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   289  		return supplier.RoleGetAll(s.TmpContext)
   290  	})
   291  }
   292  
   293  func (s *LayeredRoleStore) GetByName(name string) StoreChannel {
   294  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   295  		return supplier.RoleGetByName(s.TmpContext, name)
   296  	})
   297  }
   298  
   299  func (s *LayeredRoleStore) GetByNames(names []string) StoreChannel {
   300  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   301  		return supplier.RoleGetByNames(s.TmpContext, names)
   302  	})
   303  }
   304  
   305  func (s *LayeredRoleStore) Delete(roldId string) StoreChannel {
   306  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   307  		return supplier.RoleDelete(s.TmpContext, roldId)
   308  	})
   309  }
   310  
   311  func (s *LayeredRoleStore) PermanentDeleteAll() StoreChannel {
   312  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   313  		return supplier.RolePermanentDeleteAll(s.TmpContext)
   314  	})
   315  }
   316  
   317  type LayeredSchemeStore struct {
   318  	*LayeredStore
   319  }
   320  
   321  func (s *LayeredSchemeStore) Save(scheme *model.Scheme) StoreChannel {
   322  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   323  		return supplier.SchemeSave(s.TmpContext, scheme)
   324  	})
   325  }
   326  
   327  func (s *LayeredSchemeStore) Get(schemeId string) StoreChannel {
   328  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   329  		return supplier.SchemeGet(s.TmpContext, schemeId)
   330  	})
   331  }
   332  
   333  func (s *LayeredSchemeStore) GetByName(schemeName string) StoreChannel {
   334  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   335  		return supplier.SchemeGetByName(s.TmpContext, schemeName)
   336  	})
   337  }
   338  
   339  func (s *LayeredSchemeStore) Delete(schemeId string) StoreChannel {
   340  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   341  		return supplier.SchemeDelete(s.TmpContext, schemeId)
   342  	})
   343  }
   344  
   345  func (s *LayeredSchemeStore) GetAllPage(scope string, offset int, limit int) StoreChannel {
   346  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   347  		return supplier.SchemeGetAllPage(s.TmpContext, scope, offset, limit)
   348  	})
   349  }
   350  
   351  func (s *LayeredSchemeStore) PermanentDeleteAll() StoreChannel {
   352  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   353  		return supplier.SchemePermanentDeleteAll(s.TmpContext)
   354  	})
   355  }
   356  
   357  type LayeredGroupStore struct {
   358  	*LayeredStore
   359  }
   360  
   361  func (s *LayeredGroupStore) Create(group *model.Group) StoreChannel {
   362  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   363  		return supplier.GroupCreate(s.TmpContext, group)
   364  	})
   365  }
   366  
   367  func (s *LayeredGroupStore) Get(groupID string) StoreChannel {
   368  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   369  		return supplier.GroupGet(s.TmpContext, groupID)
   370  	})
   371  }
   372  
   373  func (s *LayeredGroupStore) GetByRemoteID(remoteID string, groupSource model.GroupSource) StoreChannel {
   374  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   375  		return supplier.GroupGetByRemoteID(s.TmpContext, remoteID, groupSource)
   376  	})
   377  }
   378  
   379  func (s *LayeredGroupStore) GetAllBySource(groupSource model.GroupSource) StoreChannel {
   380  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   381  		return supplier.GroupGetAllBySource(s.TmpContext, groupSource)
   382  	})
   383  }
   384  
   385  func (s *LayeredGroupStore) Update(group *model.Group) StoreChannel {
   386  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   387  		return supplier.GroupUpdate(s.TmpContext, group)
   388  	})
   389  }
   390  
   391  func (s *LayeredGroupStore) Delete(groupID string) StoreChannel {
   392  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   393  		return supplier.GroupDelete(s.TmpContext, groupID)
   394  	})
   395  }
   396  
   397  func (s *LayeredGroupStore) GetMemberUsers(groupID string) StoreChannel {
   398  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   399  		return supplier.GroupGetMemberUsers(s.TmpContext, groupID)
   400  	})
   401  }
   402  
   403  func (s *LayeredGroupStore) GetMemberUsersPage(groupID string, offset int, limit int) StoreChannel {
   404  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   405  		return supplier.GroupGetMemberUsersPage(s.TmpContext, groupID, offset, limit)
   406  	})
   407  }
   408  
   409  func (s *LayeredGroupStore) GetMemberCount(groupID string) StoreChannel {
   410  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   411  		return supplier.GroupGetMemberCount(s.TmpContext, groupID)
   412  	})
   413  }
   414  
   415  func (s *LayeredGroupStore) CreateOrRestoreMember(groupID string, userID string) StoreChannel {
   416  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   417  		return supplier.GroupCreateOrRestoreMember(s.TmpContext, groupID, userID)
   418  	})
   419  }
   420  
   421  func (s *LayeredGroupStore) DeleteMember(groupID string, userID string) StoreChannel {
   422  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   423  		return supplier.GroupDeleteMember(s.TmpContext, groupID, userID)
   424  	})
   425  }
   426  
   427  func (s *LayeredGroupStore) CreateGroupSyncable(groupSyncable *model.GroupSyncable) StoreChannel {
   428  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   429  		return supplier.GroupCreateGroupSyncable(s.TmpContext, groupSyncable)
   430  	})
   431  }
   432  
   433  func (s *LayeredGroupStore) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) StoreChannel {
   434  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   435  		return supplier.GroupGetGroupSyncable(s.TmpContext, groupID, syncableID, syncableType)
   436  	})
   437  }
   438  
   439  func (s *LayeredGroupStore) GetAllGroupSyncablesByGroupId(groupID string, syncableType model.GroupSyncableType) StoreChannel {
   440  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   441  		return supplier.GroupGetAllGroupSyncablesByGroup(s.TmpContext, groupID, syncableType)
   442  	})
   443  }
   444  
   445  func (s *LayeredGroupStore) UpdateGroupSyncable(groupSyncable *model.GroupSyncable) StoreChannel {
   446  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   447  		return supplier.GroupUpdateGroupSyncable(s.TmpContext, groupSyncable)
   448  	})
   449  }
   450  
   451  func (s *LayeredGroupStore) DeleteGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) StoreChannel {
   452  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   453  		return supplier.GroupDeleteGroupSyncable(s.TmpContext, groupID, syncableID, syncableType)
   454  	})
   455  }
   456  
   457  func (s *LayeredGroupStore) PendingAutoAddTeamMembers(minGroupMembersCreateAt int64) StoreChannel {
   458  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   459  		return supplier.PendingAutoAddTeamMembers(s.TmpContext, minGroupMembersCreateAt)
   460  	})
   461  }
   462  
   463  func (s *LayeredGroupStore) PendingAutoAddChannelMembers(minGroupMembersCreateAt int64) StoreChannel {
   464  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   465  		return supplier.PendingAutoAddChannelMembers(s.TmpContext, minGroupMembersCreateAt)
   466  	})
   467  }