-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial-match traversal of large bytes (#375)
Implement an optional `LargeBytesNode` for an `AsLargeBytes() (io.ReadSeeker, error)` method. This allows, per the [selector spec](ipld/ipld#184) the ability to efficiently traverse sub-ranges of ADLs.
- Loading branch information
Showing
19 changed files
with
349 additions
and
8 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 basicnode | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
"github.com/ipld/go-ipld-prime/node/mixins" | ||
) | ||
|
||
var ( | ||
_ datamodel.Node = streamBytes{nil} | ||
_ datamodel.NodePrototype = Prototype__Bytes{} | ||
_ datamodel.NodeBuilder = &plainBytes__Builder{} | ||
_ datamodel.NodeAssembler = &plainBytes__Assembler{} | ||
) | ||
|
||
func NewBytesFromReader(rs io.ReadSeeker) datamodel.Node { | ||
return streamBytes{rs} | ||
} | ||
|
||
// streamBytes is a boxed reader that complies with datamodel.Node. | ||
type streamBytes struct { | ||
io.ReadSeeker | ||
} | ||
|
||
// -- Node interface methods --> | ||
|
||
func (streamBytes) Kind() datamodel.Kind { | ||
return datamodel.Kind_Bytes | ||
} | ||
func (streamBytes) LookupByString(string) (datamodel.Node, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.LookupByString("") | ||
} | ||
func (streamBytes) LookupByNode(key datamodel.Node) (datamodel.Node, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.LookupByNode(nil) | ||
} | ||
func (streamBytes) LookupByIndex(idx int64) (datamodel.Node, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.LookupByIndex(0) | ||
} | ||
func (streamBytes) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.LookupBySegment(seg) | ||
} | ||
func (streamBytes) MapIterator() datamodel.MapIterator { | ||
return nil | ||
} | ||
func (streamBytes) ListIterator() datamodel.ListIterator { | ||
return nil | ||
} | ||
func (streamBytes) Length() int64 { | ||
return -1 | ||
} | ||
func (streamBytes) IsAbsent() bool { | ||
return false | ||
} | ||
func (streamBytes) IsNull() bool { | ||
return false | ||
} | ||
func (streamBytes) AsBool() (bool, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.AsBool() | ||
} | ||
func (streamBytes) AsInt() (int64, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.AsInt() | ||
} | ||
func (streamBytes) AsFloat() (float64, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.AsFloat() | ||
} | ||
func (streamBytes) AsString() (string, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.AsString() | ||
} | ||
func (n streamBytes) AsBytes() ([]byte, error) { | ||
return io.ReadAll(n) | ||
} | ||
func (streamBytes) AsLink() (datamodel.Link, error) { | ||
return mixins.Bytes{TypeName: "bytes"}.AsLink() | ||
} | ||
func (streamBytes) Prototype() datamodel.NodePrototype { | ||
return Prototype__Bytes{} | ||
} | ||
func (n streamBytes) AsLargeBytes() (io.ReadSeeker, error) { | ||
return n.ReadSeeker, nil | ||
} |
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,12 @@ | ||
package basicnode_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipld/go-ipld-prime/node/basicnode" | ||
"github.com/ipld/go-ipld-prime/node/tests" | ||
) | ||
|
||
func TestBytes(t *testing.T) { | ||
tests.SpecTestBytes(t, basicnode.Prototype__Bytes{}) | ||
} |
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,35 @@ | ||
package tests | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
) | ||
|
||
func SpecTestBytes(t *testing.T, np datamodel.NodePrototype) { | ||
t.Run("byte node", func(t *testing.T) { | ||
nb := np.NewBuilder() | ||
err := nb.AssignBytes([]byte("asdf")) | ||
qt.Check(t, err, qt.IsNil) | ||
n := nb.Build() | ||
|
||
qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Bytes) | ||
qt.Check(t, n.IsNull(), qt.IsFalse) | ||
x, err := n.AsBytes() | ||
qt.Check(t, err, qt.IsNil) | ||
qt.Check(t, x, qt.DeepEquals, []byte("asdf")) | ||
|
||
lbn, ok := n.(datamodel.LargeBytesNode) | ||
if ok { | ||
str, err := lbn.AsLargeBytes() | ||
qt.Check(t, err, qt.IsNil) | ||
bytes, err := io.ReadAll(str) | ||
qt.Check(t, err, qt.IsNil) | ||
qt.Check(t, bytes, qt.DeepEquals, []byte("asdf")) | ||
} | ||
|
||
}) | ||
} |
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
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.