BatchInsertBlock

This commit is contained in:
withchao 2023-05-26 14:30:10 +08:00
parent 2401cd3525
commit 1ba6bef27a
2 changed files with 63 additions and 55 deletions

View File

@ -275,6 +275,9 @@ func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationI
} }
func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversationID string, msgList []*sdkws.MsgData, currentMaxSeq int64) error { func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversationID string, msgList []*sdkws.MsgData, currentMaxSeq int64) error {
if len(msgList) == 0 {
return errs.ErrArgs.Wrap("msgList is empty")
}
msgs := make([]any, len(msgList)) msgs := make([]any, len(msgList))
for i, msg := range msgList { for i, msg := range msgList {
if msg == nil { if msg == nil {
@ -314,7 +317,18 @@ func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversatio
Ex: msg.Ex, Ex: msg.Ex,
} }
} }
return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, currentMaxSeq-int64(len(msgList))) return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, msgList[0].Seq)
}
func (db *commonMsgDatabase) MarkUserDeleteMsg(ctx context.Context, conversationID string, seq int64, userIDs []string) error {
if len(userIDs) == 0 {
return nil
}
msgs := make([]any, len(userIDs))
for i, userID := range userIDs {
msgs[i] = userID
}
return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyDel, seq)
} }
func (db *commonMsgDatabase) RevokeMsg(ctx context.Context, conversationID string, seq int64, revoke *unRelationTb.RevokeModel) error { func (db *commonMsgDatabase) RevokeMsg(ctx context.Context, conversationID string, seq int64, revoke *unRelationTb.RevokeModel) error {

View File

@ -152,16 +152,14 @@ func GetDB() *commonMsgDatabase {
func Test_Insert(t *testing.T) { func Test_Insert(t *testing.T) {
db := GetDB() db := GetDB()
ctx := context.Background() ctx := context.Background()
var arr []*unRelationTb.MsgInfoModel var arr []any
for i := 0; i < 345; i++ { for i := 0; i < 345; i++ {
arr = append(arr, &unRelationTb.MsgInfoModel{ arr = append(arr, &unRelationTb.MsgDataModel{
Msg: &unRelationTb.MsgDataModel{ Seq: int64(i),
Seq: int64(i), Content: fmt.Sprintf("test-%d", i),
Content: fmt.Sprintf("test-%d", i),
},
}) })
} }
if err := db.BatchInsertBlock(ctx, "test", arr, 0); err != nil { if err := db.BatchInsertBlock(ctx, "test", arr, updateKeyMsg, 0); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -169,17 +167,15 @@ func Test_Insert(t *testing.T) {
func Test_Revoke(t *testing.T) { func Test_Revoke(t *testing.T) {
db := GetDB() db := GetDB()
ctx := context.Background() ctx := context.Background()
var arr []*unRelationTb.MsgInfoModel var arr []any
for i := 0; i < 456; i++ { for i := 0; i < 456; i++ {
arr = append(arr, &unRelationTb.MsgInfoModel{ arr = append(arr, &unRelationTb.RevokeModel{
Revoke: &unRelationTb.RevokeModel{ UserID: "uid_" + strconv.Itoa(i),
UserID: "uid_" + strconv.Itoa(i), Nickname: "uname_" + strconv.Itoa(i),
Nickname: "uname_" + strconv.Itoa(i), Time: time.Now().UnixMilli(),
Time: time.Now().UnixMilli(),
},
}) })
} }
if err := db.BatchInsertBlock(ctx, "test", arr, 123); err != nil { if err := db.BatchInsertBlock(ctx, "test", arr, updateKeyRevoke, 123); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -187,48 +183,46 @@ func Test_Revoke(t *testing.T) {
func Test_Delete(t *testing.T) { func Test_Delete(t *testing.T) {
db := GetDB() db := GetDB()
ctx := context.Background() ctx := context.Background()
var arr []*unRelationTb.MsgInfoModel var arr []any
for i := 0; i < 123; i++ { for i := 0; i < 123; i++ {
arr = append(arr, &unRelationTb.MsgInfoModel{ arr = append(arr, []string{"uid_1", "uid_2"})
DelList: []string{"uid_1", "uid_2"},
})
} }
if err := db.BatchInsertBlock(ctx, "test", arr, 210); err != nil { if err := db.BatchInsertBlock(ctx, "test", arr, updateKeyDel, 210); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func Test_Delete1(t *testing.T) { //func Test_Delete1(t *testing.T) {
config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"} // config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"}
config.Config.Mongo.DBTimeout = 60 // config.Config.Mongo.DBTimeout = 60
config.Config.Mongo.DBDatabase = "openIM" // config.Config.Mongo.DBDatabase = "openIM"
config.Config.Mongo.DBSource = "admin" // config.Config.Mongo.DBSource = "admin"
config.Config.Mongo.DBUserName = "root" // config.Config.Mongo.DBUserName = "root"
config.Config.Mongo.DBPassword = "openIM123" // config.Config.Mongo.DBPassword = "openIM123"
config.Config.Mongo.DBMaxPoolSize = 100 // config.Config.Mongo.DBMaxPoolSize = 100
config.Config.Mongo.DBRetainChatRecords = 3650 // config.Config.Mongo.DBRetainChatRecords = 3650
config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3" // config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3"
//
mongo, err := unrelation.NewMongo() // mongo, err := unrelation.NewMongo()
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
err = mongo.GetDatabase().Client().Ping(context.Background(), nil) // err = mongo.GetDatabase().Client().Ping(context.Background(), nil)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
//
c := mongo.GetClient().Database("openIM").Collection("msg") // c := mongo.GetClient().Database("openIM").Collection("msg")
//
var o unRelationTb.MsgDocModel // var o unRelationTb.MsgDocModel
//
err = c.FindOne(context.Background(), bson.M{"doc_id": "test:0"}).Decode(&o) // err = c.FindOne(context.Background(), bson.M{"doc_id": "test:0"}).Decode(&o)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
//
for i, model := range o.Msg { // for i, model := range o.Msg {
fmt.Println(i, model == nil) // fmt.Println(i, model == nil)
} // }
//
} //}