From d8b009ee3d1069ef9ba3703fec367bdce3a8e996 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 9 Jan 2023 14:49:01 +0800 Subject: [PATCH] sqlx: adjust index post service stub interface --- internal/dao/sakila/index.go | 65 +++++++++++++++++++++++++++++------ internal/dao/sakila/sakila.go | 37 ++++++++++++++------ internal/dao/sakila/sqlx.go | 3 -- 3 files changed, 81 insertions(+), 24 deletions(-) diff --git a/internal/dao/sakila/index.go b/internal/dao/sakila/index.go index 341cc544..3e3144fd 100644 --- a/internal/dao/sakila/index.go +++ b/internal/dao/sakila/index.go @@ -11,43 +11,88 @@ import ( ) var ( - _ core.IndexPostsService = (*indexPostsServant)(nil) + _ core.IndexPostsService = (*friendIndexServant)(nil) + _ core.IndexPostsService = (*followIndexServant)(nil) + _ core.IndexPostsService = (*lightIndexServant)(nil) _ core.IndexPostsService = (*simpleIndexPostsServant)(nil) ) -type indexPostsServant struct { +type friendIndexServant struct { *sqlxServant + ams core.AuthorizationManageService + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +type followIndexServant struct { + *sqlxServant + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +type lightIndexServant struct { + *sqlxServant + ths core.TweetHelpService stmtIndex *sqlx.Stmt } type simpleIndexPostsServant struct { *sqlxServant + ths core.TweetHelpService stmtIndex *sqlx.Stmt } // IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; -func (s *indexPostsServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { +func (s *friendIndexServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; +func (s *followIndexServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; +func (s *lightIndexServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { // TODO - debug.NotImplemented() - return nil, nil + return nil, debug.ErrNotImplemented } // simpleCacheIndexGetPosts simpleCacheIndex 专属获取广场推文列表函数 func (s *simpleIndexPostsServant) IndexPosts(_user *core.User, offset int, limit int) (*core.IndexTweetList, error) { // TODO - debug.NotImplemented() - return nil, nil + return nil, debug.ErrNotImplemented +} + +func newFriendIndexService(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpService) core.IndexPostsService { + return &friendIndexServant{ + ams: ams, + sqlxServant: newSqlxServant(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newFollowIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { + return &followIndexServant{ + ths: ths, + sqlxServant: newSqlxServant(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } } -func newIndexPostsService(db *sqlx.DB) core.IndexPostsService { - return &indexPostsServant{ +func newLightIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { + return &lightIndexServant{ + ths: ths, sqlxServant: newSqlxServant(db), stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), } } -func newSimpleIndexPostsService(db *sqlx.DB) core.IndexPostsService { +func newSimpleIndexPostsService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { return &simpleIndexPostsServant{ + ths: ths, sqlxServant: newSqlxServant(db), stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), } diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index 354b7525..76ccfd6b 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -35,33 +35,48 @@ type dataServant struct { } func NewDataService() (core.DataService, core.VersionInfo) { - // initialize CacheIndex if needed var ( - c core.CacheIndexService - v core.VersionInfo + v core.VersionInfo + cis core.CacheIndexService + ips core.IndexPostsService ) db := sqlxDB() pvs := security.NewPhoneVerifyService() + ams := NewAuthorizationManageService() + ths := newTweetHelpService(db) - i := newIndexPostsService(db) + // initialize core.IndexPostsService + if cfg.If("Friendship") { + ips = newFriendIndexService(db, ams, ths) + } else if cfg.If("Followship") { + ips = newFollowIndexService(db, ths) + } else if cfg.If("Lightship") { + ips = newLightIndexService(db, ths) + } else { + // default use lightship post index service + ips = newLightIndexService(db, ths) + } + + // initialize core.CacheIndexService if cfg.If("SimpleCacheIndex") { - i = newSimpleIndexPostsService(db) - c, v = cache.NewSimpleCacheIndexService(i) + // simpleCache use special post index service + ips = newSimpleIndexPostsService(db, ths) + cis, v = cache.NewSimpleCacheIndexService(ips) } else if cfg.If("BigCacheIndex") { - a := newAuthorizationManageService(db) - c, v = cache.NewBigCacheIndexService(i, a) + // TODO: make cache index post in different scence like friendship/followship/lightship + cis, v = cache.NewBigCacheIndexService(ips, ams) } else { - c, v = cache.NewNoneCacheIndexService(i) + cis, v = cache.NewNoneCacheIndexService(ips) } logrus.Infof("use %s as cache index service by version: %s", v.Name(), v.Version()) ds := &dataServant{ - IndexPostsService: c, + IndexPostsService: cis, WalletService: newWalletService(db), MessageService: newMessageService(db), TopicService: newTopicService(db), TweetService: newTweetService(db), - TweetManageService: newTweetManageService(db, c), + TweetManageService: newTweetManageService(db, cis), TweetHelpService: newTweetHelpService(db), CommentService: newCommentService(db), CommentManageService: newCommentManageService(db), diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go index d5696418..c38994b2 100644 --- a/internal/dao/sakila/sqlx.go +++ b/internal/dao/sakila/sqlx.go @@ -2,9 +2,6 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -// Core service implement base sqlx+mysql. All sub-service -// will declare here and provide initial function. - package sakila import (