BatchInsertBlock

This commit is contained in:
withchao 2023-05-25 17:31:46 +08:00
parent 85de9aaefb
commit f389359dd0
4 changed files with 67 additions and 25 deletions

View File

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

View File

@ -197,3 +197,38 @@ func Test_Delete(t *testing.T) {
t.Fatal(err) 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)
}
}

View File

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

View File

@ -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) { func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
var msgDocs []table.MsgDocModel //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})) //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 { //if err != nil {
return nil, utils.Wrap(err, "") // return nil, utils.Wrap(err, "")
} //}
err = cursor.All(ctx, &msgDocs) //err = cursor.All(ctx, &msgDocs)
if err != nil { //if err != nil {
return nil, utils.Wrap(err, "") // return nil, utils.Wrap(err, "")
} //}
if len(msgDocs) > 0 { //if len(msgDocs) > 0 {
if len(msgDocs[0].Msg) > 0 { // if len(msgDocs[0].Msg) > 0 {
return &msgDocs[0].Msg[len(msgDocs[0].Msg)-1], nil // return &msgDocs[0].Msg[len(msgDocs[0].Msg)-1], nil
} // }
return nil, errs.ErrRecordNotFound.Wrap("len(msgDocs[0].Msgs) < 0") // return nil, errs.ErrRecordNotFound.Wrap("len(msgDocs[0].Msgs) < 0")
} //}
return nil, ErrMsgNotFound return nil, ErrMsgNotFound
} }