Unverified 提交 952a1432 作者: Whyrusleeping 提交者: GitHub

Merge pull request #4471 from ipfs/feat/coreapi/dag

RFC: coreapi.Dag
......@@ -25,6 +25,10 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
return (*UnixfsAPI)(api)
}
func (api *CoreAPI) Dag() coreiface.DagAPI {
return &DagAPI{api, nil}
}
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
p, err := api.ResolvePath(ctx, p)
if err != nil {
......
package coreapi
import (
"context"
"fmt"
"io"
gopath "path"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
coredag "github.com/ipfs/go-ipfs/core/coredag"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)
type DagAPI struct {
*CoreAPI
*caopts.DagOptions
}
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
settings, err := caopts.DagPutOptions(opts...)
if err != nil {
return nil, err
}
codec, ok := cid.CodecToStr[settings.Codec]
if !ok {
return nil, fmt.Errorf("invalid codec %d", settings.Codec)
}
nds, err := coredag.ParseInputs(settings.InputEnc, codec, src, settings.MhType, settings.MhLength)
if err != nil {
return nil, err
}
if len(nds) == 0 {
return nil, fmt.Errorf("no node returned from ParseInputs")
}
_, err = api.node.DAG.Add(nds[0])
if err != nil {
return nil, err
}
return ParseCid(nds[0].Cid()), nil
}
func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
return api.core().ResolveNode(ctx, path)
}
func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.DagTreeOption) ([]coreiface.Path, error) {
settings, err := caopts.DagTreeOptions(opts...)
if err != nil {
return nil, err
}
n, err := api.Get(ctx, p)
if err != nil {
return nil, err
}
paths := n.Tree("", settings.Depth)
out := make([]coreiface.Path, len(paths))
for n, p2 := range paths {
out[n], err = ParsePath(gopath.Join(p.String(), p2))
if err != nil {
return nil, err
}
}
return out, nil
}
func (api *DagAPI) core() coreiface.CoreAPI {
return api.CoreAPI
}
package coreapi_test
import (
"context"
"path"
"strings"
"testing"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
)
var (
treeExpected = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"c/d": {},
"c/e": {},
}
)
func TestPut(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`))
if err != nil {
t.Error(err)
}
if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}
func TestPutWithHash(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), api.Dag().WithHash(mh.ID, -1))
if err != nil {
t.Error(err)
}
if res.Cid().String() != "z5hRLNd2sv4z1c" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}
func TestPath(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`))
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`))
if err != nil {
t.Error(err)
}
p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
nd, err := api.Dag().Get(ctx, p)
if err != nil {
t.Error(err)
}
if nd.Cid().String() != sub.Cid().String() {
t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
}
}
func TestTree(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
if err != nil {
t.Error(err)
}
res, err := api.Dag().Get(ctx, c)
if err != nil {
t.Error(err)
}
lst := res.Tree("", -1)
if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
}
for _, ent := range lst {
if _, ok := treeExpected[ent]; !ok {
t.Errorf("unexpected tree entry %s", ent)
}
}
}
......@@ -7,6 +7,8 @@ import (
"errors"
"io"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
ipld "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)
......@@ -34,6 +36,7 @@ type Reader interface {
type CoreAPI interface {
// Unixfs returns an implementation of Unixfs API
Unixfs() UnixfsAPI
Dag() DagAPI
// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, Path) (Path, error)
......@@ -55,6 +58,38 @@ type UnixfsAPI interface {
Ls(context.Context, Path) ([]*Link, error)
}
// DagAPI specifies the interface to IPLD
type DagAPI interface {
// Put inserts data using specified format and input encoding.
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
// "sha256" are used.
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
WithInputEnc(enc string) options.DagPutOption
// WithCodec is an option for Put which specifies the multicodec to use to
// serialize the object. Default is cid.DagCBOR (0x71)
WithCodec(codec uint64) options.DagPutOption
// WithHash is an option for Put which specifies the multihash settings to use
// when hashing the object. Default is based on the codec used
// (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
// the hash will be used
WithHash(mhType uint64, mhLen int) options.DagPutOption
// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (Node, error)
// Tree returns list of paths within a node specified by the path.
Tree(ctx context.Context, path Path, opts ...options.DagTreeOption) ([]Path, error)
// WithDepth is an option for Tree which specifies maximum depth of the
// returned tree. Default is -1 (no depth limit)
WithDepth(depth int) options.DagTreeOption
}
// type ObjectAPI interface {
// New() (cid.Cid, Object)
// Get(string) (Object, error)
......
package options
import (
"math"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)
type DagPutSettings struct {
InputEnc string
Codec uint64
MhType uint64
MhLength int
}
type DagTreeSettings struct {
Depth int
}
type DagPutOption func(*DagPutSettings) error
type DagTreeOption func(*DagTreeSettings) error
func DagPutOptions(opts ...DagPutOption) (*DagPutSettings, error) {
options := &DagPutSettings{
InputEnc: "json",
Codec: cid.DagCBOR,
MhType: math.MaxUint64,
MhLength: -1,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) {
options := &DagTreeSettings{
Depth: -1,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type DagOptions struct{}
func (api *DagOptions) WithInputEnc(enc string) DagPutOption {
return func(settings *DagPutSettings) error {
settings.InputEnc = enc
return nil
}
}
func (api *DagOptions) WithCodec(codec uint64) DagPutOption {
return func(settings *DagPutSettings) error {
settings.Codec = codec
return nil
}
}
func (api *DagOptions) WithHash(mhType uint64, mhLen int) DagPutOption {
return func(settings *DagPutSettings) error {
settings.MhType = mhType
settings.MhLength = mhLen
return nil
}
}
func (api *DagOptions) WithDepth(depth int) DagTreeOption {
return func(settings *DagTreeSettings) error {
settings.Depth = depth
return nil
}
}
......@@ -17,6 +17,7 @@ import (
config "github.com/ipfs/go-ipfs/repo/config"
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
unixfs "github.com/ipfs/go-ipfs/unixfs"
cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"
)
......@@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
// `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
r := &repo.Mock{
C: config.Config{
Identity: config.Identity{
......@@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
if err != nil {
return nil, nil, err
}
api := coreapi.NewCoreAPI(node).Unixfs()
api := coreapi.NewCoreAPI(node)
return node, api, nil
}
......@@ -55,7 +56,7 @@ func TestAdd(t *testing.T) {
}
str := strings.NewReader(helloStr)
p, err := api.Add(ctx, str)
p, err := api.Unixfs().Add(ctx, str)
if err != nil {
t.Error(err)
}
......@@ -64,7 +65,7 @@ func TestAdd(t *testing.T) {
t.Fatalf("expected path %s, got: %s", hello, p)
}
r, err := api.Cat(ctx, hello)
r, err := api.Unixfs().Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
......@@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) {
}
str := strings.NewReader("")
p, err := api.Add(ctx, str)
p, err := api.Unixfs().Add(ctx, str)
if err != nil {
t.Error(err)
}
......@@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) {
t.Fatalf("expected CID %s, got: %s", hello, p)
}
r, err := api.Cat(ctx, hello)
r, err := api.Unixfs().Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
......@@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) {
t.Fatal(err)
}
r, err := api.Cat(ctx, emptyFile)
r, err := api.Unixfs().Cat(ctx, emptyFile)
if err != nil {
t.Fatal(err)
}
......@@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) {
t.Fatalf("expected path %s, got: %s", emptyDir, p)
}
_, err = api.Cat(ctx, emptyDir)
_, err = api.Unixfs().Cat(ctx, emptyDir)
if err != coreiface.ErrIsDir {
t.Fatalf("expected ErrIsDir, got: %s", err)
}
......@@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) {
t.Error(err)
}
_, err = api.Cat(ctx, coreapi.ParseCid(c))
_, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c))
if !strings.Contains(err.Error(), "proto: required field") {
t.Fatalf("expected protobuf error, got: %s", err)
}
......@@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) {
t.Error(err)
}
_, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
_, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
if err != coreiface.ErrOffline {
t.Fatalf("expected ErrOffline, got: %s", err)
}
......@@ -229,7 +230,7 @@ func TestLs(t *testing.T) {
}
p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)
links, err := api.Ls(ctx, p)
links, err := api.Unixfs().Ls(ctx, p)
if err != nil {
t.Error(err)
}
......@@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) {
t.Error(err)
}
links, err := api.Ls(ctx, emptyDir)
links, err := api.Unixfs().Ls(ctx, emptyDir)
if err != nil {
t.Error(err)
}
......@@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err)
}
links, err := api.Ls(ctx, coreapi.ParseCid(c))
links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c))
if err != nil {
t.Error(err)
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论