2023-01-28 13:19:36 +08:00
|
|
|
package unrelation
|
2021-05-26 19:17:51 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-02-23 19:15:30 +08:00
|
|
|
"OpenIM/pkg/common/config"
|
|
|
|
|
"OpenIM/pkg/common/db/table/unrelation"
|
|
|
|
|
"OpenIM/pkg/utils"
|
2023-01-16 20:14:26 +08:00
|
|
|
"context"
|
2022-02-23 12:27:01 +08:00
|
|
|
"fmt"
|
2023-01-16 20:14:26 +08:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2022-02-23 12:27:01 +08:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2023-01-16 20:14:26 +08:00
|
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
|
|
|
"strings"
|
2021-06-28 15:32:26 +08:00
|
|
|
"time"
|
2021-05-26 19:17:51 +08:00
|
|
|
)
|
|
|
|
|
|
2023-02-14 16:33:18 +08:00
|
|
|
//func NewMongo() *Mongo {
|
|
|
|
|
// mgo := &Mongo{}
|
|
|
|
|
// mgo.InitMongo()
|
|
|
|
|
// return mgo
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
func NewMongo() (*Mongo, error) {
|
|
|
|
|
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
|
|
|
|
if config.Config.Mongo.DBUri != "" {
|
|
|
|
|
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
|
|
|
|
|
uri = config.Config.Mongo.DBUri
|
|
|
|
|
} else {
|
|
|
|
|
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
|
|
|
|
|
mongodbHosts := ""
|
|
|
|
|
for i, v := range config.Config.Mongo.DBAddress {
|
|
|
|
|
if i == len(config.Config.Mongo.DBAddress)-1 {
|
|
|
|
|
mongodbHosts += v
|
|
|
|
|
} else {
|
|
|
|
|
mongodbHosts += v + ","
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
|
|
|
|
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
|
|
|
|
|
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
|
|
|
|
} else {
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
|
|
|
mongodbHosts, config.Config.Mongo.DBDatabase,
|
|
|
|
|
config.Config.Mongo.DBMaxPoolSize)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("mongo:", uri)
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
|
|
|
|
defer cancel()
|
|
|
|
|
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &Mongo{db: mongoClient}, nil
|
2023-02-13 17:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 15:55:04 +08:00
|
|
|
type Mongo struct {
|
2023-02-02 16:11:24 +08:00
|
|
|
db *mongo.Client
|
2023-01-17 16:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 15:55:04 +08:00
|
|
|
func (m *Mongo) InitMongo() {
|
2022-02-23 14:30:21 +08:00
|
|
|
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
2022-03-21 11:13:17 +08:00
|
|
|
if config.Config.Mongo.DBUri != "" {
|
|
|
|
|
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
|
|
|
|
|
uri = config.Config.Mongo.DBUri
|
|
|
|
|
} else {
|
2022-09-16 15:06:49 +08:00
|
|
|
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
|
|
|
|
|
mongodbHosts := ""
|
|
|
|
|
for i, v := range config.Config.Mongo.DBAddress {
|
|
|
|
|
if i == len(config.Config.Mongo.DBAddress)-1 {
|
|
|
|
|
mongodbHosts += v
|
|
|
|
|
} else {
|
|
|
|
|
mongodbHosts += v + ","
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-15 11:56:36 +08:00
|
|
|
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
|
2022-09-16 15:06:49 +08:00
|
|
|
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017,localhost:27018/?replicaSet=replset")
|
|
|
|
|
//mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
|
2022-09-16 15:14:02 +08:00
|
|
|
//uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin&replicaSet=replset",
|
2022-09-16 16:29:37 +08:00
|
|
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
2022-09-16 15:06:49 +08:00
|
|
|
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
|
2022-04-15 11:56:36 +08:00
|
|
|
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
2022-04-15 17:43:04 +08:00
|
|
|
} else {
|
2022-09-16 15:54:26 +08:00
|
|
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
2022-09-16 15:06:49 +08:00
|
|
|
mongodbHosts, config.Config.Mongo.DBDatabase,
|
2022-04-15 17:43:04 +08:00
|
|
|
config.Config.Mongo.DBMaxPoolSize)
|
2022-04-15 11:56:36 +08:00
|
|
|
}
|
2022-03-21 11:13:17 +08:00
|
|
|
}
|
2023-01-29 11:30:12 +08:00
|
|
|
fmt.Println(utils.GetFuncName(1), "start to init mongoDB:", uri)
|
2022-02-23 12:27:01 +08:00
|
|
|
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
2022-03-31 11:28:51 +08:00
|
|
|
if err != nil {
|
2022-02-23 12:27:01 +08:00
|
|
|
time.Sleep(time.Duration(30) * time.Second)
|
2023-01-16 20:14:26 +08:00
|
|
|
mongoClient, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error() + " mongo.Connect failed " + uri)
|
2022-02-23 12:27:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-02 16:11:24 +08:00
|
|
|
m.db = mongoClient
|
2023-01-17 17:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-29 11:30:12 +08:00
|
|
|
func (m *Mongo) GetClient() *mongo.Client {
|
2023-02-02 16:11:24 +08:00
|
|
|
return m.db
|
2023-01-29 11:30:12 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 15:55:04 +08:00
|
|
|
func (m *Mongo) CreateMsgIndex() {
|
2023-02-15 15:52:32 +08:00
|
|
|
if err := m.createMongoIndex(unrelation.CChat, false, "uid"); err != nil {
|
2023-02-10 15:46:29 +08:00
|
|
|
fmt.Println(err.Error() + " index create failed " + unrelation.CChat + " uid, please create index by yourself in field uid")
|
2022-04-18 19:24:36 +08:00
|
|
|
}
|
2023-01-28 15:55:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Mongo) CreateSuperGroupIndex() {
|
2023-02-03 12:16:48 +08:00
|
|
|
if err := m.createMongoIndex(unrelation.CSuperGroup, true, "group_id"); err != nil {
|
2023-02-15 15:52:32 +08:00
|
|
|
panic(err.Error() + "index create failed " + unrelation.CSuperGroup + " group_id")
|
2023-01-28 15:55:04 +08:00
|
|
|
}
|
2023-02-03 12:16:48 +08:00
|
|
|
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
|
2023-02-15 15:52:32 +08:00
|
|
|
panic(err.Error() + "index create failed " + unrelation.CUserToSuperGroup + "user_id")
|
2023-01-28 15:55:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 16:11:24 +08:00
|
|
|
func (m *Mongo) CreateExtendMsgSetIndex() {
|
2023-02-03 12:16:48 +08:00
|
|
|
if err := m.createMongoIndex(unrelation.CExtendMsgSet, true, "-create_time", "work_moment_id"); err != nil {
|
2023-02-10 18:13:58 +08:00
|
|
|
panic(err.Error() + "index create failed " + unrelation.CExtendMsgSet + " -create_time, work_moment_id")
|
2022-04-18 19:24:36 +08:00
|
|
|
}
|
2021-05-26 19:17:51 +08:00
|
|
|
}
|
2022-05-06 17:23:37 +08:00
|
|
|
|
2023-01-28 15:55:04 +08:00
|
|
|
func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...string) error {
|
2023-02-02 16:11:24 +08:00
|
|
|
db := m.db.Database(config.Config.Mongo.DBDatabase).Collection(collection)
|
2022-05-06 17:23:37 +08:00
|
|
|
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
|
|
|
|
|
indexView := db.Indexes()
|
|
|
|
|
keysDoc := bsonx.Doc{}
|
2023-01-16 20:14:26 +08:00
|
|
|
// create composite indexes
|
2022-05-06 17:23:37 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
|
if strings.HasPrefix(key, "-") {
|
|
|
|
|
keysDoc = keysDoc.Append(strings.TrimLeft(key, "-"), bsonx.Int32(-1))
|
|
|
|
|
} else {
|
|
|
|
|
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-16 20:14:26 +08:00
|
|
|
// create index
|
2022-09-30 18:58:19 +08:00
|
|
|
index := mongo.IndexModel{
|
|
|
|
|
Keys: keysDoc,
|
|
|
|
|
}
|
2022-05-06 17:23:37 +08:00
|
|
|
if isUnique == true {
|
|
|
|
|
index.Options = options.Index().SetUnique(true)
|
|
|
|
|
}
|
|
|
|
|
result, err := indexView.CreateOne(
|
|
|
|
|
context.Background(),
|
|
|
|
|
index,
|
|
|
|
|
opts,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return utils.Wrap(err, result)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-02-08 18:29:11 +08:00
|
|
|
|
|
|
|
|
func MongoTransaction(ctx context.Context, mgo *mongo.Client, fn func(ctx mongo.SessionContext) error) error {
|
|
|
|
|
sess, err := mgo.StartSession()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
sCtx := mongo.NewSessionContext(ctx, sess)
|
|
|
|
|
defer sess.EndSession(sCtx)
|
|
|
|
|
if err := fn(sCtx); err != nil {
|
|
|
|
|
_ = sess.AbortTransaction(sCtx)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return utils.Wrap(sess.CommitTransaction(sCtx), "")
|
|
|
|
|
}
|
2023-02-10 11:03:03 +08:00
|
|
|
|
|
|
|
|
func getTxCtx(ctx context.Context, tx []any) context.Context {
|
|
|
|
|
if len(tx) > 0 {
|
|
|
|
|
if ctx, ok := tx[0].(mongo.SessionContext); ok {
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ctx
|
|
|
|
|
}
|