2021-10-16 23:03:16 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
2023-01-03 07:20:45 +00:00
|
|
|
"container/list"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2022-04-18 02:35:43 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2021-10-16 23:03:16 +00:00
|
|
|
)
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOffset int64) (visibles *IntervalList[*VisibleInterval]) {
|
2021-10-16 23:03:16 +00:00
|
|
|
|
|
|
|
var points []*Point
|
|
|
|
for _, chunk := range chunks {
|
2023-01-03 07:20:45 +00:00
|
|
|
if chunk.IsChunkManifest {
|
|
|
|
println("This should not happen! A manifest chunk found:", chunk.GetFileIdString())
|
|
|
|
}
|
|
|
|
start, stop := max(chunk.Offset, startOffset), min(chunk.Offset+int64(chunk.Size), stopOffset)
|
|
|
|
if start >= stop {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-16 23:03:16 +00:00
|
|
|
points = append(points, &Point{
|
|
|
|
x: chunk.Offset,
|
2022-10-28 19:53:19 +00:00
|
|
|
ts: chunk.ModifiedTsNs,
|
2021-10-16 23:03:16 +00:00
|
|
|
chunk: chunk,
|
|
|
|
isStart: true,
|
|
|
|
})
|
|
|
|
points = append(points, &Point{
|
|
|
|
x: chunk.Offset + int64(chunk.Size),
|
2022-10-28 19:53:19 +00:00
|
|
|
ts: chunk.ModifiedTsNs,
|
2021-10-16 23:03:16 +00:00
|
|
|
chunk: chunk,
|
|
|
|
isStart: false,
|
|
|
|
})
|
|
|
|
}
|
2023-09-18 21:43:05 +00:00
|
|
|
slices.SortFunc(points, func(a, b *Point) int {
|
2022-04-18 02:35:43 +00:00
|
|
|
if a.x != b.x {
|
2023-09-18 21:43:05 +00:00
|
|
|
return int(a.x - b.x)
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
2022-04-18 02:35:43 +00:00
|
|
|
if a.ts != b.ts {
|
2023-09-18 21:43:05 +00:00
|
|
|
return int(a.ts - b.ts)
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
2023-09-18 21:43:05 +00:00
|
|
|
if a.isStart {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if b.isStart {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
2021-10-16 23:03:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var prevX int64
|
2023-01-03 07:20:45 +00:00
|
|
|
queue := list.New() // points with higher ts are at the tail
|
|
|
|
visibles = NewIntervalList[*VisibleInterval]()
|
|
|
|
var prevPoint *Point
|
2021-10-16 23:03:16 +00:00
|
|
|
for _, point := range points {
|
2023-01-03 07:20:45 +00:00
|
|
|
if queue.Len() > 0 {
|
|
|
|
prevPoint = queue.Back().Value.(*Point)
|
|
|
|
} else {
|
|
|
|
prevPoint = nil
|
|
|
|
}
|
2021-10-16 23:03:16 +00:00
|
|
|
if point.isStart {
|
2023-01-03 07:20:45 +00:00
|
|
|
if prevPoint != nil {
|
|
|
|
if point.x != prevX && prevPoint.ts < point.ts {
|
|
|
|
addToVisibles(visibles, prevX, prevPoint, point)
|
2021-10-16 23:03:16 +00:00
|
|
|
prevX = point.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// insert into queue
|
2023-01-03 07:20:45 +00:00
|
|
|
if prevPoint == nil || prevPoint.ts < point.ts {
|
|
|
|
queue.PushBack(point)
|
|
|
|
prevX = point.x
|
|
|
|
} else {
|
|
|
|
for e := queue.Front(); e != nil; e = e.Next() {
|
|
|
|
if e.Value.(*Point).ts > point.ts {
|
|
|
|
queue.InsertBefore(point, e)
|
|
|
|
break
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-03 07:20:45 +00:00
|
|
|
isLast := true
|
|
|
|
for e := queue.Back(); e != nil; e = e.Prev() {
|
|
|
|
if e.Value.(*Point).ts == point.ts {
|
|
|
|
queue.Remove(e)
|
2021-10-16 23:03:16 +00:00
|
|
|
break
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
isLast = false
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
if isLast && prevPoint != nil {
|
|
|
|
addToVisibles(visibles, prevX, prevPoint, point)
|
2021-10-16 23:03:16 +00:00
|
|
|
prevX = point.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func addToVisibles(visibles *IntervalList[*VisibleInterval], prevX int64, startPoint *Point, point *Point) {
|
2021-10-16 23:03:16 +00:00
|
|
|
if prevX < point.x {
|
|
|
|
chunk := startPoint.chunk
|
2023-01-03 07:20:45 +00:00
|
|
|
visible := &VisibleInterval{
|
|
|
|
start: prevX,
|
|
|
|
stop: point.x,
|
|
|
|
fileId: chunk.GetFileIdString(),
|
|
|
|
modifiedTsNs: chunk.ModifiedTsNs,
|
|
|
|
offsetInChunk: prevX - chunk.Offset,
|
|
|
|
chunkSize: chunk.Size,
|
|
|
|
cipherKey: chunk.CipherKey,
|
|
|
|
isGzipped: chunk.IsCompressed,
|
|
|
|
}
|
|
|
|
appendVisibleInterfal(visibles, visible)
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendVisibleInterfal(visibles *IntervalList[*VisibleInterval], visible *VisibleInterval) {
|
|
|
|
visibles.AppendInterval(&Interval[*VisibleInterval]{
|
|
|
|
StartOffset: visible.start,
|
|
|
|
StopOffset: visible.stop,
|
|
|
|
TsNs: visible.modifiedTsNs,
|
|
|
|
Value: visible,
|
|
|
|
})
|
2021-10-16 23:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Point struct {
|
|
|
|
x int64
|
|
|
|
ts int64
|
|
|
|
chunk *filer_pb.FileChunk
|
|
|
|
isStart bool
|
|
|
|
}
|