seaweedfs/weed/util/log_buffer/sealed_buffer.go

66 lines
1.5 KiB
Go
Raw Normal View History

2020-04-11 19:37:41 +00:00
package log_buffer
2020-04-11 09:19:45 +00:00
2020-04-30 09:19:32 +00:00
import (
"fmt"
"time"
)
2020-04-11 09:19:45 +00:00
type MemBuffer struct {
buf []byte
size int
2020-04-11 09:19:45 +00:00
startTime time.Time
stopTime time.Time
2024-01-08 08:03:08 +00:00
batchIndex int64
2020-04-11 09:19:45 +00:00
}
type SealedBuffers struct {
buffers []*MemBuffer
}
func newSealedBuffers(size int) *SealedBuffers {
sbs := &SealedBuffers{}
sbs.buffers = make([]*MemBuffer, size)
for i := 0; i < size; i++ {
sbs.buffers[i] = &MemBuffer{
buf: make([]byte, BufferSize),
}
}
return sbs
}
2024-01-08 08:03:08 +00:00
func (sbs *SealedBuffers) SealBuffer(startTime, stopTime time.Time, buf []byte, pos int, batchIndex int64) (newBuf []byte) {
2020-04-11 09:19:45 +00:00
oldMemBuffer := sbs.buffers[0]
size := len(sbs.buffers)
for i := 0; i < size-1; i++ {
sbs.buffers[i].buf = sbs.buffers[i+1].buf
sbs.buffers[i].size = sbs.buffers[i+1].size
2020-04-11 09:19:45 +00:00
sbs.buffers[i].startTime = sbs.buffers[i+1].startTime
sbs.buffers[i].stopTime = sbs.buffers[i+1].stopTime
2024-01-08 08:03:08 +00:00
sbs.buffers[i].batchIndex = sbs.buffers[i+1].batchIndex
2020-04-11 09:19:45 +00:00
}
sbs.buffers[size-1].buf = buf
sbs.buffers[size-1].size = pos
2020-04-11 09:19:45 +00:00
sbs.buffers[size-1].startTime = startTime
sbs.buffers[size-1].stopTime = stopTime
2024-01-08 08:03:08 +00:00
sbs.buffers[size-1].batchIndex = batchIndex
2020-04-11 09:19:45 +00:00
return oldMemBuffer.buf
}
func (mb *MemBuffer) locateByTs(lastReadTime time.Time) (pos int) {
lastReadTs := lastReadTime.UnixNano()
for pos < len(mb.buf) {
size, t := readTs(mb.buf, pos)
if t > lastReadTs {
return
}
pos += size + 4
}
return len(mb.buf)
}
2020-04-30 09:19:32 +00:00
func (mb *MemBuffer) String() string {
return fmt.Sprintf("[%v,%v] bytes:%d", mb.startTime, mb.stopTime, mb.size)
2020-05-10 10:50:30 +00:00
}