mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-06 01:55:58 +08:00
errcode
This commit is contained in:
@@ -1,171 +0,0 @@
|
||||
package unrelation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/log"
|
||||
promePkg "Open_IM/pkg/common/prometheus"
|
||||
pbMsg "Open_IM/pkg/proto/msg"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
go_redis "github.com/go-redis/redis/v8"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func (d *db.DataBases) BatchInsertChat2DB(userID string, msgList []*pbMsg.MsgDataToMQ, operationID string, currentMaxSeq uint64) error {
|
||||
newTime := getCurrentTimestampByMill()
|
||||
if len(msgList) > GetSingleGocMsgNum() {
|
||||
return errors.New("too large")
|
||||
}
|
||||
isInit := false
|
||||
var remain uint64
|
||||
blk0 := uint64(GetSingleGocMsgNum() - 1)
|
||||
//currentMaxSeq 4998
|
||||
if currentMaxSeq < uint64(mongo2.GetSingleGocMsgNum()) {
|
||||
remain = blk0 - currentMaxSeq //1
|
||||
} else {
|
||||
excludeBlk0 := currentMaxSeq - blk0 //=1
|
||||
//(5000-1)%5000 == 4999
|
||||
remain = (uint64(mongo2.GetSingleGocMsgNum()) - (excludeBlk0 % uint64(mongo2.GetSingleGocMsgNum()))) % uint64(mongo2.GetSingleGocMsgNum())
|
||||
}
|
||||
//remain=1
|
||||
insertCounter := uint64(0)
|
||||
msgListToMongo := make([]mongo2.MsgInfo, 0)
|
||||
msgListToMongoNext := make([]mongo2.MsgInfo, 0)
|
||||
seqUid := ""
|
||||
seqUidNext := ""
|
||||
log.Debug(operationID, "remain ", remain, "insertCounter ", insertCounter, "currentMaxSeq ", currentMaxSeq, userID, len(msgList))
|
||||
var err error
|
||||
for _, m := range msgList {
|
||||
log.Debug(operationID, "msg node ", m.String(), m.MsgData.ClientMsgID)
|
||||
currentMaxSeq++
|
||||
sMsg := mongo2.MsgInfo{}
|
||||
sMsg.SendTime = m.MsgData.SendTime
|
||||
m.MsgData.Seq = uint32(currentMaxSeq)
|
||||
log.Debug(operationID, "mongo msg node ", m.String(), m.MsgData.ClientMsgID, "userID: ", userID, "seq: ", currentMaxSeq)
|
||||
if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
if isInit {
|
||||
msgListToMongoNext = append(msgListToMongoNext, sMsg)
|
||||
seqUidNext = mongo2.getSeqUid(userID, uint32(currentMaxSeq))
|
||||
log.Debug(operationID, "msgListToMongoNext ", seqUidNext, m.MsgData.Seq, m.MsgData.ClientMsgID, insertCounter, remain)
|
||||
continue
|
||||
}
|
||||
if insertCounter < remain {
|
||||
msgListToMongo = append(msgListToMongo, sMsg)
|
||||
insertCounter++
|
||||
seqUid = mongo2.getSeqUid(userID, uint32(currentMaxSeq))
|
||||
log.Debug(operationID, "msgListToMongo ", seqUid, m.MsgData.Seq, m.MsgData.ClientMsgID, insertCounter, remain, "userID: ", userID)
|
||||
} else {
|
||||
msgListToMongoNext = append(msgListToMongoNext, sMsg)
|
||||
seqUidNext = mongo2.getSeqUid(userID, uint32(currentMaxSeq))
|
||||
log.Debug(operationID, "msgListToMongoNext ", seqUidNext, m.MsgData.Seq, m.MsgData.ClientMsgID, insertCounter, remain, "userID: ", userID)
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(mongo2.cChat)
|
||||
|
||||
if seqUid != "" {
|
||||
filter := bson.M{"uid": seqUid}
|
||||
log.NewDebug(operationID, "filter ", seqUid, "list ", msgListToMongo, "userID: ", userID)
|
||||
err := c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": bson.M{"$each": msgListToMongo}}}).Err()
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
filter := bson.M{"uid": seqUid}
|
||||
sChat := mongo2.UserChat{}
|
||||
sChat.UID = seqUid
|
||||
sChat.Msg = msgListToMongo
|
||||
log.NewDebug(operationID, "filter ", seqUid, "list ", msgListToMongo)
|
||||
if _, err = c.InsertOne(ctx, &sChat); err != nil {
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoFailedCounter)
|
||||
log.NewError(operationID, "InsertOne failed", filter, err.Error(), sChat)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoSuccessCounter)
|
||||
} else {
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoFailedCounter)
|
||||
log.Error(operationID, "FindOneAndUpdate failed ", err.Error(), filter)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
} else {
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoSuccessCounter)
|
||||
}
|
||||
}
|
||||
if seqUidNext != "" {
|
||||
filter := bson.M{"uid": seqUidNext}
|
||||
sChat := mongo2.UserChat{}
|
||||
sChat.UID = seqUidNext
|
||||
sChat.Msg = msgListToMongoNext
|
||||
log.NewDebug(operationID, "filter ", seqUidNext, "list ", msgListToMongoNext, "userID: ", userID)
|
||||
if _, err = c.InsertOne(ctx, &sChat); err != nil {
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoFailedCounter)
|
||||
log.NewError(operationID, "InsertOne failed", filter, err.Error(), sChat)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
promePkg.PromeInc(promePkg.MsgInsertMongoSuccessCounter)
|
||||
}
|
||||
log.Debug(operationID, "batch mgo cost time ", mongo2.getCurrentTimestampByMill()-newTime, userID, len(msgList))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) BatchInsertChat2Cache(insertID string, msgList []*pbMsg.MsgDataToMQ, operationID string) (error, uint64) {
|
||||
newTime := mongo2.getCurrentTimestampByMill()
|
||||
lenList := len(msgList)
|
||||
if lenList > mongo2.GetSingleGocMsgNum() {
|
||||
return errors.New("too large"), 0
|
||||
}
|
||||
if lenList < 1 {
|
||||
return errors.New("too short as 0"), 0
|
||||
}
|
||||
// judge sessionType to get seq
|
||||
var currentMaxSeq uint64
|
||||
var err error
|
||||
if msgList[0].MsgData.SessionType == constant.SuperGroupChatType {
|
||||
currentMaxSeq, err = d.GetGroupMaxSeq(insertID)
|
||||
log.Debug(operationID, "constant.SuperGroupChatType lastMaxSeq before add ", currentMaxSeq, "userID ", insertID, err)
|
||||
} else {
|
||||
currentMaxSeq, err = d.GetUserMaxSeq(insertID)
|
||||
log.Debug(operationID, "constant.SingleChatType lastMaxSeq before add ", currentMaxSeq, "userID ", insertID, err)
|
||||
}
|
||||
if err != nil && err != go_redis.Nil {
|
||||
promePkg.PromeInc(promePkg.SeqGetFailedCounter)
|
||||
return utils.Wrap(err, ""), 0
|
||||
}
|
||||
promePkg.PromeInc(promePkg.SeqGetSuccessCounter)
|
||||
|
||||
lastMaxSeq := currentMaxSeq
|
||||
for _, m := range msgList {
|
||||
|
||||
currentMaxSeq++
|
||||
sMsg := mongo2.MsgInfo{}
|
||||
sMsg.SendTime = m.MsgData.SendTime
|
||||
m.MsgData.Seq = uint32(currentMaxSeq)
|
||||
log.Debug(operationID, "cache msg node ", m.String(), m.MsgData.ClientMsgID, "userID: ", insertID, "seq: ", currentMaxSeq)
|
||||
}
|
||||
log.Debug(operationID, "SetMessageToCache ", insertID, len(msgList))
|
||||
err, failedNum := d.SetMessageToCache(msgList, insertID, operationID)
|
||||
if err != nil {
|
||||
promePkg.PromeAdd(promePkg.MsgInsertRedisFailedCounter, failedNum)
|
||||
log.Error(operationID, "setMessageToCache failed, continue ", err.Error(), len(msgList), insertID)
|
||||
} else {
|
||||
promePkg.PromeInc(promePkg.MsgInsertRedisSuccessCounter)
|
||||
}
|
||||
log.Debug(operationID, "batch to redis cost time ", mongo2.getCurrentTimestampByMill()-newTime, insertID, len(msgList))
|
||||
if msgList[0].MsgData.SessionType == constant.SuperGroupChatType {
|
||||
err = d.SetGroupMaxSeq(insertID, currentMaxSeq)
|
||||
} else {
|
||||
err = d.SetUserMaxSeq(insertID, currentMaxSeq)
|
||||
}
|
||||
if err != nil {
|
||||
promePkg.PromeInc(promePkg.SeqSetFailedCounter)
|
||||
} else {
|
||||
promePkg.PromeInc(promePkg.SeqSetSuccessCounter)
|
||||
}
|
||||
return utils.Wrap(err, ""), lastMaxSeq
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package unrelation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/unrelation"
|
||||
unRelationTb "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/proto/sdkws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@@ -19,19 +19,15 @@ type ExtendMsgSetMongoDriver struct {
|
||||
}
|
||||
|
||||
func NewExtendMsgSetMongoDriver(mgoDB *mongo.Database) *ExtendMsgSetMongoDriver {
|
||||
return &ExtendMsgSetMongoDriver{mgoDB: mgoDB, ExtendMsgSetCollection: mgoDB.Collection(unrelation.CExtendMsgSet)}
|
||||
return &ExtendMsgSetMongoDriver{mgoDB: mgoDB, ExtendMsgSetCollection: mgoDB.Collection(unRelationTb.CExtendMsgSet)}
|
||||
}
|
||||
|
||||
func (e *ExtendMsgSetMongoDriver) CreateExtendMsgSet(ctx context.Context, set *unrelation.ExtendMsgSet) error {
|
||||
func (e *ExtendMsgSetMongoDriver) CreateExtendMsgSet(ctx context.Context, set *unRelationTb.ExtendMsgSetModel) error {
|
||||
_, err := e.ExtendMsgSetCollection.InsertOne(ctx, set)
|
||||
return err
|
||||
}
|
||||
|
||||
type GetAllExtendMsgSetOpts struct {
|
||||
ExcludeExtendMsgs bool
|
||||
}
|
||||
|
||||
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID string, opts *GetAllExtendMsgSetOpts) (sets []*unrelation.ExtendMsgSet, err error) {
|
||||
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID string, opts *unRelationTb.GetAllExtendMsgSetOpts) (sets []*unRelationTb.ExtendMsgSetModel, err error) {
|
||||
regex := fmt.Sprintf("^%s", ID)
|
||||
var findOpts *options.FindOptions
|
||||
if opts != nil {
|
||||
@@ -51,7 +47,7 @@ func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID str
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, maxMsgUpdateTime int64) (*unrelation.ExtendMsgSet, error) {
|
||||
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, maxMsgUpdateTime int64) (*unRelationTb.ExtendMsgSetModel, error) {
|
||||
var err error
|
||||
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{"extend_msgs": 0})
|
||||
// update newest
|
||||
@@ -63,7 +59,7 @@ func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, sourceID
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var setList []unrelation.ExtendMsgSet
|
||||
var setList []unRelationTb.ExtendMsgSetModel
|
||||
if err := result.All(ctx, &setList); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
@@ -74,7 +70,7 @@ func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, sourceID
|
||||
}
|
||||
|
||||
// first modify msg
|
||||
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, sourceID string, sessionType int32, msg *unrelation.ExtendMsg) error {
|
||||
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, sourceID string, sessionType int32, msg *unRelationTb.ExtendMsgModel) error {
|
||||
set, err := e.GetExtendMsgSet(ctx, sourceID, sessionType, 0)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
@@ -84,10 +80,10 @@ func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, sourceID
|
||||
if set != nil {
|
||||
index = set.SplitSourceIDAndGetIndex()
|
||||
}
|
||||
err = e.CreateExtendMsgSet(ctx, &unrelation.ExtendMsgSet{
|
||||
err = e.CreateExtendMsgSet(ctx, &unRelationTb.ExtendMsgSetModel{
|
||||
SourceID: set.GetSourceID(sourceID, index),
|
||||
SessionType: sessionType,
|
||||
ExtendMsgs: map[string]unrelation.ExtendMsg{msg.ClientMsgID: *msg},
|
||||
ExtendMsgs: map[string]unRelationTb.ExtendMsgModel{msg.ClientMsgID: *msg},
|
||||
ExtendMsgNum: 1,
|
||||
CreateTime: msg.MsgFirstModifyTime,
|
||||
MaxMsgUpdateTime: msg.MsgFirstModifyTime,
|
||||
@@ -136,14 +132,14 @@ func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(ctx context.Context
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *ExtendMsgSetMongoDriver) GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *unrelation.ExtendMsg, err error) {
|
||||
func (e *ExtendMsgSetMongoDriver) GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *unRelationTb.ExtendMsgModel, err error) {
|
||||
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{fmt.Sprintf("extend_msgs.%s", clientMsgID): 1})
|
||||
regex := fmt.Sprintf("^%s", sourceID)
|
||||
result, err := e.ExtendMsgSetCollection.Find(ctx, bson.M{"source_id": primitive.Regex{Pattern: regex}, "session_type": sessionType, "max_msg_update_time": bson.M{"$lte": maxMsgUpdateTime}}, findOpts)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var setList []unrelation.ExtendMsgSet
|
||||
var setList []unRelationTb.ExtendMsgSetModel
|
||||
if err := result.All(ctx, &setList); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
@@ -62,17 +62,17 @@ func (m *Mongo) GetClient() *mongo.Client {
|
||||
}
|
||||
|
||||
func (m *Mongo) CreateMsgIndex() {
|
||||
if err := m.createMongoIndex(unrelation, false, "uid"); err != nil {
|
||||
if err := m.createMongoIndex(unrelation.CChat, false, "uid"); err != nil {
|
||||
fmt.Println(err.Error() + " index create failed " + unrelation.CChat + " uid, please create index by yourself in field uid")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Mongo) CreateSuperGroupIndex() {
|
||||
if err := m.createMongoIndex(unrelation.CSuperGroup, true, "group_id"); err != nil {
|
||||
panic(err.Error() + "index create failed " + unrelation.CTag + " group_id")
|
||||
panic(err.Error() + "index create failed " + unrelation.CSuperGroup + " group_id")
|
||||
}
|
||||
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
|
||||
panic(err.Error() + "index create failed " + unrelation.CTag + "user_id")
|
||||
panic(err.Error() + "index create failed " + unrelation.CUserToSuperGroup + "user_id")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,670 +0,0 @@
|
||||
package unrelation
|
||||
|
||||
|
||||
// deleteMsgByLogic
|
||||
//func (d *db.DataBases) DelMsgBySeqList(userID string, seqList []uint32, operationID string) (totalUnexistSeqList []uint32, err error) {
|
||||
// log.Debug(operationID, utils.GetSelfFuncName(), "args ", userID, seqList)
|
||||
// sortkeys.Uint32s(seqList)
|
||||
// suffixUserID2SubSeqList := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
// t := make(map[string][]uint32)
|
||||
// for i := 0; i < len(seqList); i++ {
|
||||
// seqUid := getSeqUid(uid, seqList[i])
|
||||
// if value, ok := t[seqUid]; !ok {
|
||||
// var temp []uint32
|
||||
// t[seqUid] = append(temp, seqList[i])
|
||||
// } else {
|
||||
// t[seqUid] = append(value, seqList[i])
|
||||
// }
|
||||
// }
|
||||
// return t
|
||||
// }(userID, seqList)
|
||||
//
|
||||
// lock := sync.Mutex{}
|
||||
// var wg sync.WaitGroup
|
||||
// wg.Add(len(suffixUserID2SubSeqList))
|
||||
// for k, v := range suffixUserID2SubSeqList {
|
||||
// go func(suffixUserID string, subSeqList []uint32, operationID string) {
|
||||
// defer wg.Done()
|
||||
// unexistSeqList, err := d.DelMsgBySeqListInOneDoc(suffixUserID, subSeqList, operationID)
|
||||
// if err != nil {
|
||||
// log.Error(operationID, "DelMsgBySeqListInOneDoc failed ", err.Error(), suffixUserID, subSeqList)
|
||||
// return
|
||||
// }
|
||||
// lock.Lock()
|
||||
// totalUnexistSeqList = append(totalUnexistSeqList, unexistSeqList...)
|
||||
// lock.Unlock()
|
||||
// }(k, v, operationID)
|
||||
// }
|
||||
// return totalUnexistSeqList, err
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) DelMsgBySeqListInOneDoc(suffixUserID string, seqList []uint32, operationID string) ([]uint32, error) {
|
||||
// log.Debug(operationID, utils.GetSelfFuncName(), "args ", suffixUserID, seqList)
|
||||
// seqMsgList, indexList, unexistSeqList, err := d.GetMsgAndIndexBySeqListInOneMongo2(suffixUserID, seqList, operationID)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// for i, v := range seqMsgList {
|
||||
// if err := d.ReplaceMsgByIndex(suffixUserID, v, operationID, indexList[i]); err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// }
|
||||
// return unexistSeqList, nil
|
||||
//}
|
||||
|
||||
// deleteMsgByLogic
|
||||
//func (d *db.DataBases) DelMsgLogic(uid string, seqList []uint32, operationID string) error {
|
||||
// sortkeys.Uint32s(seqList)
|
||||
// seqMsgs, err := d.GetMsgBySeqListMongo2(uid, seqList, operationID)
|
||||
// if err != nil {
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// for _, seqMsg := range seqMsgs {
|
||||
// log.NewDebug(operationID, utils.GetSelfFuncName(), *seqMsg)
|
||||
// seqMsg.Status = constant.MsgDeleted
|
||||
// if err = d.ReplaceMsgBySeq(uid, seqMsg, operationID); err != nil {
|
||||
// log.NewError(operationID, utils.GetSelfFuncName(), "ReplaceMsgListBySeq error", err.Error())
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
//func (d *db.DataBases) ReplaceMsgByIndex(suffixUserID string, msg *sdkws.MsgData, operationID string, seqIndex int) error {
|
||||
// log.NewInfo(operationID, utils.GetSelfFuncName(), suffixUserID, *msg)
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// s := fmt.Sprintf("msg.%d.msg", seqIndex)
|
||||
// log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
|
||||
// msg.Status = constant.MsgDeleted
|
||||
// bytes, err := proto.Marshal(msg)
|
||||
// if err != nil {
|
||||
// log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal failed ", err.Error(), msg.String())
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// updateResult, err := c.UpdateOne(ctx, bson.M{"uid": suffixUserID}, bson.M{"$set": bson.M{s: bytes}})
|
||||
// log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
|
||||
// if err != nil {
|
||||
// log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
//func (d *db.DataBases) ReplaceMsgBySeq(uid string, msg *sdkws.MsgData, operationID string) error {
|
||||
// log.NewInfo(operationID, utils.GetSelfFuncName(), uid, *msg)
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// uid = getSeqUid(uid, msg.Seq)
|
||||
// seqIndex := getMsgIndex(msg.Seq)
|
||||
// s := fmt.Sprintf("msg.%d.msg", seqIndex)
|
||||
// log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
|
||||
// bytes, err := proto.Marshal(msg)
|
||||
// if err != nil {
|
||||
// log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal", err.Error())
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
//
|
||||
// updateResult, err := c.UpdateOne(
|
||||
// ctx, bson.M{"uid": uid},
|
||||
// bson.M{"$set": bson.M{s: bytes}})
|
||||
// log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
|
||||
// if err != nil {
|
||||
// log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) UpdateOneMsgList(msg *UserChat) error {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// _, err := c.UpdateOne(ctx, bson.M{"uid": msg.UID}, bson.M{"$set": bson.M{"msg": msg.Msg}})
|
||||
// return err
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetMsgBySeqList(uid string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
// log.NewInfo(operationID, utils.GetSelfFuncName(), uid, seqList)
|
||||
// var hasSeqList []uint32
|
||||
// singleCount := 0
|
||||
// session := d.mgoSession.Clone()
|
||||
// if session == nil {
|
||||
// return nil, errors.New("session == nil")
|
||||
// }
|
||||
// defer session.Close()
|
||||
// c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||||
// m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
// t := make(map[string][]uint32)
|
||||
// for i := 0; i < len(seqList); i++ {
|
||||
// seqUid := getSeqUid(uid, seqList[i])
|
||||
// if value, ok := t[seqUid]; !ok {
|
||||
// var temp []uint32
|
||||
// t[seqUid] = append(temp, seqList[i])
|
||||
// } else {
|
||||
// t[seqUid] = append(value, seqList[i])
|
||||
// }
|
||||
// }
|
||||
// return t
|
||||
// }(uid, seqList)
|
||||
// sChat := UserChat{}
|
||||
// for seqUid, value := range m {
|
||||
// if err = c.Find(bson.M{"uid": seqUid}).One(&sChat); err != nil {
|
||||
// log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
|
||||
// continue
|
||||
// }
|
||||
// singleCount = 0
|
||||
// for i := 0; i < len(sChat.Msg); i++ {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
// log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
|
||||
// return nil, err
|
||||
// }
|
||||
// if isContainInt32(msg.Seq, value) {
|
||||
// seqMsg = append(seqMsg, msg)
|
||||
// hasSeqList = append(hasSeqList, msg.Seq)
|
||||
// singleCount++
|
||||
// if singleCount == len(value) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if len(hasSeqList) != len(seqList) {
|
||||
// var diff []uint32
|
||||
// diff = utils.Difference(hasSeqList, seqList)
|
||||
// exceptionMSg := genExceptionMessageBySeqList(diff)
|
||||
// seqMsg = append(seqMsg, exceptionMSg...)
|
||||
//
|
||||
// }
|
||||
// return seqMsg, nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetUserMsgListByIndex(ID string, index int64) (*UserChat, error) {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// regex := fmt.Sprintf("^%s", ID)
|
||||
// findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
|
||||
// var msgs []UserChat
|
||||
// //primitive.Regex{Pattern: regex}
|
||||
// cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// err = cursor.All(context.Background(), &msgs)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
|
||||
// }
|
||||
// if len(msgs) > 0 {
|
||||
// return &msgs[0], nil
|
||||
// } else {
|
||||
// return nil, ErrMsgListNotExist
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) DelMongoMsgs(IDList []string) error {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// _, err := c.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": IDList}})
|
||||
// return err
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) ReplaceMsgToBlankByIndex(suffixID string, index int) (replaceMaxSeq uint32, err error) {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// userChat := &UserChat{}
|
||||
// err = c.FindOne(ctx, bson.M{"uid": suffixID}).Decode(&userChat)
|
||||
// if err != nil {
|
||||
// return 0, err
|
||||
// }
|
||||
// for i, msg := range userChat.Msg {
|
||||
// if i <= index {
|
||||
// msgPb := &sdkws.MsgData{}
|
||||
// if err = proto.Unmarshal(msg.Msg, msgPb); err != nil {
|
||||
// continue
|
||||
// }
|
||||
// newMsgPb := &sdkws.MsgData{Seq: msgPb.Seq}
|
||||
// bytes, err := proto.Marshal(newMsgPb)
|
||||
// if err != nil {
|
||||
// continue
|
||||
// }
|
||||
// msg.Msg = bytes
|
||||
// msg.SendTime = 0
|
||||
// replaceMaxSeq = msgPb.Seq
|
||||
// }
|
||||
// }
|
||||
// _, err = c.UpdateOne(ctx, bson.M{"uid": suffixID}, bson.M{"$set": bson.M{"msg": userChat.Msg}})
|
||||
// return replaceMaxSeq, err
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetNewestMsg(ID string) (msg *sdkws.MsgData, err error) {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// regex := fmt.Sprintf("^%s", ID)
|
||||
// findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": -1})
|
||||
// var userChats []UserChat
|
||||
// cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// err = cursor.All(ctx, &userChats)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// if len(userChats) > 0 {
|
||||
// if len(userChats[0].Msg) > 0 {
|
||||
// msgPb := &sdkws.MsgData{}
|
||||
// err = proto.Unmarshal(userChats[0].Msg[len(userChats[0].Msg)-1].Msg, msgPb)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// return msgPb, nil
|
||||
// }
|
||||
// return nil, errors.New("len(userChats[0].Msg) < 0")
|
||||
// }
|
||||
// return nil, nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetOldestMsg(ID string) (msg *sdkws.MsgData, err error) {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// regex := fmt.Sprintf("^%s", ID)
|
||||
// findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": 1})
|
||||
// var userChats []UserChat
|
||||
// cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// err = cursor.All(ctx, &userChats)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// var oldestMsg []byte
|
||||
// if len(userChats) > 0 {
|
||||
// for _, v := range userChats[0].Msg {
|
||||
// if v.SendTime != 0 {
|
||||
// oldestMsg = v.Msg
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// if len(oldestMsg) == 0 {
|
||||
// oldestMsg = userChats[0].Msg[len(userChats[0].Msg)-1].Msg
|
||||
// }
|
||||
// msgPb := &sdkws.MsgData{}
|
||||
// err = proto.Unmarshal(oldestMsg, msgPb)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// return msgPb, nil
|
||||
// }
|
||||
// return nil, nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetMsgBySeqListMongo2(uid string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
// var hasSeqList []uint32
|
||||
// singleCount := 0
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
//
|
||||
// m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
// t := make(map[string][]uint32)
|
||||
// for i := 0; i < len(seqList); i++ {
|
||||
// seqUid := getSeqUid(uid, seqList[i])
|
||||
// if value, ok := t[seqUid]; !ok {
|
||||
// var temp []uint32
|
||||
// t[seqUid] = append(temp, seqList[i])
|
||||
// } else {
|
||||
// t[seqUid] = append(value, seqList[i])
|
||||
// }
|
||||
// }
|
||||
// return t
|
||||
// }(uid, seqList)
|
||||
// sChat := UserChat{}
|
||||
// for seqUid, value := range m {
|
||||
// if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
|
||||
// log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
|
||||
// continue
|
||||
// }
|
||||
// singleCount = 0
|
||||
// for i := 0; i < len(sChat.Msg); i++ {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
// log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
|
||||
// return nil, err
|
||||
// }
|
||||
// if isContainInt32(msg.Seq, value) {
|
||||
// seqMsg = append(seqMsg, msg)
|
||||
// hasSeqList = append(hasSeqList, msg.Seq)
|
||||
// singleCount++
|
||||
// if singleCount == len(value) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if len(hasSeqList) != len(seqList) {
|
||||
// var diff []uint32
|
||||
// diff = utils.Difference(hasSeqList, seqList)
|
||||
// exceptionMSg := genExceptionMessageBySeqList(diff)
|
||||
// seqMsg = append(seqMsg, exceptionMSg...)
|
||||
//
|
||||
// }
|
||||
// return seqMsg, nil
|
||||
//}
|
||||
//func (d *db.DataBases) GetSuperGroupMsgBySeqListMongo(groupID string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
// var hasSeqList []uint32
|
||||
// singleCount := 0
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
//
|
||||
// m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
// t := make(map[string][]uint32)
|
||||
// for i := 0; i < len(seqList); i++ {
|
||||
// seqUid := getSeqUid(uid, seqList[i])
|
||||
// if value, ok := t[seqUid]; !ok {
|
||||
// var temp []uint32
|
||||
// t[seqUid] = append(temp, seqList[i])
|
||||
// } else {
|
||||
// t[seqUid] = append(value, seqList[i])
|
||||
// }
|
||||
// }
|
||||
// return t
|
||||
// }(groupID, seqList)
|
||||
// sChat := UserChat{}
|
||||
// for seqUid, value := range m {
|
||||
// if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
|
||||
// log.NewError(operationID, "not find seqGroupID", seqUid, value, groupID, seqList, err.Error())
|
||||
// continue
|
||||
// }
|
||||
// singleCount = 0
|
||||
// for i := 0; i < len(sChat.Msg); i++ {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
// log.NewError(operationID, "Unmarshal err", seqUid, value, groupID, seqList, err.Error())
|
||||
// return nil, err
|
||||
// }
|
||||
// if isContainInt32(msg.Seq, value) {
|
||||
// seqMsg = append(seqMsg, msg)
|
||||
// hasSeqList = append(hasSeqList, msg.Seq)
|
||||
// singleCount++
|
||||
// if singleCount == len(value) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if len(hasSeqList) != len(seqList) {
|
||||
// var diff []uint32
|
||||
// diff = utils.Difference(hasSeqList, seqList)
|
||||
// exceptionMSg := genExceptionSuperGroupMessageBySeqList(diff, groupID)
|
||||
// seqMsg = append(seqMsg, exceptionMSg...)
|
||||
//
|
||||
// }
|
||||
// return seqMsg, nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) GetMsgAndIndexBySeqListInOneMongo2(suffixUserID string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, indexList []int, unexistSeqList []uint32, err error) {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// sChat := UserChat{}
|
||||
// if err = c.FindOne(ctx, bson.M{"uid": suffixUserID}).Decode(&sChat); err != nil {
|
||||
// log.NewError(operationID, "not find seqUid", suffixUserID, err.Error())
|
||||
// return nil, nil, nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// singleCount := 0
|
||||
// var hasSeqList []uint32
|
||||
// for i := 0; i < len(sChat.Msg); i++ {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
// log.NewError(operationID, "Unmarshal err", msg.String(), err.Error())
|
||||
// return nil, nil, nil, err
|
||||
// }
|
||||
// if isContainInt32(msg.Seq, seqList) {
|
||||
// indexList = append(indexList, i)
|
||||
// seqMsg = append(seqMsg, msg)
|
||||
// hasSeqList = append(hasSeqList, msg.Seq)
|
||||
// singleCount++
|
||||
// if singleCount == len(seqList) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// for _, i := range seqList {
|
||||
// if isContainInt32(i, hasSeqList) {
|
||||
// continue
|
||||
// }
|
||||
// unexistSeqList = append(unexistSeqList, i)
|
||||
// }
|
||||
// return seqMsg, indexList, unexistSeqList, nil
|
||||
//}
|
||||
//
|
||||
//func genExceptionMessageBySeqList(seqList []uint32) (exceptionMsg []*sdkws.MsgData) {
|
||||
// for _, v := range seqList {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// msg.Seq = v
|
||||
// exceptionMsg = append(exceptionMsg, msg)
|
||||
// }
|
||||
// return exceptionMsg
|
||||
//}
|
||||
//
|
||||
//func genExceptionSuperGroupMessageBySeqList(seqList []uint32, groupID string) (exceptionMsg []*sdkws.MsgData) {
|
||||
// for _, v := range seqList {
|
||||
// msg := new(sdkws.MsgData)
|
||||
// msg.Seq = v
|
||||
// msg.GroupID = groupID
|
||||
// msg.SessionType = constant.SuperGroupChatType
|
||||
// exceptionMsg = append(exceptionMsg, msg)
|
||||
// }
|
||||
// return exceptionMsg
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) SaveUserChatMongo2(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// newTime := getCurrentTimestampByMill()
|
||||
// operationID := ""
|
||||
// seqUid := getSeqUid(uid, m.MsgData.Seq)
|
||||
// filter := bson.M{"uid": seqUid}
|
||||
// var err error
|
||||
// sMsg := MsgInfo{}
|
||||
// sMsg.SendTime = sendTime
|
||||
// if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// err = c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": sMsg}}).Err()
|
||||
// log.NewWarn(operationID, "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
|
||||
// if err != nil {
|
||||
// sChat := UserChat{}
|
||||
// sChat.UID = seqUid
|
||||
// sChat.Msg = append(sChat.Msg, sMsg)
|
||||
// if _, err = c.InsertOne(ctx, &sChat); err != nil {
|
||||
// log.NewDebug(operationID, "InsertOne failed", filter)
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// } else {
|
||||
// log.NewDebug(operationID, "FindOneAndUpdate ok", filter)
|
||||
// }
|
||||
//
|
||||
// log.NewDebug(operationID, "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
|
||||
// return nil
|
||||
//}
|
||||
|
||||
//
|
||||
//func (d *DataBases) SaveUserChatListMongo2(uid string, sendTime int64, msgList []*pbMsg.MsgDataToDB) error {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// newTime := getCurrentTimestampByMill()
|
||||
// operationID := ""
|
||||
// seqUid := ""
|
||||
// msgListToMongo := make([]MsgInfo, 0)
|
||||
//
|
||||
// for _, m := range msgList {
|
||||
// seqUid = getSeqUid(uid, m.MsgData.Seq)
|
||||
// var err error
|
||||
// sMsg := MsgInfo{}
|
||||
// sMsg.SendTime = sendTime
|
||||
// if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// msgListToMongo = append(msgListToMongo, sMsg)
|
||||
// }
|
||||
//
|
||||
// filter := bson.M{"uid": seqUid}
|
||||
// log.NewDebug(operationID, "filter ", seqUid)
|
||||
// err := c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": bson.M{"$each": msgListToMongo}}}).Err()
|
||||
// log.NewWarn(operationID, "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
|
||||
// if err != nil {
|
||||
// sChat := UserChat{}
|
||||
// sChat.UID = seqUid
|
||||
// sChat.Msg = msgListToMongo
|
||||
//
|
||||
// if _, err = c.InsertOne(ctx, &sChat); err != nil {
|
||||
// log.NewError(operationID, "InsertOne failed", filter, err.Error(), sChat)
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// } else {
|
||||
// log.NewDebug(operationID, "FindOneAndUpdate ok", filter)
|
||||
// }
|
||||
//
|
||||
// log.NewDebug(operationID, "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
|
||||
// return nil
|
||||
//}
|
||||
|
||||
//func (d *db.DataBases) SaveUserChat(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
|
||||
// var seqUid string
|
||||
// newTime := getCurrentTimestampByMill()
|
||||
// session := d.mgoSession.Clone()
|
||||
// if session == nil {
|
||||
// return errors.New("session == nil")
|
||||
// }
|
||||
// defer session.Close()
|
||||
// log.NewDebug("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
|
||||
// c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||||
// seqUid = getSeqUid(uid, m.MsgData.Seq)
|
||||
// n, err := c.Find(bson.M{"uid": seqUid}).Count()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// log.NewDebug("", "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
|
||||
// sMsg := MsgInfo{}
|
||||
// sMsg.SendTime = sendTime
|
||||
// if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if n == 0 {
|
||||
// sChat := UserChat{}
|
||||
// sChat.UID = seqUid
|
||||
// sChat.Msg = append(sChat.Msg, sMsg)
|
||||
// err = c.Insert(&sChat)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else {
|
||||
// err = c.Update(bson.M{"uid": seqUid}, bson.M{"$push": bson.M{"msg": sMsg}})
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// log.NewDebug("", "insert mgo data cost time", getCurrentTimestampByMill()-newTime)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) DelUserChatMongo2(uid string) error {
|
||||
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
// filter := bson.M{"uid": uid}
|
||||
//
|
||||
// delTime := time.Now().Unix() - int64(config.Config.Mongo.DBRetainChatRecords)*24*3600
|
||||
// if _, err := c.UpdateOne(ctx, filter, bson.M{"$pull": bson.M{"msg": bson.M{"sendtime": bson.M{"$lte": delTime}}}}); err != nil {
|
||||
// return utils.Wrap(err, "")
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (d *db.DataBases) MgoSkipUID(count int) (string, error) {
|
||||
// return "", nil
|
||||
//session := d.mgoSession.Clone()
|
||||
//if session == nil {
|
||||
// return "", errors.New("session == nil")
|
||||
//}
|
||||
//defer session.Close()
|
||||
//
|
||||
//c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||||
//
|
||||
//sChat := UserChat{}
|
||||
//c.Find(nil).Skip(count).Limit(1).One(&sChat)
|
||||
//return sChat.UID, nil
|
||||
}
|
||||
|
||||
//func generateTagID(tagName, userID string) string {
|
||||
// return utils.Md5(tagName + userID + strconv.Itoa(rand.Int()) + time.Now().String())
|
||||
//}
|
||||
|
||||
|
||||
|
||||
//func getCurrentTimestampByMill() int64 {
|
||||
// return time.Now().UnixNano() / 1e6
|
||||
//}
|
||||
//func GetCurrentTimestampByMill() int64 {
|
||||
// return time.Now().UnixNano() / 1e6
|
||||
//}
|
||||
|
||||
//func getSeqUid(uid string, seq uint32) string {
|
||||
// seqSuffix := seq / singleGocMsgNum
|
||||
// return indexGen(uid, seqSuffix)
|
||||
//}
|
||||
//
|
||||
//func getSeqUserIDList(userID string, maxSeq uint32) []string {
|
||||
// seqMaxSuffix := maxSeq / singleGocMsgNum
|
||||
// var seqUserIDList []string
|
||||
// for i := 0; i <= int(seqMaxSuffix); i++ {
|
||||
// seqUserID := indexGen(userID, uint32(i))
|
||||
// seqUserIDList = append(seqUserIDList, seqUserID)
|
||||
// }
|
||||
// return seqUserIDList
|
||||
//}
|
||||
//
|
||||
//func getSeqSuperGroupID(groupID string, seq uint32) string {
|
||||
// seqSuffix := seq / singleGocMsgNum
|
||||
// return superGroupIndexGen(groupID, seqSuffix)
|
||||
//}
|
||||
//
|
||||
//func GetSeqUid(uid string, seq uint32) string {
|
||||
// return getSeqUid(uid, seq)
|
||||
//}
|
||||
//
|
||||
//func getMsgIndex(seq uint32) int {
|
||||
// seqSuffix := seq / singleGocMsgNum
|
||||
// var index uint32
|
||||
// if seqSuffix == 0 {
|
||||
// index = (seq - seqSuffix*singleGocMsgNum) - 1
|
||||
// } else {
|
||||
// index = seq - seqSuffix*singleGocMsgNum
|
||||
// }
|
||||
// return int(index)
|
||||
//}
|
||||
|
||||
//func isContainInt32(target uint32, List []uint32) bool {
|
||||
// for _, element := range List {
|
||||
// if target == element {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
//}
|
||||
//
|
||||
//func isNotContainInt32(target uint32, List []uint32) bool {
|
||||
// for _, i := range List {
|
||||
// if i == target {
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
// return true
|
||||
//}
|
||||
//
|
||||
//func indexGen(uid string, seqSuffix uint32) string {
|
||||
// return uid + ":" + strconv.FormatInt(int64(seqSuffix), 10)
|
||||
//}
|
||||
|
||||
//func superGroupIndexGen(groupID string, seqSuffix uint32) string {
|
||||
// return "super_group_" + groupID + ":" + strconv.FormatInt(int64(seqSuffix), 10)
|
||||
//}
|
||||
|
||||
+75
-505
@@ -1,561 +1,131 @@
|
||||
package unrelation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/unrelation"
|
||||
table "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/proto/sdkws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/gogo/protobuf/sortkeys"
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
|
||||
var ErrMsgNotFound = errors.New("msg not found")
|
||||
|
||||
type MsgMongoDriver struct {
|
||||
mgoDB *mongo.Database
|
||||
MsgCollection *mongo.Collection
|
||||
msg table.MsgDocModel
|
||||
}
|
||||
|
||||
func NewMsgMongoDriver(mgoDB *mongo.Database) *MsgMongoDriver {
|
||||
return &MsgMongoDriver{mgoDB: mgoDB, MsgCollection: mgoDB.Collection(unrelation.CChat)}
|
||||
return &MsgMongoDriver{mgoDB: mgoDB, MsgCollection: mgoDB.Collection(table.MsgDocModel{}.TableName())}
|
||||
}
|
||||
|
||||
func (m *MsgMongoDriver) FindOneAndUpdate(ctx context.Context, filter, update, output interface{}, opts ...*options.FindOneAndUpdateOptions) error {
|
||||
return m.MsgCollection.FindOneAndUpdate(ctx, filter, update, opts...).Decode(output)
|
||||
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
|
||||
filter := bson.M{"uid": docID}
|
||||
return m.MsgCollection.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": bson.M{"$each": msgsToMongo}}}).Err()
|
||||
}
|
||||
|
||||
func (m *MsgMongoDriver) UpdateOne(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) error {
|
||||
_, err := m.MsgCollection.UpdateOne(ctx, filter, update, opts...)
|
||||
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
|
||||
_, err := m.MsgCollection.InsertOne(ctx, model)
|
||||
return err
|
||||
}
|
||||
|
||||
// database controller
|
||||
func (m *MsgMongoDriver) DelMsgBySeqList(ctx context.Context, userID string, seqList []uint32) (totalUnExistSeqList []uint32, err error) {
|
||||
sortkeys.Uint32s(seqList)
|
||||
suffixUserID2SubSeqList := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
t := make(map[string][]uint32)
|
||||
for i := 0; i < len(seqList); i++ {
|
||||
seqUid := getSeqUid(uid, seqList[i])
|
||||
if value, ok := t[seqUid]; !ok {
|
||||
var temp []uint32
|
||||
t[seqUid] = append(temp, seqList[i])
|
||||
} else {
|
||||
t[seqUid] = append(value, seqList[i])
|
||||
}
|
||||
}
|
||||
return t
|
||||
}(userID, seqList)
|
||||
lock := sync.Mutex{}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(suffixUserID2SubSeqList))
|
||||
for k, v := range suffixUserID2SubSeqList {
|
||||
go func(suffixUserID string, subSeqList []uint32) {
|
||||
defer wg.Done()
|
||||
unexistSeqList, err := m.DelMsgBySeqListInOneDoc(ctx, suffixUserID, subSeqList)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
lock.Lock()
|
||||
totalUnExistSeqList = append(totalUnExistSeqList, unexistSeqList...)
|
||||
lock.Unlock()
|
||||
}(k, v)
|
||||
}
|
||||
return totalUnExistSeqList, nil
|
||||
}
|
||||
|
||||
func (m *MsgMongoDriver) DelMsgBySeqListInOneDoc(ctx context.Context, suffixUserID string, seqList []uint32) ([]uint32, error) {
|
||||
seqMsgList, indexList, unexistSeqList, err := m.GetMsgAndIndexBySeqListInOneMongo2(suffixUserID, seqList)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
for i, v := range seqMsgList {
|
||||
if err := m.ReplaceMsgByIndex(suffixUserID, v, operationID, indexList[i]); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
}
|
||||
return unexistSeqList, nil
|
||||
}
|
||||
|
||||
// database
|
||||
func (m *MsgMongoDriver) DelMsgLogic(ctx context.Context, uid string, seqList []uint32) error {
|
||||
sortkeys.Uint32s(seqList)
|
||||
seqMsgs, err := d.GetMsgBySeqListMongo2(ctx, uid, seqList)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
for _, seqMsg := range seqMsgs {
|
||||
seqMsg.Status = constant.MsgDeleted
|
||||
if err = d.ReplaceMsgBySeq(ctx, uid, seqMsg); err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "ReplaceMsgListBySeq error", err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// model
|
||||
func (m *MsgMongoDriver) ReplaceMsgByIndex(ctx context.Context, suffixUserID string, msg *sdkws.MsgData, seqIndex int) error {
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), suffixUserID, *msg)
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
s := fmt.Sprintf("msg.%d.msg", seqIndex)
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
|
||||
msg.Status = constant.MsgDeleted
|
||||
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error {
|
||||
msg.Status = status
|
||||
bytes, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal failed ", err.Error(), msg.String())
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
updateResult, err := c.UpdateOne(ctx, bson.M{"uid": suffixUserID}, bson.M{"$set": bson.M{s: bytes}})
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
|
||||
_, err = m.MsgCollection.UpdateOne(ctx, bson.M{"uid": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msg.%d.msg", seqIndex): bytes}})
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) ReplaceMsgBySeq(uid string, msg *sdkws.MsgData, operationID string) error {
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), uid, *msg)
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
uid = getSeqUid(uid, msg.Seq)
|
||||
seqIndex := getMsgIndex(msg.Seq)
|
||||
s := fmt.Sprintf("msg.%d.msg", seqIndex)
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
|
||||
bytes, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal", err.Error())
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
updateResult, err := c.UpdateOne(
|
||||
ctx, bson.M{"uid": uid},
|
||||
bson.M{"$set": bson.M{s: bytes}})
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
return nil
|
||||
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
|
||||
doc := &table.MsgDocModel{}
|
||||
err := m.MsgCollection.FindOne(ctx, bson.M{"uid": docID}).Decode(doc)
|
||||
return doc, err
|
||||
}
|
||||
|
||||
func (d *db.DataBases) UpdateOneMsgList(msg *UserChat) error {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
_, err := c.UpdateOne(ctx, bson.M{"uid": msg.UID}, bson.M{"$set": bson.M{"msg": msg.Msg}})
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *db.DataBases) GetMsgBySeqList(uid string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), uid, seqList)
|
||||
var hasSeqList []uint32
|
||||
singleCount := 0
|
||||
session := d.mgoSession.Clone()
|
||||
if session == nil {
|
||||
return nil, errors.New("session == nil")
|
||||
}
|
||||
defer session.Close()
|
||||
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||||
m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
t := make(map[string][]uint32)
|
||||
for i := 0; i < len(seqList); i++ {
|
||||
seqUid := getSeqUid(uid, seqList[i])
|
||||
if value, ok := t[seqUid]; !ok {
|
||||
var temp []uint32
|
||||
t[seqUid] = append(temp, seqList[i])
|
||||
} else {
|
||||
t[seqUid] = append(value, seqList[i])
|
||||
}
|
||||
}
|
||||
return t
|
||||
}(uid, seqList)
|
||||
sChat := UserChat{}
|
||||
for seqUid, value := range m {
|
||||
if err = c.Find(bson.M{"uid": seqUid}).One(&sChat); err != nil {
|
||||
log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
|
||||
continue
|
||||
}
|
||||
singleCount = 0
|
||||
for i := 0; i < len(sChat.Msg); i++ {
|
||||
msg := new(sdkws.MsgData)
|
||||
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if isContainInt32(msg.Seq, value) {
|
||||
seqMsg = append(seqMsg, msg)
|
||||
hasSeqList = append(hasSeqList, msg.Seq)
|
||||
singleCount++
|
||||
if singleCount == len(value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(hasSeqList) != len(seqList) {
|
||||
var diff []uint32
|
||||
diff = utils.Difference(hasSeqList, seqList)
|
||||
exceptionMSg := genExceptionMessageBySeqList(diff)
|
||||
seqMsg = append(seqMsg, exceptionMSg...)
|
||||
|
||||
}
|
||||
return seqMsg, nil
|
||||
}
|
||||
|
||||
// model
|
||||
func (d *db.DataBases) GetUserMsgListByIndex(docID string, index int64) (*UserChat, error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
regex := fmt.Sprintf("^%s", docID)
|
||||
func (m *MsgMongoDriver) GetMsgsByIndex(ctx context.Context, sourceID string, index int64) (*table.MsgDocModel, error) {
|
||||
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
|
||||
var msgs []UserChat
|
||||
//primitive.Regex{Pattern: regex}
|
||||
cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
|
||||
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: fmt.Sprintf("^%s", sourceID)}}, findOpts)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var msgs []table.MsgDocModel
|
||||
err = cursor.All(context.Background(), &msgs)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
|
||||
}
|
||||
if len(msgs) > 0 {
|
||||
return &msgs[0], nil
|
||||
} else {
|
||||
return nil, ErrMsgListNotExist
|
||||
}
|
||||
return nil, ErrMsgListNotExist
|
||||
}
|
||||
|
||||
// model
|
||||
func (d *db.DataBases) DelMongoMsgs(IDList []string) error {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
_, err := c.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": IDList}})
|
||||
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, sourceID string) (*table.MsgInfoModel, error) {
|
||||
var msgDocs []table.MsgDocModel
|
||||
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": bson.M{"$regex": fmt.Sprintf("^%s", sourceID)}}, options.Find().SetLimit(1).SetSort(bson.M{"uid": -1}))
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
err = cursor.All(ctx, &msgDocs)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
if len(msgDocs) > 0 {
|
||||
if len(msgDocs[0].Msg) > 0 {
|
||||
return &msgDocs[0].Msg[len(msgDocs[0].Msg)-1], nil
|
||||
}
|
||||
return nil, errors.New("len(msgDocs[0].Msg) < 0")
|
||||
}
|
||||
return nil, ErrMsgNotFound
|
||||
}
|
||||
|
||||
func (m *MsgMongoDriver) GetOldestMsg(ctx context.Context, sourceID string) (*table.MsgInfoModel, error) {
|
||||
var msgDocs []table.MsgDocModel
|
||||
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": bson.M{"$regex": fmt.Sprintf("^%s", sourceID)}}, options.Find().SetLimit(1).SetSort(bson.M{"uid": 1}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = cursor.All(ctx, &msgDocs)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var oldestMsg table.MsgInfoModel
|
||||
if len(msgDocs) > 0 {
|
||||
for _, v := range msgDocs[0].Msg {
|
||||
if v.SendTime != 0 {
|
||||
oldestMsg = v
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(oldestMsg.Msg) == 0 {
|
||||
if len(msgDocs[0].Msg) > 0 {
|
||||
oldestMsg = msgDocs[0].Msg[0]
|
||||
}
|
||||
}
|
||||
return &oldestMsg, nil
|
||||
}
|
||||
return nil, ErrMsgNotFound
|
||||
}
|
||||
|
||||
func (m *MsgMongoDriver) Delete(ctx context.Context, docIDs []string) error {
|
||||
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": docIDs}})
|
||||
return err
|
||||
}
|
||||
|
||||
// model
|
||||
func (d *db.DataBases) ReplaceMsgToBlankByIndex(suffixID string, index int) (replaceMaxSeq uint32, err error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
userChat := &UserChat{}
|
||||
err = c.FindOne(ctx, bson.M{"uid": suffixID}).Decode(&userChat)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for i, msg := range userChat.Msg {
|
||||
if i <= index {
|
||||
msgPb := &sdkws.MsgData{}
|
||||
if err = proto.Unmarshal(msg.Msg, msgPb); err != nil {
|
||||
continue
|
||||
}
|
||||
newMsgPb := &sdkws.MsgData{Seq: msgPb.Seq}
|
||||
bytes, err := proto.Marshal(newMsgPb)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
msg.Msg = bytes
|
||||
msg.SendTime = 0
|
||||
replaceMaxSeq = msgPb.Seq
|
||||
}
|
||||
}
|
||||
_, err = c.UpdateOne(ctx, bson.M{"uid": suffixID}, bson.M{"$set": bson.M{"msg": userChat.Msg}})
|
||||
return replaceMaxSeq, err
|
||||
}
|
||||
|
||||
func (d *db.DataBases) GetNewestMsg(ID string) (msg *sdkws.MsgData, err error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
regex := fmt.Sprintf("^%s", ID)
|
||||
findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": -1})
|
||||
var userChats []UserChat
|
||||
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = cursor.All(ctx, &userChats)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
if len(userChats) > 0 {
|
||||
if len(userChats[0].Msg) > 0 {
|
||||
msgPb := &sdkws.MsgData{}
|
||||
err = proto.Unmarshal(userChats[0].Msg[len(userChats[0].Msg)-1].Msg, msgPb)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
return msgPb, nil
|
||||
}
|
||||
return nil, errors.New("len(userChats[0].Msg) < 0")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) GetOldestMsg(ID string) (msg *sdkws.MsgData, err error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
regex := fmt.Sprintf("^%s", ID)
|
||||
findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": 1})
|
||||
var userChats []UserChat
|
||||
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = cursor.All(ctx, &userChats)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
var oldestMsg []byte
|
||||
if len(userChats) > 0 {
|
||||
for _, v := range userChats[0].Msg {
|
||||
if v.SendTime != 0 {
|
||||
oldestMsg = v.Msg
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(oldestMsg) == 0 {
|
||||
oldestMsg = userChats[0].Msg[len(userChats[0].Msg)-1].Msg
|
||||
}
|
||||
msgPb := &sdkws.MsgData{}
|
||||
err = proto.Unmarshal(oldestMsg, msgPb)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
return msgPb, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) GetMsgBySeqListMongo2(uid string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
var hasSeqList []uint32
|
||||
singleCount := 0
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
|
||||
m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
t := make(map[string][]uint32)
|
||||
for i := 0; i < len(seqList); i++ {
|
||||
seqUid := getSeqUid(uid, seqList[i])
|
||||
if value, ok := t[seqUid]; !ok {
|
||||
var temp []uint32
|
||||
t[seqUid] = append(temp, seqList[i])
|
||||
} else {
|
||||
t[seqUid] = append(value, seqList[i])
|
||||
}
|
||||
}
|
||||
return t
|
||||
}(uid, seqList)
|
||||
sChat := UserChat{}
|
||||
for seqUid, value := range m {
|
||||
if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
|
||||
log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
|
||||
continue
|
||||
}
|
||||
singleCount = 0
|
||||
for i := 0; i < len(sChat.Msg); i++ {
|
||||
msg := new(sdkws.MsgData)
|
||||
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if isContainInt32(msg.Seq, value) {
|
||||
seqMsg = append(seqMsg, msg)
|
||||
hasSeqList = append(hasSeqList, msg.Seq)
|
||||
singleCount++
|
||||
if singleCount == len(value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(hasSeqList) != len(seqList) {
|
||||
var diff []uint32
|
||||
diff = utils.Difference(hasSeqList, seqList)
|
||||
exceptionMSg := genExceptionMessageBySeqList(diff)
|
||||
seqMsg = append(seqMsg, exceptionMSg...)
|
||||
|
||||
}
|
||||
return seqMsg, nil
|
||||
}
|
||||
func (d *db.DataBases) GetSuperGroupMsgBySeqListMongo(groupID string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, err error) {
|
||||
var hasSeqList []uint32
|
||||
singleCount := 0
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
|
||||
m := func(uid string, seqList []uint32) map[string][]uint32 {
|
||||
t := make(map[string][]uint32)
|
||||
for i := 0; i < len(seqList); i++ {
|
||||
seqUid := getSeqUid(uid, seqList[i])
|
||||
if value, ok := t[seqUid]; !ok {
|
||||
var temp []uint32
|
||||
t[seqUid] = append(temp, seqList[i])
|
||||
} else {
|
||||
t[seqUid] = append(value, seqList[i])
|
||||
}
|
||||
}
|
||||
return t
|
||||
}(groupID, seqList)
|
||||
sChat := UserChat{}
|
||||
for seqUid, value := range m {
|
||||
if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
|
||||
log.NewError(operationID, "not find seqGroupID", seqUid, value, groupID, seqList, err.Error())
|
||||
continue
|
||||
}
|
||||
singleCount = 0
|
||||
for i := 0; i < len(sChat.Msg); i++ {
|
||||
msg := new(sdkws.MsgData)
|
||||
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
log.NewError(operationID, "Unmarshal err", seqUid, value, groupID, seqList, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if isContainInt32(msg.Seq, value) {
|
||||
seqMsg = append(seqMsg, msg)
|
||||
hasSeqList = append(hasSeqList, msg.Seq)
|
||||
singleCount++
|
||||
if singleCount == len(value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(hasSeqList) != len(seqList) {
|
||||
var diff []uint32
|
||||
diff = utils.Difference(hasSeqList, seqList)
|
||||
exceptionMSg := genExceptionSuperGroupMessageBySeqList(diff, groupID)
|
||||
seqMsg = append(seqMsg, exceptionMSg...)
|
||||
|
||||
}
|
||||
return seqMsg, nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) GetMsgAndIndexBySeqListInOneMongo2(suffixUserID string, seqList []uint32, operationID string) (seqMsg []*sdkws.MsgData, indexList []int, unexistSeqList []uint32, err error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
sChat := UserChat{}
|
||||
if err = c.FindOne(ctx, bson.M{"uid": suffixUserID}).Decode(&sChat); err != nil {
|
||||
log.NewError(operationID, "not find seqUid", suffixUserID, err.Error())
|
||||
return nil, nil, nil, utils.Wrap(err, "")
|
||||
}
|
||||
singleCount := 0
|
||||
var hasSeqList []uint32
|
||||
for i := 0; i < len(sChat.Msg); i++ {
|
||||
msg := new(sdkws.MsgData)
|
||||
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
|
||||
log.NewError(operationID, "Unmarshal err", msg.String(), err.Error())
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if isContainInt32(msg.Seq, seqList) {
|
||||
indexList = append(indexList, i)
|
||||
seqMsg = append(seqMsg, msg)
|
||||
hasSeqList = append(hasSeqList, msg.Seq)
|
||||
singleCount++
|
||||
if singleCount == len(seqList) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, i := range seqList {
|
||||
if isContainInt32(i, hasSeqList) {
|
||||
continue
|
||||
}
|
||||
unexistSeqList = append(unexistSeqList, i)
|
||||
}
|
||||
return seqMsg, indexList, unexistSeqList, nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) SaveUserChatMongo2(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
newTime := getCurrentTimestampByMill()
|
||||
operationID := ""
|
||||
seqUid := getSeqUid(uid, m.MsgData.Seq)
|
||||
filter := bson.M{"uid": seqUid}
|
||||
var err error
|
||||
sMsg := MsgInfo{}
|
||||
sMsg.SendTime = sendTime
|
||||
if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
err = c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": sMsg}}).Err()
|
||||
log.NewWarn(operationID, "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
|
||||
if err != nil {
|
||||
sChat := UserChat{}
|
||||
sChat.UID = seqUid
|
||||
sChat.Msg = append(sChat.Msg, sMsg)
|
||||
if _, err = c.InsertOne(ctx, &sChat); err != nil {
|
||||
log.NewDebug(operationID, "InsertOne failed", filter)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
} else {
|
||||
log.NewDebug(operationID, "FindOneAndUpdate ok", filter)
|
||||
}
|
||||
|
||||
log.NewDebug(operationID, "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) SaveUserChat(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
|
||||
var seqUid string
|
||||
newTime := getCurrentTimestampByMill()
|
||||
session := d.mgoSession.Clone()
|
||||
if session == nil {
|
||||
return errors.New("session == nil")
|
||||
}
|
||||
defer session.Close()
|
||||
log.NewDebug("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
|
||||
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||||
seqUid = getSeqUid(uid, m.MsgData.Seq)
|
||||
n, err := c.Find(bson.M{"uid": seqUid}).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.NewDebug("", "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
|
||||
sMsg := MsgInfo{}
|
||||
sMsg.SendTime = sendTime
|
||||
if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
sChat := UserChat{}
|
||||
sChat.UID = seqUid
|
||||
sChat.Msg = append(sChat.Msg, sMsg)
|
||||
err = c.Insert(&sChat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = c.Update(bson.M{"uid": seqUid}, bson.M{"$push": bson.M{"msg": sMsg}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
log.NewDebug("", "insert mgo data cost time", getCurrentTimestampByMill()-newTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *db.DataBases) CleanUpUserMsgFromMongo(userID string, operationID string) error {
|
||||
ctx := context.Background()
|
||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||
maxSeq, err := d.GetUserMaxSeq(userID)
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
seqUsers := getSeqUserIDList(userID, uint32(maxSeq))
|
||||
log.Error(operationID, "getSeqUserIDList", seqUsers)
|
||||
_, err = c.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": seqUsers}})
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
}
|
||||
return utils.Wrap(err, "")
|
||||
func (m *MsgMongoDriver) UpdateOneDoc(ctx context.Context, msg *table.MsgDocModel) error {
|
||||
_, err := m.MsgCollection.UpdateOne(ctx, bson.M{"uid": msg.DocID}, bson.M{"$set": bson.M{"msg": msg.Msg}})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ type SuperGroupMongoDriver struct {
|
||||
//}
|
||||
|
||||
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
|
||||
GroupID: groupID,
|
||||
MemberIDs: initMemberIDs,
|
||||
@@ -147,7 +147,7 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string, tx ...any) (group *unrelation.SuperGroupModel, err error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
@@ -155,14 +155,15 @@ func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID stri
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*unrelation.SuperGroupModel, err error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||
"$in": groupIDs,
|
||||
}})
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
if err := cursor.All(ctx, &groups); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
@@ -170,7 +171,7 @@ func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []s
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||
@@ -194,7 +195,7 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||
@@ -212,14 +213,14 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*unrelation.UserToSuperGroupModel, error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
var user unrelation.UserToSuperGroupModel
|
||||
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||
return &user, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
group, err := s.TakeSuperGroup(ctx, groupID, tx...)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -231,7 +232,7 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st
|
||||
}
|
||||
|
||||
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
// ctx = s.getTxCtx(ctx, tx)
|
||||
// ctx = getTxCtx(ctx, tx)
|
||||
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
// superGroup := &unrelation.SuperGroupModel{}
|
||||
@@ -249,7 +250,7 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st
|
||||
//}
|
||||
|
||||
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
_, err := s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user