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/unrelation/init_mongo.go

127 lines
4.4 KiB

2 years ago
package unrelation
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/utils"
2 years ago
"context"
"fmt"
2 years ago
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2 years ago
"go.mongodb.org/mongo-driver/x/bsonx"
"log"
"strings"
3 years ago
"time"
)
2 years ago
type Client struct {
mongo *mongo.Client
}
func NewMongoClient(mdb *mongo.Client) *Client {
return &Client{mongo: mdb}
2 years ago
}
2 years ago
func initMongo() *mongo.Database {
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 + ","
}
}
3 years ago
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017,localhost:27018/?replicaSet=replset")
//mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
//uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin&replicaSet=replset",
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
3 years ago
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
3 years ago
} else {
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
mongodbHosts, config.Config.Mongo.DBDatabase,
3 years ago
config.Config.Mongo.DBMaxPoolSize)
3 years ago
}
}
2 years ago
log.Println("start to init mongoDB:", uri)
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
3 years ago
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
2 years ago
mongoClient, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err.Error() + " mongo.Connect failed " + uri)
}
}
2 years ago
return mongoClient.Database(config.Config.Mongo.DBDatabase)
2 years ago
}
2 years ago
func GetCollection(mongoClient *mongo.Client) {
}
2 years ago
func CreateAllIndex(mongoClient *mongo.Client) {
3 years ago
// mongodb create index
3 years ago
if err := createMongoIndex(mongoClient, cSendLog, false, "send_id", "-send_time"); err != nil {
panic(err.Error() + " index create failed " + cSendLog + " send_id, -send_time")
3 years ago
}
if err := createMongoIndex(mongoClient, cChat, false, "uid"); err != nil {
2 years ago
fmt.Println(err.Error() + " index create failed " + cChat + " uid, please create index by yourself in field uid")
3 years ago
}
3 years ago
if err := createMongoIndex(mongoClient, cWorkMoment, true, "-create_time", "work_moment_id"); err != nil {
panic(err.Error() + "index create failed " + cWorkMoment + " -create_time, work_moment_id")
3 years ago
}
3 years ago
if err := createMongoIndex(mongoClient, cWorkMoment, true, "work_moment_id"); err != nil {
panic(err.Error() + "index create failed " + cWorkMoment + " work_moment_id ")
3 years ago
}
3 years ago
if err := createMongoIndex(mongoClient, cWorkMoment, false, "user_id", "-create_time"); err != nil {
panic(err.Error() + "index create failed " + cWorkMoment + "user_id, -create_time")
3 years ago
}
3 years ago
if err := createMongoIndex(mongoClient, cTag, false, "user_id", "-create_time"); err != nil {
panic(err.Error() + "index create failed " + cTag + " user_id, -create_time")
3 years ago
}
3 years ago
if err := createMongoIndex(mongoClient, cTag, true, "tag_id"); err != nil {
panic(err.Error() + "index create failed " + cTag + " tag_id")
3 years ago
}
}
3 years ago
3 years ago
func createMongoIndex(client *mongo.Client, collection string, isUnique bool, keys ...string) error {
3 years ago
db := client.Database(config.Config.Mongo.DBDatabase).Collection(collection)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := db.Indexes()
keysDoc := bsonx.Doc{}
2 years ago
// create composite indexes
3 years ago
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))
}
}
2 years ago
2 years ago
// create index
2 years ago
index := mongo.IndexModel{
Keys: keysDoc,
}
3 years ago
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
}