From cd4373824573400d62a37f3edc890a7c50bc137f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 18 Aug 2020 00:32:01 -0700 Subject: [PATCH] fix reading when filling zeros --- weed/filer2/reader_at.go | 4 ++-- weed/filer2/reader_at_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/weed/filer2/reader_at.go b/weed/filer2/reader_at.go index 84915f3f2..52460380c 100644 --- a/weed/filer2/reader_at.go +++ b/weed/filer2/reader_at.go @@ -82,8 +82,8 @@ func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) { } if startOffset < chunk.LogicOffset { gap := int(chunk.LogicOffset - startOffset) - glog.V(4).Infof("zero [%d,%d)", n, n+gap) - n += gap + glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap)) + n += int(min(int64(gap), remaining)) startOffset, remaining = chunk.LogicOffset, remaining-int64(gap) if remaining <= 0 { break diff --git a/weed/filer2/reader_at_test.go b/weed/filer2/reader_at_test.go index 6ead79b1e..f93d7ea11 100644 --- a/weed/filer2/reader_at_test.go +++ b/weed/filer2/reader_at_test.go @@ -124,3 +124,33 @@ func TestReaderAt0(t *testing.T) { testReadAt(t, readerAt, 10, 5, 0, io.EOF) } + +func TestReaderAt1(t *testing.T) { + + visibles := []VisibleInterval{ + { + start: 2, + stop: 5, + fileId: "1", + chunkSize: 9, + }, + } + + readerAt := &ChunkReadAt{ + chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64), + lookupFileId: nil, + readerLock: sync.Mutex{}, + fileSize: 20, + chunkCache: &mockChunkCache{}, + } + + testReadAt(t, readerAt, 0, 20, 20, io.EOF) + testReadAt(t, readerAt, 1, 7, 7, nil) + testReadAt(t, readerAt, 0, 1, 1, nil) + testReadAt(t, readerAt, 18, 4, 2, io.EOF) + testReadAt(t, readerAt, 12, 4, 4, nil) + testReadAt(t, readerAt, 4, 20, 16, io.EOF) + testReadAt(t, readerAt, 4, 10, 10, nil) + testReadAt(t, readerAt, 1, 10, 10, nil) + +}