fix ViewFromVisibleIntervals

This commit is contained in:
Chris Lu 2020-08-16 00:49:26 -07:00
parent aec7f32b02
commit 1d9ea30b72
2 changed files with 45 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import (
"sort" "sort"
"sync" "sync"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
) )
@ -134,17 +135,19 @@ func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int
for _, chunk := range visibles { for _, chunk := range visibles {
if chunk.start <= offset && offset < chunk.stop && offset < stop { glog.V(1).Infof("visible [%d,%d)", chunk.start, chunk.stop)
chunkStart, chunkStop := max(offset, chunk.start), min(stop, chunk.stop)
if chunkStart < chunkStop {
views = append(views, &ChunkView{ views = append(views, &ChunkView{
FileId: chunk.fileId, FileId: chunk.fileId,
Offset: offset - chunk.start, // offset is the data starting location in this file id Offset: chunkStart-chunk.start,
Size: uint64(min(chunk.stop, stop) - offset), Size: uint64(chunkStop - chunkStart),
LogicOffset: offset, LogicOffset: chunk.start,
ChunkSize: chunk.chunkSize, ChunkSize: chunk.chunkSize,
CipherKey: chunk.cipherKey, CipherKey: chunk.cipherKey,
IsGzipped: chunk.isGzipped, IsGzipped: chunk.isGzipped,
}) })
offset = min(chunk.stop, stop)
} }
} }
@ -266,3 +269,9 @@ func min(x, y int64) int64 {
} }
return y return y
} }
func max(x, y int64) int64 {
if x <= y {
return y
}
return x
}

View file

@ -2,9 +2,13 @@ package filer2
import ( import (
"log" "log"
"math"
"testing" "testing"
"fmt" "fmt"
"github.com/stretchr/testify/assert"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
) )
@ -418,3 +422,30 @@ func BenchmarkCompactFileChunks(b *testing.B) {
CompactFileChunks(nil, chunks) CompactFileChunks(nil, chunks)
} }
} }
func TestViewFromVisibleIntervals(t *testing.T) {
visibles := []VisibleInterval{
{
start: 0,
stop: 25,
fileId: "fid1",
},
{
start: 4096,
stop: 8192,
fileId: "fid2",
},
{
start: 16384,
stop: 18551,
fileId: "fid3",
},
}
views := ViewFromVisibleIntervals(visibles, 0, math.MaxInt32)
if len(views) != len(visibles) {
assert.Equal(t, len(visibles), len(views), "ViewFromVisibleIntervals error")
}
}