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

114 lines
3.4 KiB

2 years ago
package unrelation
import (
2 years ago
"context"
"fmt"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
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"
"strings"
3 years ago
"time"
)
2 years ago
type Mongo struct {
db *mongo.Client
}
2 years ago
func NewMongo() (*Mongo, error) {
2 years ago
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
2 years ago
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
2 years ago
}
2 years ago
func (m *Mongo) GetClient() *mongo.Client {
2 years ago
return m.db
2 years ago
}
2 years ago
func (m *Mongo) GetDatabase() *mongo.Database {
return m.db.Database(config.Config.Mongo.DBDatabase)
}
func (m *Mongo) CreateMsgIndex() error {
2 years ago
return m.createMongoIndex(unrelation.Msg, false, "uid")
2 years ago
}
2 years ago
func (m *Mongo) CreateSuperGroupIndex() error {
2 years ago
if err := m.createMongoIndex(unrelation.CSuperGroup, true, "group_id"); err != nil {
2 years ago
return err
2 years ago
}
2 years ago
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
2 years ago
return err
2 years ago
}
2 years ago
return nil
2 years ago
}
2 years ago
func (m *Mongo) CreateExtendMsgSetIndex() error {
return m.createMongoIndex(unrelation.CExtendMsgSet, true, "-create_time", "work_moment_id")
}
3 years ago
2 years ago
func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...string) error {
2 years ago
db := m.db.Database(config.Config.Mongo.DBDatabase).Collection(collection)
3 years ago
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
// 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
}