-
Notifications
You must be signed in to change notification settings - Fork 54
Add NextReader to BlockReader #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |||||
| "errors" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "math" | ||||||
|
|
||||||
| blocks "github.com/ipfs/go-block-format" | ||||||
| "github.com/ipfs/go-cid" | ||||||
|
|
@@ -141,6 +142,23 @@ func (br *BlockReader) Next() (blocks.Block, error) { | |||||
| return blocks.NewBlockWithCid(data, c) | ||||||
| } | ||||||
|
|
||||||
| // NextReader returns a CID, io.Reader and length of what should be the next block | ||||||
| // the CID itself is not verified, and the reader is limited to the size of the block | ||||||
| // The user of this function HAS TO consume all of the bytes in the returned reader before using any other function | ||||||
| // on the BlockReader. | ||||||
| // The returned length might be larger than MaxAllowedSectionSize, it is up to the user to check before loading the data into memory. | ||||||
| func (br *BlockReader) NextReader() (cid.Cid, io.Reader, uint64, error) { | ||||||
| c, length, err := util.ReadNodeHeader(br.r, br.opts.ZeroLengthSectionAsEOF, math.MaxUint64) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or is there a reason you're using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the maxsize param is uint64 everywhere, so from an external API viewpoint, I'm passing unlimited (maximum allowed value). Then the function internally caps to what it is able to handle. Both perspectives make sense though |
||||||
| if err != nil { | ||||||
| return cid.Undef, nil, 0, err | ||||||
| } | ||||||
| limitReader := io.LimitReader(br.r, int64(length)) | ||||||
|
|
||||||
| ss := uint64(c.ByteLen()) + length | ||||||
| br.offset += uint64(varint.UvarintSize(ss)) + ss | ||||||
| return c, limitReader, length, nil | ||||||
| } | ||||||
|
|
||||||
| // BlockMetadata contains metadata about a block's section in a CAR file/stream. | ||||||
| // | ||||||
| // There are two offsets for the block section which will be the same if the | ||||||
|
|
@@ -171,26 +189,14 @@ type BlockMetadata struct { | |||||
| // If the underlying reader used by the BlockReader is actually a ReadSeeker, this method will attempt to | ||||||
| // seek over the underlying data rather than reading it into memory. | ||||||
| func (br *BlockReader) SkipNext() (*BlockMetadata, error) { | ||||||
| sectionSize, err := util.LdReadSize(br.r, br.opts.ZeroLengthSectionAsEOF, br.opts.MaxAllowedSectionSize) | ||||||
| c, blockSize, err := util.ReadNodeHeader(br.r, br.opts.ZeroLengthSectionAsEOF, br.opts.MaxAllowedSectionSize) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| if sectionSize == 0 { | ||||||
| _, _, err := cid.CidFromBytes([]byte{}) // generate zero-byte CID error | ||||||
| if err == nil { | ||||||
| panic("expected zero-byte CID error") | ||||||
| } | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| cidSize := uint64(c.ByteLen()) | ||||||
| sectionSize := blockSize + cidSize | ||||||
| lenSize := uint64(varint.UvarintSize(sectionSize)) | ||||||
|
|
||||||
| cidSize, c, err := cid.CidFromReader(io.LimitReader(br.r, int64(sectionSize))) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| blockSize := sectionSize - uint64(cidSize) | ||||||
| blockOffset := br.offset | ||||||
|
|
||||||
| // move our reader forward; either by seeking or slurping | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ package util | |||||||
| import ( | ||||||||
| "errors" | ||||||||
| "io" | ||||||||
| "math" | ||||||||
|
|
||||||||
| internalio "github.com/ipld/go-car/v2/internal/io" | ||||||||
|
|
||||||||
|
|
@@ -33,6 +34,33 @@ func ReadNode(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (cid.Cid, []b | |||||||
| return c, data[n:], nil | ||||||||
| } | ||||||||
|
|
||||||||
| // ReadNodeHeader returns the specified CID of the node and the length of data to be read. | ||||||||
| func ReadNodeHeader(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (cid.Cid, uint64, error) { | ||||||||
| size, err := LdReadSize(r, zeroLenAsEOF, maxReadBytes) | ||||||||
| if err != nil { | ||||||||
| return cid.Cid{}, 0, err | ||||||||
| } | ||||||||
|
|
||||||||
| if size == 0 { | ||||||||
| _, _, err := cid.CidFromBytes([]byte{}) // generate zero-byte CID error | ||||||||
| if err == nil { | ||||||||
| panic("expected zero-byte CID error") | ||||||||
| } | ||||||||
| return cid.Undef, 0, err | ||||||||
| } | ||||||||
|
|
||||||||
| if size > math.MaxInt64 { | ||||||||
| return cid.Cid{}, 0, ErrHeaderTooLarge | ||||||||
| } | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
this is unnecessary, it's what
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also if this really is necessary,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this because the int64 cast for LimitReader (don't want to cast to negatives) |
||||||||
| limitReader := io.LimitReader(r, int64(size)) | ||||||||
| n, c, err := cid.CidFromReader(limitReader) | ||||||||
| if err != nil { | ||||||||
| return cid.Cid{}, 0, err | ||||||||
| } | ||||||||
|
|
||||||||
| return c, size - uint64(n), nil | ||||||||
| } | ||||||||
|
|
||||||||
| func LdWrite(w io.Writer, d ...[]byte) error { | ||||||||
| var sum uint64 | ||||||||
| for _, s := range d { | ||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.