You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/controller/group.go

173 lines
5.9 KiB

2 years ago
package controller
import (
"Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/relation"
"Open_IM/pkg/common/db/unrelation"
"context"
"github.com/dtm-labs/rockscache"
_ "github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
)
type GroupInterface interface {
FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error)
CreateGroup(ctx context.Context, groups []*relation.Group) error
DeleteGroupByIDs(ctx context.Context, groupIDs []string) error
TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error)
//mongo
2 years ago
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string, memberNumCount int) error
2 years ago
GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
}
type GroupController struct {
database DataBase
}
2 years ago
func NewGroupController(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Client) GroupInterface {
2 years ago
groupController := &GroupController{database: newGroupDatabase(db, rdb, mgoDB)}
return groupController
}
func (g *GroupController) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) {
2 years ago
return g.database.FindGroupsByID(ctx, groupIDs)
2 years ago
}
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relation.Group) error {
2 years ago
return g.database.CreateGroup(ctx, groups)
2 years ago
}
func (g *GroupController) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error {
2 years ago
return g.database.DeleteGroupByIDs(ctx, groupIDs)
2 years ago
}
func (g *GroupController) TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error) {
2 years ago
return g.database.TakeGroupByID(ctx, groupID)
2 years ago
}
func (g *GroupController) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
2 years ago
return g.database.GetSuperGroupByID(ctx, groupID)
2 years ago
}
2 years ago
func (g *GroupController) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string, memberNumCount int) error {
return g.database.CreateSuperGroup(ctx, groupID, initMemberIDList, memberNumCount)
}
2 years ago
type DataBase interface {
2 years ago
FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error)
CreateGroup(ctx context.Context, groups []*relation.Group) error
DeleteGroupByIDs(ctx context.Context, groupIDs []string) error
TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error)
GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
2 years ago
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string, memberNumCount int) error
2 years ago
}
type GroupDataBase struct {
2 years ago
groupDB *relation.Group
groupMemberDB *relation.GroupMember
groupRequestDB *relation.GroupRequest
2 years ago
db *gorm.DB
2 years ago
2 years ago
cache *cache.GroupCache
2 years ago
mongoDB *unrelation.SuperGroupMgoDB
2 years ago
}
2 years ago
func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Client) DataBase {
2 years ago
groupDB := relation.NewGroupDB(db)
groupMemberDB := relation.NewGroupMemberDB(db)
groupRequestDB := relation.NewGroupRequest(db)
2 years ago
newDB := db
2 years ago
database := &GroupDataBase{
2 years ago
groupDB: groupDB,
groupMemberDB: groupMemberDB,
groupRequestDB: groupRequestDB,
2 years ago
db: newDB,
2 years ago
cache: cache.NewGroupCache(rdb, groupDB, groupMemberDB, groupRequestDB, rockscache.Options{
2 years ago
RandomExpireAdjustment: 0.2,
DisableCacheRead: false,
DisableCacheDelete: false,
StrongConsistency: true,
}),
mongoDB: unrelation.NewSuperGroupMgoDB(mgoDB),
}
return database
}
2 years ago
func (g *GroupDataBase) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) {
2 years ago
return g.cache.GetGroupsInfo(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation.Group) error {
2 years ago
return g.groupDB.Create(ctx, groups)
2 years ago
}
2 years ago
func (g *GroupDataBase) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error {
2 years ago
return g.groupDB.DB.Transaction(func(tx *gorm.DB) error {
if err := g.groupDB.Delete(ctx, groupIDs, tx); err != nil {
2 years ago
return err
}
2 years ago
if err := g.cache.DelGroupsInfo(ctx, groupIDs); err != nil {
2 years ago
return err
}
return nil
})
}
2 years ago
func (g *GroupDataBase) TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error) {
2 years ago
return g.cache.GetGroupInfo(ctx, groupID)
2 years ago
}
func (g *GroupDataBase) Update(ctx context.Context, groups []*relation.Group) error {
2 years ago
return g.db.Transaction(func(tx *gorm.DB) error {
2 years ago
if err := g.groupDB.Update(ctx, groups, tx); err != nil {
2 years ago
return err
}
var groupIDs []string
for _, group := range groups {
groupIDs = append(groupIDs, group.GroupID)
}
2 years ago
if err := g.cache.DelGroupsInfo(ctx, groupIDs); err != nil {
2 years ago
return err
}
return nil
})
}
2 years ago
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string, memberNumCount int) error {
2 years ago
sess, err := g.mongoDB.MgoClient.StartSession()
if err != nil {
return err
}
defer sess.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, sess)
if err = g.mongoDB.CreateSuperGroup(sCtx, groupID, initMemberIDList, memberNumCount); err != nil {
_ = sess.AbortTransaction(ctx)
return err
}
if err = g.cache.DelJoinedSuperGroupIDs(ctx, initMemberIDList); err != nil {
_ = sess.AbortTransaction(ctx)
return err
}
return sess.CommitTransaction(ctx)
2 years ago
}
2 years ago
func (g *GroupDataBase) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
2 years ago
return g.mongoDB.GetSuperGroup(ctx, groupID)
}
2 years ago
func (g *GroupDataBase) CreateGroupAndMember(ctx context.Context, groups []*relation.Group, groupMember []*relation.GroupMember) error {
return g.db.Transaction(func(tx *gorm.DB) error {
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
return err
}
if err := g.groupMemberDB.Create(ctx, groupMember, tx); err != nil {
return err
}
return nil
})
}