github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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) DropAllTables() {
   185  	s.DatabaseLayer.DropAllTables()
   186  }
   187  
   188  func (s *LayeredStore) TotalMasterDbConnections() int {
   189  	return s.DatabaseLayer.TotalMasterDbConnections()
   190  }
   191  
   192  func (s *LayeredStore) TotalReadDbConnections() int {
   193  	return s.DatabaseLayer.TotalReadDbConnections()
   194  }
   195  
   196  func (s *LayeredStore) TotalSearchDbConnections() int {
   197  	return s.DatabaseLayer.TotalSearchDbConnections()
   198  }
   199  
   200  type LayeredReactionStore struct {
   201  	*LayeredStore
   202  }
   203  
   204  func (s *LayeredReactionStore) Save(reaction *model.Reaction) StoreChannel {
   205  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   206  		return supplier.ReactionSave(s.TmpContext, reaction)
   207  	})
   208  }
   209  
   210  func (s *LayeredReactionStore) Delete(reaction *model.Reaction) StoreChannel {
   211  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   212  		return supplier.ReactionDelete(s.TmpContext, reaction)
   213  	})
   214  }
   215  
   216  func (s *LayeredReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel {
   217  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   218  		return supplier.ReactionGetForPost(s.TmpContext, postId)
   219  	})
   220  }
   221  
   222  func (s *LayeredReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel {
   223  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   224  		return supplier.ReactionDeleteAllWithEmojiName(s.TmpContext, emojiName)
   225  	})
   226  }
   227  
   228  func (s *LayeredReactionStore) PermanentDeleteBatch(endTime int64, limit int64) StoreChannel {
   229  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   230  		return supplier.ReactionPermanentDeleteBatch(s.TmpContext, endTime, limit)
   231  	})
   232  }
   233  
   234  type LayeredRoleStore struct {
   235  	*LayeredStore
   236  }
   237  
   238  func (s *LayeredRoleStore) Save(role *model.Role) StoreChannel {
   239  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   240  		return supplier.RoleSave(s.TmpContext, role)
   241  	})
   242  }
   243  
   244  func (s *LayeredRoleStore) Get(roleId string) StoreChannel {
   245  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   246  		return supplier.RoleGet(s.TmpContext, roleId)
   247  	})
   248  }
   249  
   250  func (s *LayeredRoleStore) GetByName(name string) StoreChannel {
   251  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   252  		return supplier.RoleGetByName(s.TmpContext, name)
   253  	})
   254  }
   255  
   256  func (s *LayeredRoleStore) GetByNames(names []string) StoreChannel {
   257  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   258  		return supplier.RoleGetByNames(s.TmpContext, names)
   259  	})
   260  }
   261  
   262  func (s *LayeredRoleStore) Delete(roldId string) StoreChannel {
   263  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   264  		return supplier.RoleDelete(s.TmpContext, roldId)
   265  	})
   266  }
   267  
   268  func (s *LayeredRoleStore) PermanentDeleteAll() StoreChannel {
   269  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   270  		return supplier.RolePermanentDeleteAll(s.TmpContext)
   271  	})
   272  }
   273  
   274  type LayeredSchemeStore struct {
   275  	*LayeredStore
   276  }
   277  
   278  func (s *LayeredSchemeStore) Save(scheme *model.Scheme) StoreChannel {
   279  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   280  		return supplier.SchemeSave(s.TmpContext, scheme)
   281  	})
   282  }
   283  
   284  func (s *LayeredSchemeStore) Get(schemeId string) StoreChannel {
   285  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   286  		return supplier.SchemeGet(s.TmpContext, schemeId)
   287  	})
   288  }
   289  
   290  func (s *LayeredSchemeStore) Delete(schemeId string) StoreChannel {
   291  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   292  		return supplier.SchemeDelete(s.TmpContext, schemeId)
   293  	})
   294  }
   295  
   296  func (s *LayeredSchemeStore) GetAllPage(scope string, offset int, limit int) StoreChannel {
   297  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   298  		return supplier.SchemeGetAllPage(s.TmpContext, scope, offset, limit)
   299  	})
   300  }
   301  
   302  func (s *LayeredSchemeStore) PermanentDeleteAll() StoreChannel {
   303  	return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
   304  		return supplier.SchemePermanentDeleteAll(s.TmpContext)
   305  	})
   306  }