BatchInsertBlock

test-errcode
withchao 2 years ago
parent 85de9aaefb
commit f389359dd0

@ -146,7 +146,7 @@ func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationI
return nil
}
num := db.msg.GetSingleGocMsgNum()
num = 100
//num = 100
if msgList[0].Msg != nil {
firstSeq = msgList[0].Msg.Seq
}
@ -194,7 +194,7 @@ func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationI
}
doc := unRelationTb.MsgDocModel{
DocID: docID,
Msg: make([]unRelationTb.MsgInfoModel, num),
Msg: make([]*unRelationTb.MsgInfoModel, num),
}
var insert int
for j := i; j < len(msgList); j++ {
@ -203,14 +203,21 @@ func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationI
break
}
insert++
doc.Msg[getIndex(seq)] = *msgList[j]
doc.Msg[getIndex(seq)] = msgList[j]
}
for i, model := range doc.Msg {
if model.DelList == nil {
doc.Msg[i].DelList = []string{}
}
if model.ReadList == nil {
doc.Msg[i].ReadList = []string{}
if model == nil {
doc.Msg[i] = &unRelationTb.MsgInfoModel{
DelList: []string{},
ReadList: []string{},
}
} else {
if model.DelList == nil {
doc.Msg[i].DelList = []string{}
}
if model.ReadList == nil {
doc.Msg[i].ReadList = []string{}
}
}
}
if err := db.msgDocDatabase.Create(ctx, &doc); err != nil {

@ -197,3 +197,38 @@ func Test_Delete(t *testing.T) {
t.Fatal(err)
}
}
func Test_Delete1(t *testing.T) {
config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"}
config.Config.Mongo.DBTimeout = 60
config.Config.Mongo.DBDatabase = "openIM"
config.Config.Mongo.DBSource = "admin"
config.Config.Mongo.DBUserName = "root"
config.Config.Mongo.DBPassword = "openIM123"
config.Config.Mongo.DBMaxPoolSize = 100
config.Config.Mongo.DBRetainChatRecords = 3650
config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3"
mongo, err := unrelation.NewMongo()
if err != nil {
panic(err)
}
err = mongo.GetDatabase().Client().Ping(context.Background(), nil)
if err != nil {
panic(err)
}
c := mongo.GetClient().Database("openIM").Collection("msg")
var o unRelationTb.MsgDocModel
err = c.FindOne(context.Background(), bson.M{"doc_id": "test:0"}).Decode(&o)
if err != nil {
panic(err)
}
for i, model := range o.Msg {
fmt.Println(i, model == nil)
}
}

@ -16,8 +16,8 @@ const (
)
type MsgDocModel struct {
DocID string `bson:"doc_id"`
Msg []MsgInfoModel `bson:"msgs"`
DocID string `bson:"doc_id"`
Msg []*MsgInfoModel `bson:"msgs"`
}
type RevokeModel struct {

@ -159,21 +159,21 @@ func (m *MsgMongoDriver) GetMsgsByIndex(ctx context.Context, conversationID stri
}
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
var msgDocs []table.MsgDocModel
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": bson.M{"$regex": fmt.Sprintf("^%s:", conversationID)}}, options.Find().SetLimit(1).SetSort(bson.M{"doc_id": -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, errs.ErrRecordNotFound.Wrap("len(msgDocs[0].Msgs) < 0")
}
//var msgDocs []table.MsgDocModel
//cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": bson.M{"$regex": fmt.Sprintf("^%s:", conversationID)}}, options.Find().SetLimit(1).SetSort(bson.M{"doc_id": -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, errs.ErrRecordNotFound.Wrap("len(msgDocs[0].Msgs) < 0")
//}
return nil, ErrMsgNotFound
}

Loading…
Cancel
Save