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/model.go

207 lines
6.1 KiB

package db
import (
"Open_IM/pkg/common/config"
3 years ago
"go.mongodb.org/mongo-driver/bson"
3 years ago
"go.mongodb.org/mongo-driver/x/bsonx"
"strings"
3 years ago
//"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"fmt"
"go.mongodb.org/mongo-driver/mongo/options"
3 years ago
// "context"
// "fmt"
3 years ago
"github.com/garyburd/redigo/redis"
"gopkg.in/mgo.v2"
3 years ago
"time"
3 years ago
"context"
3 years ago
//"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
3 years ago
// "go.mongodb.org/mongo-driver/mongo/options"
)
var DB DataBases
type DataBases struct {
MysqlDB mysqlDB
mgoSession *mgo.Session
redisPool *redis.Pool
3 years ago
mongoClient *mongo.Client
}
func key(dbAddress, dbName string) string {
return dbAddress + "_" + dbName
}
func init() {
3 years ago
//log.NewPrivateLog(constant.LogFileName)
3 years ago
var mongoClient *mongo.Client
var err1 error
3 years ago
//mysql init
3 years ago
initMysqlDB()
// mongo init
3 years ago
// "mongodb://sysop:moon@localhost/records"
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 {
3 years ago
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
3 years ago
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress[0],
3 years ago
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
3 years ago
} else {
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d",
config.Config.Mongo.DBAddress[0], config.Config.Mongo.DBDatabase,
config.Config.Mongo.DBMaxPoolSize)
3 years ago
}
}
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
3 years ago
if err != nil {
3 years ago
fmt.Println(" mongo.Connect failed, try ", utils.GetSelfFuncName(), err.Error(), uri)
time.Sleep(time.Duration(30) * time.Second)
mongoClient, err1 = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err1 != nil {
3 years ago
fmt.Println(" mongo.Connect retry failed, panic", err.Error(), uri)
panic(err1.Error())
}
}
3 years ago
fmt.Println("0", utils.GetSelfFuncName(), "mongo driver client init success: ", uri)
3 years ago
// mongodb create index
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
dataBase := mongoClient.Database(config.Config.Mongo.DBDatabase)
3 years ago
//cSendLogModels := []mongo.IndexModel{
// {
// //Keys: bson.M{"send_id": -1, "send_time": -1},
// Keys: bson.D{
// {Key: "send_id"},
// {Key: "send_time", Value: -1},
// },
3 years ago
// },
3 years ago
//}
//result, err := dataBase.Collection(cSendLog).Indexes().CreateMany(context.Background(), cSendLogModels, opts)
3 years ago
//if err != nil {
3 years ago
// fmt.Println("mongodb create cSendLogModels failed", result, err.Error())
3 years ago
//}
3 years ago
if err := createUniqueIndex(mongoClient, cSendLog, false, "send_id", "-send_time"); err != nil {
fmt.Println("send_id", "-send_time", "create failed", err.Error())
}
var result string
3 years ago
cChatModels := []mongo.IndexModel{
{
3 years ago
Keys: bson.M{"uid": -1},
Options: options.Index().SetUnique(true),
3 years ago
},
}
3 years ago
_, err = dataBase.Collection(cChat).Indexes().CreateMany(context.Background(), cChatModels, opts)
3 years ago
if err != nil {
fmt.Println("mongodb create cChatModels failed", result, err.Error())
}
3 years ago
_, err = dataBase.Collection(cWorkMoment).Indexes().CreateOne(context.Background(), mongo.IndexModel{
3 years ago
Keys: bson.D{
{Key: "create_time", Value: -1},
{Key: "work_moment_id"},
3 years ago
},
3 years ago
}, opts)
3 years ago
if err != nil {
3 years ago
fmt.Println("mongodb create cWorkMoment index failed", result, err.Error())
3 years ago
}
3 years ago
_, err = dataBase.Collection(cWorkMoment).Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{
{Key: "work_moment_id"},
},
Options: options.Index().SetUnique(true),
}, opts)
3 years ago
if err != nil {
3 years ago
fmt.Println("mongodb create cWorkMoment index failed", err.Error())
3 years ago
}
3 years ago
_, err = dataBase.Collection(cWorkMoment).Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{
{Key: "user_id"},
{Key: "create_time", Value: -1},
3 years ago
},
3 years ago
}, opts)
if err != nil {
fmt.Println("mongodb create cWorkMoment index failed", err.Error())
3 years ago
}
3 years ago
3 years ago
_, err = dataBase.Collection(cTag).Indexes().CreateOne(context.Background(), mongo.IndexModel{
3 years ago
Keys: bson.D{
{Key: "user_id"},
{Key: "create_time", Value: -1},
3 years ago
},
3 years ago
}, opts)
3 years ago
if err != nil {
3 years ago
fmt.Println("mongodb create cSendLog index failed", err.Error())
3 years ago
}
3 years ago
_, err = dataBase.Collection(cTag).Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{
{Key: "tag_id"},
},
Options: options.Index().SetUnique(true),
})
3 years ago
if err != nil {
3 years ago
fmt.Println("mongodb create cSendLog index failed", result, err.Error())
3 years ago
}
3 years ago
3 years ago
DB.mongoClient = mongoClient
3 years ago
3 years ago
// redis pool init
DB.redisPool = &redis.Pool{
MaxIdle: config.Config.Redis.DBMaxIdle,
MaxActive: config.Config.Redis.DBMaxActive,
IdleTimeout: time.Duration(config.Config.Redis.DBIdleTimeout) * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial(
"tcp",
config.Config.Redis.DBAddress,
redis.DialReadTimeout(time.Duration(1000)*time.Millisecond),
redis.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
redis.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
redis.DialDatabase(0),
redis.DialPassword(config.Config.Redis.DBPassWord),
)
},
}
}
3 years ago
func createUniqueIndex(client *mongo.Client, collection string, isUnique bool, keys ...string) error {
db := client.Database(config.Config.Mongo.DBDatabase).Collection(collection)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := db.Indexes()
keysDoc := bsonx.Doc{}
// 复合索引
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))
}
}
// 创建索引
index := mongo.IndexModel{
Keys: keysDoc,
}
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
}