From 6e7bfbea8e3506dd03cce1fab90e95b8281eba57 Mon Sep 17 00:00:00 2001 From: WhereAreBugs Date: Wed, 4 Feb 2026 13:43:54 +0800 Subject: [PATCH] remove(CopyStructFields):full datautil.CopyStructFields has been replaced due to performance issue. --- internal/rpc/conversation/conversation.go | 37 ++++++- internal/rpc/msg/sync_msg.go | 22 ++++- pkg/common/convert/conversation.go | 98 ++++++++++++++++--- pkg/common/convert/friend.go | 22 +++-- pkg/common/storage/controller/conversation.go | 7 +- 5 files changed, 152 insertions(+), 34 deletions(-) diff --git a/internal/rpc/conversation/conversation.go b/internal/rpc/conversation/conversation.go index 707a67b14..6f150c679 100644 --- a/internal/rpc/conversation/conversation.go +++ b/internal/rpc/conversation/conversation.go @@ -233,9 +233,28 @@ func (c *conversationServer) getConversations(ctx context.Context, ownerUserID s // Deprecated func (c *conversationServer) SetConversation(ctx context.Context, req *pbconversation.SetConversationReq) (*pbconversation.SetConversationResp, error) { + if req.Conversation == nil { + return nil, errs.ErrArgs.WrapMsg("conversation must not be nil") + } var conversation dbModel.Conversation - if err := datautil.CopyStructFields(&conversation, req.Conversation); err != nil { - return nil, err + conversation.OwnerUserID = req.Conversation.OwnerUserID + conversation.ConversationID = req.Conversation.ConversationID + conversation.RecvMsgOpt = req.Conversation.RecvMsgOpt + conversation.ConversationType = req.Conversation.ConversationType + conversation.UserID = req.Conversation.UserID + conversation.GroupID = req.Conversation.GroupID + conversation.IsPinned = req.Conversation.IsPinned + conversation.AttachedInfo = req.Conversation.AttachedInfo + conversation.IsPrivateChat = req.Conversation.IsPrivateChat + conversation.GroupAtType = req.Conversation.GroupAtType + conversation.Ex = req.Conversation.Ex + conversation.BurnDuration = req.Conversation.BurnDuration + conversation.MinSeq = req.Conversation.MinSeq + conversation.MaxSeq = req.Conversation.MaxSeq + conversation.MsgDestructTime = req.Conversation.MsgDestructTime + conversation.IsMsgDestruct = req.Conversation.IsMsgDestruct + if req.Conversation.LatestMsgDestructTime != 0 { + conversation.LatestMsgDestructTime = time.UnixMilli(req.Conversation.LatestMsgDestructTime) } err := c.conversationDatabase.SetUserConversations(ctx, req.Conversation.OwnerUserID, []*dbModel.Conversation{&conversation}) if err != nil { @@ -606,9 +625,17 @@ func (c *conversationServer) getConversationInfo( } for conversationID, chatLog := range chatLogs { pbchatLog := &pbconversation.ConversationElem{} - msgInfo := &pbconversation.MsgInfo{} - if err := datautil.CopyStructFields(msgInfo, chatLog); err != nil { - return nil, err + msgInfo := &pbconversation.MsgInfo{ + ServerMsgID: chatLog.ServerMsgID, + ClientMsgID: chatLog.ClientMsgID, + SessionType: chatLog.SessionType, + SendID: chatLog.SendID, + RecvID: chatLog.RecvID, + GroupID: chatLog.GroupID, + MsgFrom: chatLog.MsgFrom, + ContentType: chatLog.ContentType, + Content: string(chatLog.Content), + Ex: chatLog.Ex, } switch chatLog.SessionType { case constant.SingleChatType: diff --git a/internal/rpc/msg/sync_msg.go b/internal/rpc/msg/sync_msg.go index 6cf1c21d3..aa11817e6 100644 --- a/internal/rpc/msg/sync_msg.go +++ b/internal/rpc/msg/sync_msg.go @@ -24,7 +24,6 @@ import ( "github.com/openimsdk/protocol/msg" "github.com/openimsdk/protocol/sdkws" "github.com/openimsdk/tools/log" - "github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/timeutil" ) @@ -216,9 +215,24 @@ func (m *msgServer) SearchMessage(ctx context.Context, req *msg.SearchMessageReq // Construct response with updated information for _, chatLog := range chatLogs { pbchatLog := &msg.ChatLog{} - datautil.CopyStructFields(pbchatLog, chatLog.MsgData) - pbchatLog.SendTime = chatLog.MsgData.SendTime - pbchatLog.CreateTime = chatLog.MsgData.CreateTime + msgData := chatLog.MsgData + pbchatLog.ServerMsgID = msgData.ServerMsgID + pbchatLog.ClientMsgID = msgData.ClientMsgID + pbchatLog.SendID = msgData.SendID + pbchatLog.RecvID = msgData.RecvID + pbchatLog.GroupID = msgData.GroupID + pbchatLog.SenderPlatformID = msgData.SenderPlatformID + pbchatLog.SenderNickname = msgData.SenderNickname + pbchatLog.SenderFaceURL = msgData.SenderFaceURL + pbchatLog.SessionType = msgData.SessionType + pbchatLog.MsgFrom = msgData.MsgFrom + pbchatLog.ContentType = msgData.ContentType + pbchatLog.Content = string(msgData.Content) + pbchatLog.Status = msgData.Status + pbchatLog.SendTime = msgData.SendTime + pbchatLog.CreateTime = msgData.CreateTime + pbchatLog.Ex = msgData.Ex + pbchatLog.Seq = msgData.Seq if chatLog.MsgData.SenderNickname == "" { pbchatLog.SenderNickname = sendMap[chatLog.MsgData.SendID] } diff --git a/pkg/common/convert/conversation.go b/pkg/common/convert/conversation.go index 9389b0252..9da7b9205 100644 --- a/pkg/common/convert/conversation.go +++ b/pkg/common/convert/conversation.go @@ -15,46 +15,120 @@ package convert import ( + "time" + "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" "github.com/openimsdk/protocol/conversation" - "github.com/openimsdk/tools/utils/datautil" ) func ConversationDB2Pb(conversationDB *model.Conversation) *conversation.Conversation { - conversationPB := &conversation.Conversation{} - conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.UnixMilli() - if err := datautil.CopyStructFields(conversationPB, conversationDB); err != nil { + if conversationDB == nil { return nil } - return conversationPB + return &conversation.Conversation{ + OwnerUserID: conversationDB.OwnerUserID, + ConversationID: conversationDB.ConversationID, + RecvMsgOpt: conversationDB.RecvMsgOpt, + ConversationType: conversationDB.ConversationType, + UserID: conversationDB.UserID, + GroupID: conversationDB.GroupID, + IsPinned: conversationDB.IsPinned, + AttachedInfo: conversationDB.AttachedInfo, + IsPrivateChat: conversationDB.IsPrivateChat, + GroupAtType: conversationDB.GroupAtType, + Ex: conversationDB.Ex, + BurnDuration: conversationDB.BurnDuration, + MinSeq: conversationDB.MinSeq, + MaxSeq: conversationDB.MaxSeq, + MsgDestructTime: conversationDB.MsgDestructTime, + LatestMsgDestructTime: conversationDB.LatestMsgDestructTime.UnixMilli(), + IsMsgDestruct: conversationDB.IsMsgDestruct, + } } func ConversationsDB2Pb(conversationsDB []*model.Conversation) (conversationsPB []*conversation.Conversation) { for _, conversationDB := range conversationsDB { - conversationPB := &conversation.Conversation{} - if err := datautil.CopyStructFields(conversationPB, conversationDB); err != nil { + if conversationDB == nil { continue } - conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.UnixMilli() + conversationPB := &conversation.Conversation{ + OwnerUserID: conversationDB.OwnerUserID, + ConversationID: conversationDB.ConversationID, + RecvMsgOpt: conversationDB.RecvMsgOpt, + ConversationType: conversationDB.ConversationType, + UserID: conversationDB.UserID, + GroupID: conversationDB.GroupID, + IsPinned: conversationDB.IsPinned, + AttachedInfo: conversationDB.AttachedInfo, + IsPrivateChat: conversationDB.IsPrivateChat, + GroupAtType: conversationDB.GroupAtType, + Ex: conversationDB.Ex, + BurnDuration: conversationDB.BurnDuration, + MinSeq: conversationDB.MinSeq, + MaxSeq: conversationDB.MaxSeq, + MsgDestructTime: conversationDB.MsgDestructTime, + LatestMsgDestructTime: conversationDB.LatestMsgDestructTime.UnixMilli(), + IsMsgDestruct: conversationDB.IsMsgDestruct, + } conversationsPB = append(conversationsPB, conversationPB) } return conversationsPB } func ConversationPb2DB(conversationPB *conversation.Conversation) *model.Conversation { - conversationDB := &model.Conversation{} - if err := datautil.CopyStructFields(conversationDB, conversationPB); err != nil { + if conversationPB == nil { return nil } + conversationDB := &model.Conversation{ + OwnerUserID: conversationPB.OwnerUserID, + ConversationID: conversationPB.ConversationID, + RecvMsgOpt: conversationPB.RecvMsgOpt, + ConversationType: conversationPB.ConversationType, + UserID: conversationPB.UserID, + GroupID: conversationPB.GroupID, + IsPinned: conversationPB.IsPinned, + AttachedInfo: conversationPB.AttachedInfo, + IsPrivateChat: conversationPB.IsPrivateChat, + GroupAtType: conversationPB.GroupAtType, + Ex: conversationPB.Ex, + BurnDuration: conversationPB.BurnDuration, + MinSeq: conversationPB.MinSeq, + MaxSeq: conversationPB.MaxSeq, + MsgDestructTime: conversationPB.MsgDestructTime, + IsMsgDestruct: conversationPB.IsMsgDestruct, + } + if conversationPB.LatestMsgDestructTime != 0 { + conversationDB.LatestMsgDestructTime = time.UnixMilli(conversationPB.LatestMsgDestructTime) + } return conversationDB } func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversationsDB []*model.Conversation) { for _, conversationPB := range conversationsPB { - conversationDB := &model.Conversation{} - if err := datautil.CopyStructFields(conversationDB, conversationPB); err != nil { + if conversationPB == nil { continue } + conversationDB := &model.Conversation{ + OwnerUserID: conversationPB.OwnerUserID, + ConversationID: conversationPB.ConversationID, + RecvMsgOpt: conversationPB.RecvMsgOpt, + ConversationType: conversationPB.ConversationType, + UserID: conversationPB.UserID, + GroupID: conversationPB.GroupID, + IsPinned: conversationPB.IsPinned, + AttachedInfo: conversationPB.AttachedInfo, + IsPrivateChat: conversationPB.IsPrivateChat, + GroupAtType: conversationPB.GroupAtType, + Ex: conversationPB.Ex, + BurnDuration: conversationPB.BurnDuration, + MinSeq: conversationPB.MinSeq, + MaxSeq: conversationPB.MaxSeq, + MsgDestructTime: conversationPB.MsgDestructTime, + IsMsgDestruct: conversationPB.IsMsgDestruct, + } + if conversationPB.LatestMsgDestructTime != 0 { + conversationDB.LatestMsgDestructTime = time.UnixMilli(conversationPB.LatestMsgDestructTime) + } conversationsDB = append(conversationsDB, conversationDB) } return conversationsDB diff --git a/pkg/common/convert/friend.go b/pkg/common/convert/friend.go index e783ecb24..e9002d744 100644 --- a/pkg/common/convert/friend.go +++ b/pkg/common/convert/friend.go @@ -21,18 +21,22 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" "github.com/openimsdk/open-im-server/v3/pkg/notification/common_user" "github.com/openimsdk/protocol/relation" - "github.com/openimsdk/protocol/sdkws" "github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/timeutil" ) func FriendPb2DB(friend *sdkws.FriendInfo) *model.Friend { - dbFriend := &model.Friend{} - err := datautil.CopyStructFields(dbFriend, friend) - if err != nil { + if friend == nil { return nil } + dbFriend := &model.Friend{} + dbFriend.OwnerUserID = friend.OwnerUserID + dbFriend.Remark = friend.Remark + dbFriend.AddSource = friend.AddSource + dbFriend.OperatorUserID = friend.OperatorUserID + dbFriend.Ex = friend.Ex + dbFriend.IsPinned = friend.IsPinned dbFriend.FriendUserID = friend.FriendUser.UserID dbFriend.CreateTime = timeutil.UnixSecondToTime(friend.CreateTime) return dbFriend @@ -69,10 +73,12 @@ func FriendsDB2Pb(ctx context.Context, friendsDB []*model.Friend, getUsers func( } for _, friend := range friendsDB { friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - err := datautil.CopyStructFields(friendPb, friend) - if err != nil { - return nil, err - } + friendPb.OwnerUserID = friend.OwnerUserID + friendPb.Remark = friend.Remark + friendPb.AddSource = friend.AddSource + friendPb.OperatorUserID = friend.OperatorUserID + friendPb.Ex = friend.Ex + friendPb.IsPinned = friend.IsPinned friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname diff --git a/pkg/common/storage/controller/conversation.go b/pkg/common/storage/controller/conversation.go index d4088e0c0..2027e389b 100644 --- a/pkg/common/storage/controller/conversation.go +++ b/pkg/common/storage/controller/conversation.go @@ -127,13 +127,10 @@ func (c *conversationDatabase) SetUsersConversationFieldTx(ctx context.Context, var conversations []*relationtb.Conversation now := time.Now() for _, v := range NotUserIDs { - temp := new(relationtb.Conversation) - if err = datautil.CopyStructFields(temp, conversation); err != nil { - return err - } + temp := *conversation temp.OwnerUserID = v temp.CreateTime = now - conversations = append(conversations, temp) + conversations = append(conversations, &temp) } if len(conversations) > 0 { err = c.conversationDB.Create(ctx, conversations)