package common import ( "app.yhyue.com/moapp/MessageCenter/entity" "fmt" "github.com/RoaringBitmap/roaring" "sync" ) var rwMutex sync.RWMutex // 将Roaring Bitmap写入MySQL func writeBitmapToSendLog(rb *roaring.Bitmap, msgLogId int64) error { // 将Roaring Bitmap转换为字节数组 bytes, err := rb.ToBytes() if err != nil { return err } ok := entity.Mysql.Update("message_send_log", map[string]interface{}{"id": msgLogId}, map[string]interface{}{"baseUserIds": bytes}) if !ok { return fmt.Errorf("更新message_user_summary msg_log_id: %s 出错", msgLogId) } return nil } // 从MySQL中读取Roaring Bitmap func readBitmapFromSendLog(msgLogId int64) (*roaring.Bitmap, error) { // 从MySQL读取字节数组 var bytes []byte err := entity.Mysql.DB.QueryRow("SELECT baseUserIds FROM message_send_log WHERE `id` = ?", msgLogId).Scan(&bytes) if err != nil { return nil, err } // 将字节数组转换为Roaring Bitmap rb := roaring.NewBitmap() _, err = rb.FromBuffer(bytes) if err != nil { return nil, err } return rb, nil } // 更新用户消息汇总表 func writeBitmapToUserSummary(rb *roaring.Bitmap, userId string) error { allMsg, err := rb.ToBytes() if err != nil { return err } ok := entity.Mysql.Update("message_user_summary", map[string]interface{}{"userId": userId}, map[string]interface{}{ "allMsg": allMsg, }) if !ok { return fmt.Errorf("更新message_user_summary USERID: %s 出错", userId) } return nil } // 更新用户消息汇总表 -已读未读 func UpdateBitmapToUserUnread(rb2 *roaring.Bitmap, userId string) error { readMsg, err := rb2.ToBytes() if err != nil { return err } ok := entity.Mysql.Update("message_user_summary", map[string]interface{}{"userId": userId}, map[string]interface{}{ "readMsg": readMsg, }) if !ok { return fmt.Errorf("更新message_user_summary USERID: %s 出错", userId) } return nil } type MsgSummaryEntity struct { GroupId int Byte1 []byte }