mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-30 07:25:37 +08:00
Related to #39095 https://go.dev/doc/modules/version-numbers Update pkg version according to golang dep version convention --------- Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package wab
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/milvus-io/milvus/pkg/v2/streaming/util/message"
|
|
)
|
|
|
|
// WriteAheadBufferReader is used to read messages from WriteAheadBuffer.
|
|
type WriteAheadBufferReader struct {
|
|
nextOffset int
|
|
lastTimeTick uint64
|
|
snapshot []messageWithOffset
|
|
underlyingBuf *WriteAheadBuffer
|
|
}
|
|
|
|
// Next returns the next message in the buffer.
|
|
func (r *WriteAheadBufferReader) Next(ctx context.Context) (message.ImmutableMessage, error) {
|
|
// Consume snapshot first.
|
|
if msg := r.nextFromSnapshot(); msg != nil {
|
|
return msg, nil
|
|
}
|
|
|
|
snapshot, err := r.underlyingBuf.createSnapshotFromOffset(ctx, r.nextOffset, r.lastTimeTick)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r.snapshot = snapshot
|
|
return r.nextFromSnapshot(), nil
|
|
}
|
|
|
|
// nextFromSnapshot returns the next message from the snapshot.
|
|
func (r *WriteAheadBufferReader) nextFromSnapshot() message.ImmutableMessage {
|
|
if len(r.snapshot) == 0 {
|
|
return nil
|
|
}
|
|
nextMsg := r.snapshot[0]
|
|
newNextOffset := nextMsg.Offset + 1
|
|
if newNextOffset < r.nextOffset {
|
|
panic("unreachable: next offset should be monotonically increasing")
|
|
}
|
|
r.nextOffset = newNextOffset
|
|
r.lastTimeTick = nextMsg.Message.TimeTick()
|
|
r.snapshot = r.snapshot[1:]
|
|
return nextMsg.Message
|
|
}
|