-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for podsi indexing --------- Co-authored-by: Will <[email protected]> Co-authored-by: Jacob Heun <[email protected]> Co-authored-by: dirkmc <[email protected]> Co-authored-by: Anton Evangelatov <[email protected]> Co-authored-by: Jacob Heun <[email protected]> Co-authored-by: Rod Vagg <[email protected]> Co-authored-by: Łukasz Magiera <[email protected]> Co-authored-by: Łukasz Magiera <[email protected]> Co-authored-by: Hannah Howard <[email protected]> Co-authored-by: gammazero <[email protected]> Co-authored-by: Adin Schmahmann <[email protected]> Co-authored-by: Masih H. Derkani <[email protected]> Co-authored-by: Ivan Schasny <[email protected]> Co-authored-by: Ivan Schasny <[email protected]>
- Loading branch information
1 parent
e9d18ac
commit 37e4d80
Showing
31 changed files
with
1,378 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package car | ||
|
||
import ( | ||
"io" | ||
"sort" | ||
) | ||
|
||
func NewMultiReaderAt(parts ...ReaderAtSize) io.ReaderAt { | ||
m := &multiReaderAt{ | ||
parts: make([]readerAtOffset, 0, len(parts)), | ||
} | ||
var off int64 | ||
for _, p := range parts { | ||
rao := readerAtOffset{off, p} | ||
m.parts = append(m.parts, rao) | ||
off += rao.Size() | ||
} | ||
m.size = off | ||
return m | ||
} | ||
|
||
type ReaderAtSize interface { | ||
io.ReaderAt | ||
Size() int64 | ||
} | ||
|
||
type readerAtOffset struct { | ||
off int64 | ||
ReaderAtSize | ||
} | ||
|
||
type multiReaderAt struct { | ||
parts []readerAtOffset | ||
size int64 | ||
} | ||
|
||
func (m *multiReaderAt) Size() int64 { | ||
return m.size | ||
} | ||
|
||
func (m *multiReaderAt) ReadAt(p []byte, off int64) (n int, err error) { | ||
wantN := len(p) | ||
|
||
// Skip past the requested offset. | ||
skipParts := sort.Search(len(m.parts), func(i int) bool { | ||
// This function returns whether parts[i] will | ||
// contribute any bytes to our output. | ||
part := m.parts[i] | ||
return part.off+part.Size() > off | ||
}) | ||
parts := m.parts[skipParts:] | ||
|
||
// How far to skip in the first part. | ||
needSkip := off | ||
if len(parts) > 0 { | ||
needSkip -= parts[0].off | ||
} | ||
|
||
for len(parts) > 0 && len(p) > 0 { | ||
readP := p | ||
partSize := parts[0].Size() | ||
if int64(len(readP)) > partSize-needSkip { | ||
readP = readP[:partSize-needSkip] | ||
} | ||
pn, err0 := parts[0].ReadAt(readP, needSkip) | ||
if err0 != nil { | ||
return n, err0 | ||
} | ||
n += pn | ||
p = p[pn:] | ||
if int64(pn)+needSkip == partSize { | ||
parts = parts[1:] | ||
} | ||
needSkip = 0 | ||
} | ||
|
||
if n != wantN { | ||
err = io.ErrUnexpectedEOF | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package car | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"fmt" | ||
"io" | ||
mrand "math/rand" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMultiReaderAt(t *testing.T) { | ||
req := require.New(t) | ||
|
||
sourceData := make([]byte, 4<<20) | ||
_, err := rand.Read(sourceData) | ||
req.NoError(err) | ||
|
||
testRead := func(t *testing.T, mra io.ReaderAt, readLen int, pos int) { | ||
// t.Logf("testRead() readLen=%d pos=%d", readLen, pos) | ||
req := require.New(t) | ||
readData := make([]byte, readLen) | ||
n, err := mra.ReadAt(readData, int64(pos)) | ||
req.NoError(err) | ||
req.Equal(readLen, n) | ||
req.True(bytes.Equal(sourceData[pos:pos+readLen], readData)) | ||
} | ||
|
||
for _, testCase := range [][]int{ | ||
{1}, | ||
{8}, | ||
{10}, | ||
{1000}, | ||
{1024}, | ||
{2000}, | ||
{1 << 20}, | ||
{10, 10}, | ||
{1 << 20, 1 << 20}, | ||
{10, 10, 10}, | ||
{1 << 20, 1 << 20, 1 << 20}, | ||
{1, 1, 1, 1, 1}, | ||
{8, 1, 8, 1, 8}, | ||
{1000, 8, 10, 1000}, | ||
{1000, 2000, 2000, 1000}, | ||
{1000, 2000, 2000, 8, 1000}, | ||
{8, 2000, 1024, 1 << 20, 1000}, | ||
} { | ||
var sb strings.Builder | ||
for ii, sz := range testCase { | ||
if ii > 0 { | ||
sb.WriteString("_") | ||
} | ||
sb.WriteString(fmt.Sprintf("%d", sz)) | ||
} | ||
|
||
t.Run(sb.String(), func(t *testing.T) { | ||
testLen := 0 | ||
ra := make([]ReaderAtSize, len(testCase)) | ||
for ii, sz := range testCase { | ||
ra[ii] = bytes.NewReader(sourceData[testLen : testLen+sz]) | ||
testLen += sz | ||
} | ||
mra := NewMultiReaderAt(ra...) | ||
// read all | ||
testRead(t, mra, testLen, 0) | ||
// read at random positions | ||
for ii := 0; ii < 100; ii++ { | ||
pos := mrand.Intn(testLen) | ||
readLen := mrand.Intn(testLen - pos) | ||
testRead(t, mra, readLen, pos) | ||
} | ||
// read blocks | ||
off := 0 | ||
for _, sz := range testCase { | ||
testRead(t, mra, sz, off) | ||
off += sz | ||
} | ||
// read just outsize of blocks | ||
off = 0 | ||
for ii, sz := range testCase { | ||
pos := off | ||
rd := sz | ||
if ii > 0 { | ||
rd++ | ||
off-- | ||
} | ||
if off < testLen { | ||
rd++ | ||
} | ||
if rd > testLen-pos { | ||
rd = testLen - pos | ||
} | ||
testRead(t, mra, rd, pos) | ||
off += sz | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.