2024-06-27 07:11:05 +00:00
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
2024-08-07 02:34:16 +00:00
|
|
|
"github.com/milvus-io/milvus/pkg/streaming/proto/messagespb"
|
|
|
|
"github.com/milvus-io/milvus/pkg/streaming/proto/streamingpb"
|
2024-06-27 07:11:05 +00:00
|
|
|
"github.com/milvus-io/milvus/pkg/streaming/util/message"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-07-08 23:58:14 +00:00
|
|
|
DeliverPolicyTypeAll deliverPolicyType = 1
|
|
|
|
DeliverPolicyTypeLatest deliverPolicyType = 2
|
|
|
|
DeliverPolicyTypeStartFrom deliverPolicyType = 3
|
|
|
|
DeliverPolicyTypeStartAfter deliverPolicyType = 4
|
|
|
|
|
|
|
|
DeliverFilterTypeTimeTickGT deliverFilterType = 1
|
|
|
|
DeliverFilterTypeTimeTickGTE deliverFilterType = 2
|
|
|
|
DeliverFilterTypeVChannel deliverFilterType = 3
|
2024-06-27 07:11:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-07-08 23:58:14 +00:00
|
|
|
deliverPolicyType int
|
|
|
|
deliverFilterType int
|
2024-06-27 07:11:05 +00:00
|
|
|
)
|
|
|
|
|
2024-08-07 02:34:16 +00:00
|
|
|
type DeliverPolicy = *streamingpb.DeliverPolicy
|
2024-06-27 07:11:05 +00:00
|
|
|
|
|
|
|
// DeliverPolicyAll delivers all messages.
|
|
|
|
func DeliverPolicyAll() DeliverPolicy {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverPolicy{
|
|
|
|
Policy: &streamingpb.DeliverPolicy_All{},
|
2024-06-27 07:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverLatest delivers the latest message.
|
|
|
|
func DeliverPolicyLatest() DeliverPolicy {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverPolicy{
|
|
|
|
Policy: &streamingpb.DeliverPolicy_Latest{},
|
2024-06-27 07:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverEarliest delivers the earliest message.
|
|
|
|
func DeliverPolicyStartFrom(messageID message.MessageID) DeliverPolicy {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverPolicy{
|
|
|
|
Policy: &streamingpb.DeliverPolicy_StartFrom{
|
|
|
|
StartFrom: &messagespb.MessageID{
|
|
|
|
Id: messageID.Marshal(),
|
|
|
|
},
|
|
|
|
},
|
2024-06-27 07:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverPolicyStartAfter delivers the message after the specified message.
|
|
|
|
func DeliverPolicyStartAfter(messageID message.MessageID) DeliverPolicy {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverPolicy{
|
|
|
|
Policy: &streamingpb.DeliverPolicy_StartAfter{
|
|
|
|
StartAfter: &messagespb.MessageID{
|
|
|
|
Id: messageID.Marshal(),
|
|
|
|
},
|
|
|
|
},
|
2024-06-27 07:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 02:34:16 +00:00
|
|
|
type DeliverFilter = *streamingpb.DeliverFilter
|
2024-07-08 23:58:14 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// DeliverFilters
|
|
|
|
//
|
|
|
|
|
|
|
|
// DeliverFilterTimeTickGT delivers messages by time tick greater than the specified time tick.
|
|
|
|
func DeliverFilterTimeTickGT(timeTick uint64) DeliverFilter {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverFilter{
|
|
|
|
Filter: &streamingpb.DeliverFilter_TimeTickGt{
|
|
|
|
TimeTickGt: &streamingpb.DeliverFilterTimeTickGT{
|
|
|
|
TimeTick: timeTick,
|
|
|
|
},
|
|
|
|
},
|
2024-07-08 23:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverFilterTimeTickGTE delivers messages by time tick greater than or equal to the specified time tick.
|
|
|
|
func DeliverFilterTimeTickGTE(timeTick uint64) DeliverFilter {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverFilter{
|
|
|
|
Filter: &streamingpb.DeliverFilter_TimeTickGte{
|
|
|
|
TimeTickGte: &streamingpb.DeliverFilterTimeTickGTE{
|
|
|
|
TimeTick: timeTick,
|
|
|
|
},
|
|
|
|
},
|
2024-07-08 23:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeliverFilterVChannel delivers messages filtered by vchannel.
|
|
|
|
func DeliverFilterVChannel(vchannel string) DeliverFilter {
|
2024-08-07 02:34:16 +00:00
|
|
|
return &streamingpb.DeliverFilter{
|
|
|
|
Filter: &streamingpb.DeliverFilter_Vchannel{
|
|
|
|
Vchannel: &streamingpb.DeliverFilterVChannel{
|
|
|
|
Vchannel: vchannel,
|
|
|
|
},
|
|
|
|
},
|
2024-07-08 23:58:14 +00:00
|
|
|
}
|
2024-06-27 07:11:05 +00:00
|
|
|
}
|
2024-07-16 07:49:38 +00:00
|
|
|
|
|
|
|
// IsDeliverFilterTimeTick checks if the filter is time tick filter.
|
|
|
|
func IsDeliverFilterTimeTick(filter DeliverFilter) bool {
|
2024-08-07 02:34:16 +00:00
|
|
|
switch filter.GetFilter().(type) {
|
|
|
|
case *streamingpb.DeliverFilter_TimeTickGt, *streamingpb.DeliverFilter_TimeTickGte:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterFunc returns the filter function.
|
|
|
|
func GetFilterFunc(filters []DeliverFilter) (func(message.ImmutableMessage) bool, error) {
|
|
|
|
filterFuncs := make([]func(message.ImmutableMessage) bool, 0, len(filters))
|
|
|
|
for _, filter := range filters {
|
|
|
|
filter := filter
|
|
|
|
switch filter.GetFilter().(type) {
|
|
|
|
case *streamingpb.DeliverFilter_TimeTickGt:
|
|
|
|
filterFuncs = append(filterFuncs, func(im message.ImmutableMessage) bool {
|
|
|
|
return im.TimeTick() > filter.GetTimeTickGt().TimeTick
|
|
|
|
})
|
|
|
|
case *streamingpb.DeliverFilter_TimeTickGte:
|
|
|
|
filterFuncs = append(filterFuncs, func(im message.ImmutableMessage) bool {
|
|
|
|
return im.TimeTick() >= filter.GetTimeTickGte().TimeTick
|
|
|
|
})
|
|
|
|
case *streamingpb.DeliverFilter_Vchannel:
|
|
|
|
filterFuncs = append(filterFuncs, func(im message.ImmutableMessage) bool {
|
|
|
|
return im.VChannel() == filter.GetVchannel().Vchannel
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(msg message.ImmutableMessage) bool {
|
|
|
|
for _, f := range filterFuncs {
|
|
|
|
if !f(msg) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, nil
|
2024-07-16 07:49:38 +00:00
|
|
|
}
|