@ -18,11 +18,12 @@ import (
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"errors"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
"strings"
"strings"
"time"
"time"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
@ -97,8 +98,10 @@ type CommonMsgDatabase interface {
ConvertMsgsDocLen ( ctx context . Context , conversationIDs [ ] string )
ConvertMsgsDocLen ( ctx context . Context , conversationIDs [ ] string )
// clear msg
// clear msg
GetBeforeMsg ( ctx context . Context , ts int64 , limit int ) ( [ ] * model . MsgDocModel , error )
GetBeforeMsg ( ctx context . Context , ts int64 , docIds [ ] string , limit int ) ( [ ] * model . MsgDocModel , error )
DeleteDocMsgBefore ( ctx context . Context , ts int64 , doc * model . MsgDocModel ) ( [ ] int , error )
DeleteDocMsgBefore ( ctx context . Context , ts int64 , doc * model . MsgDocModel ) ( [ ] int , error )
GetDocIDs ( ctx context . Context ) ( [ ] string , error )
}
}
func NewCommonMsgDatabase ( msgDocModel database . Msg , msg cache . MsgCache , seqUser cache . SeqUser , seqConversation cache . SeqConversationCache , kafkaConf * config . Kafka ) ( CommonMsgDatabase , error ) {
func NewCommonMsgDatabase ( msgDocModel database . Msg , msg cache . MsgCache , seqUser cache . SeqUser , seqConversation cache . SeqConversationCache , kafkaConf * config . Kafka ) ( CommonMsgDatabase , error ) {
@ -912,8 +915,25 @@ func (db *commonMsgDatabase) ConvertMsgsDocLen(ctx context.Context, conversation
db . msgDocDatabase . ConvertMsgsDocLen ( ctx , conversationIDs )
db . msgDocDatabase . ConvertMsgsDocLen ( ctx , conversationIDs )
}
}
func ( db * commonMsgDatabase ) GetBeforeMsg ( ctx context . Context , ts int64 , limit int ) ( [ ] * model . MsgDocModel , error ) {
func ( db * commonMsgDatabase ) GetBeforeMsg ( ctx context . Context , ts int64 , docIDs [ ] string , limit int ) ( [ ] * model . MsgDocModel , error ) {
return db . msgDocDatabase . GetBeforeMsg ( ctx , ts , limit )
var msgs [ ] * model . MsgDocModel
for i := 0 ; i < len ( docIDs ) ; i += 1000 {
end := i + 1000
if end > len ( docIDs ) {
end = len ( docIDs )
}
res , err := db . msgDocDatabase . GetBeforeMsg ( ctx , ts , docIDs [ i : end ] , limit )
if err != nil {
return nil , err
}
msgs = append ( msgs , res ... )
if len ( msgs ) >= limit {
return msgs [ : limit ] , nil
}
}
return msgs , nil
}
}
func ( db * commonMsgDatabase ) DeleteDocMsgBefore ( ctx context . Context , ts int64 , doc * model . MsgDocModel ) ( [ ] int , error ) {
func ( db * commonMsgDatabase ) DeleteDocMsgBefore ( ctx context . Context , ts int64 , doc * model . MsgDocModel ) ( [ ] int , error ) {
@ -936,8 +956,10 @@ func (db *commonMsgDatabase) DeleteDocMsgBefore(ctx context.Context, ts int64, d
return index , err
return index , err
}
}
if len ( index ) == notNull {
if len ( index ) == notNull {
log . ZDebug ( ctx , "Delete db in Doc" , "DocID" , doc . DocID , "index" , index , "maxSeq" , maxSeq )
return index , db . msgDocDatabase . DeleteDoc ( ctx , doc . DocID )
return index , db . msgDocDatabase . DeleteDoc ( ctx , doc . DocID )
} else {
} else {
log . ZDebug ( ctx , "delete db in index" , "DocID" , doc . DocID , "index" , index , "maxSeq" , maxSeq )
return index , db . msgDocDatabase . DeleteMsgByIndex ( ctx , doc . DocID , index )
return index , db . msgDocDatabase . DeleteMsgByIndex ( ctx , doc . DocID , index )
}
}
}
}
@ -955,3 +977,7 @@ func (db *commonMsgDatabase) setMinSeq(ctx context.Context, conversationID strin
}
}
return db . seqConversation . SetMinSeq ( ctx , conversationID , seq )
return db . seqConversation . SetMinSeq ( ctx , conversationID , seq )
}
}
func ( db * commonMsgDatabase ) GetDocIDs ( ctx context . Context ) ( [ ] string , error ) {
return db . msgDocDatabase . GetDocIDs ( ctx )
}