当前位置: 首页>>代码示例>>Golang>>正文


Golang merkledag.NodeWithData函数代码示例

本文整理汇总了Golang中github.com/ipfs/go-ipfs/merkledag.NodeWithData函数的典型用法代码示例。如果您正苦于以下问题:Golang NodeWithData函数的具体用法?Golang NodeWithData怎么用?Golang NodeWithData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NodeWithData函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testInsert

func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr string) {
	child := dag.NodeWithData([]byte(data))
	ck, err := e.tmp.Add(child)
	if err != nil {
		t.Fatal(err)
	}

	var c func() *dag.ProtoNode
	if create {
		c = func() *dag.ProtoNode {
			return &dag.ProtoNode{}
		}
	}

	err = e.InsertNodeAtPath(context.Background(), path, child, c)
	if experr != "" {
		var got string
		if err != nil {
			got = err.Error()
		}
		if got != experr {
			t.Fatalf("expected '%s' but got '%s'", experr, got)
		}
		return
	}

	if err != nil {
		t.Fatal(err, path, data, create, experr)
	}

	assertNodeAtPath(t, e.tmp, e.root, path, ck)
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:32,代码来源:utils_test.go

示例2: actorMakeFile

func actorMakeFile(d *Directory) error {
	d, err := randomWalk(d, rand.Intn(7))
	if err != nil {
		return err
	}

	name := randomName()
	f, err := NewFile(name, dag.NodeWithData(ft.FilePBData(nil, 0)), d, d.dserv)
	if err != nil {
		return err
	}

	wfd, err := f.Open(OpenWriteOnly, true)
	if err != nil {
		return err
	}

	rread := rand.New(rand.NewSource(time.Now().UnixNano()))
	r := io.LimitReader(rread, int64(77*rand.Intn(123)))
	_, err = io.Copy(wfd, r)
	if err != nil {
		return err
	}

	err = wfd.Close()
	if err != nil {
		return err
	}

	return nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:31,代码来源:mfs_test.go

示例3: TestAddLink

func TestAddLink(t *testing.T) {
	ds := mdtest.Mock()
	fishnode := dag.NodeWithData([]byte("fishcakes!"))

	fk, err := ds.Add(fishnode)
	if err != nil {
		t.Fatal(err)
	}

	nd := new(dag.ProtoNode)
	nnode, err := addLink(context.Background(), ds, nd, "fish", fishnode)
	if err != nil {
		t.Fatal(err)
	}

	fnprime, err := nnode.GetLinkedNode(context.Background(), ds, "fish")
	if err != nil {
		t.Fatal(err)
	}

	fnpkey := fnprime.Cid()
	if !fnpkey.Equals(fk) {
		t.Fatal("wrong child node found!")
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:25,代码来源:utils_test.go

示例4: newFan

func newFan(t *testing.T, ds mdag.DAGService) node.Node {
	a := mdag.NodeWithData([]byte("/a"))
	addLink(t, ds, a, child(t, ds, a, "aa"))
	addLink(t, ds, a, child(t, ds, a, "ab"))
	addLink(t, ds, a, child(t, ds, a, "ac"))
	addLink(t, ds, a, child(t, ds, a, "ad"))
	return a
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:8,代码来源:traverse_test.go

示例5: AddLink

// AddLink creates a unixfs symlink and returns its hash
func (s *Shell) AddLink(target string) (string, error) {
	d, _ := ft.SymlinkData(target)
	nd := dag.NodeWithData(d)
	c, err := s.node.DAG.Add(nd)
	if err != nil {
		return "", err
	}
	return c.String(), nil
}
开发者ID:noffle,项目名称:ipfs-embedded-shell,代码行数:10,代码来源:add.go

示例6: TestBadPBData

func TestBadPBData(t *testing.T) {
	dserv := testu.GetDAGServ()
	ctx, closer := context.WithCancel(context.Background())
	defer closer()

	node := mdag.NodeWithData([]byte{42})
	_, err := NewDagReader(ctx, node, dserv)
	if err == nil {
		t.Fatal("excepted error, got nil")
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:11,代码来源:dagreader_test.go

示例7: newLinkedList

func newLinkedList(t *testing.T, ds mdag.DAGService) node.Node {
	a := mdag.NodeWithData([]byte("/a"))
	aa := child(t, ds, a, "aa")
	aaa := child(t, ds, aa, "aaa")
	aaaa := child(t, ds, aaa, "aaaa")
	aaaaa := child(t, ds, aaaa, "aaaaa")
	addLink(t, ds, aaaa, aaaaa)
	addLink(t, ds, aaa, aaaa)
	addLink(t, ds, aa, aaa)
	addLink(t, ds, a, aa)
	return a
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:12,代码来源:traverse_test.go

示例8: newBinaryTree

func newBinaryTree(t *testing.T, ds mdag.DAGService) node.Node {
	a := mdag.NodeWithData([]byte("/a"))
	aa := child(t, ds, a, "aa")
	ab := child(t, ds, a, "ab")
	addLink(t, ds, aa, child(t, ds, aa, "aaa"))
	addLink(t, ds, aa, child(t, ds, aa, "aab"))
	addLink(t, ds, ab, child(t, ds, ab, "aba"))
	addLink(t, ds, ab, child(t, ds, ab, "abb"))
	addLink(t, ds, a, aa)
	addLink(t, ds, a, ab)
	return a
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:12,代码来源:traverse_test.go

示例9: TestSet

func TestSet(t *testing.T) {
	ds := mdtest.Mock()
	limit := 10000 // 10000 reproduces the pinloss issue fairly reliably

	if os.Getenv("STRESS_IT_OUT_YO") != "" {
		limit = 10000000
	}
	var inputs []*cid.Cid
	for i := 0; i < limit; i++ {
		c, err := ds.Add(dag.NodeWithData([]byte(fmt.Sprint(i))))
		if err != nil {
			t.Fatal(err)
		}

		inputs = append(inputs, c)
	}

	out, err := storeSet(context.Background(), ds, inputs, ignoreCids)
	if err != nil {
		t.Fatal(err)
	}

	// weird wrapper node because loadSet expects us to pass an
	// object pointing to multiple named sets
	setroot := &dag.ProtoNode{}
	err = setroot.AddNodeLinkClean("foo", out)
	if err != nil {
		t.Fatal(err)
	}

	outset, err := loadSet(context.Background(), ds, setroot, "foo", ignoreCids)
	if err != nil {
		t.Fatal(err)
	}

	if len(outset) != limit {
		t.Fatal("got wrong number", len(outset), limit)
	}

	seen := cid.NewSet()
	for _, c := range outset {
		seen.Add(c)
	}

	for _, c := range inputs {
		if !seen.Has(c) {
			t.Fatalf("expected to have %s, didnt find it")
		}
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:50,代码来源:set_test.go

示例10: getFileHandle

func getFileHandle(r *mfs.Root, path string, create bool) (*mfs.File, error) {

	target, err := mfs.Lookup(r, path)
	switch err {
	case nil:
		fi, ok := target.(*mfs.File)
		if !ok {
			return nil, fmt.Errorf("%s was not a file", path)
		}
		return fi, nil

	case os.ErrNotExist:
		if !create {
			return nil, err
		}

		// if create is specified and the file doesnt exist, we create the file
		dirname, fname := gopath.Split(path)
		pdiri, err := mfs.Lookup(r, dirname)
		if err != nil {
			log.Error("lookupfail ", dirname)
			return nil, err
		}
		pdir, ok := pdiri.(*mfs.Directory)
		if !ok {
			return nil, fmt.Errorf("%s was not a directory", dirname)
		}

		nd := dag.NodeWithData(ft.FilePBData(nil, 0))
		err = pdir.AddChild(fname, nd)
		if err != nil {
			return nil, err
		}

		fsn, err := pdir.Child(fname)
		if err != nil {
			return nil, err
		}

		fi, ok := fsn.(*mfs.File)
		if !ok {
			return nil, errors.New("Expected *mfs.File, didnt get it. This is likely a race condition.")
		}
		return fi, nil

	default:
		return nil, err
	}
}
开发者ID:yuanwr,项目名称:go-ipfs,代码行数:49,代码来源:files.go

示例11: addFile

func (adder *Adder) addFile(file files.File) error {
	err := adder.maybePauseForGC()
	if err != nil {
		return err
	}

	if file.IsDirectory() {
		return adder.addDir(file)
	}

	// case for symlink
	if s, ok := file.(*files.Symlink); ok {
		sdata, err := unixfs.SymlinkData(s.Target)
		if err != nil {
			return err
		}

		dagnode := dag.NodeWithData(sdata)
		_, err = adder.dagService.Add(dagnode)
		if err != nil {
			return err
		}

		return adder.addNode(dagnode, s.FileName())
	}

	// case for regular file
	// if the progress flag was specified, wrap the file so that we can send
	// progress updates to the client (over the output channel)
	var reader io.Reader = file
	if adder.Progress {
		rdr := &progressReader{file: file, out: adder.Out}
		if fi, ok := file.(files.FileInfo); ok {
			reader = &progressReader2{rdr, fi}
		} else {
			reader = rdr
		}
	}

	dagnode, err := adder.add(reader)
	if err != nil {
		return err
	}

	// patch it into the root
	return adder.addNode(dagnode, file.FileName())
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:47,代码来源:add.go

示例12: TestTypeFailures

func TestTypeFailures(t *testing.T) {
	dserv := testu.GetDAGServ()
	ctx, closer := context.WithCancel(context.Background())
	defer closer()

	node := unixfs.EmptyDirNode()
	if _, err := NewDagReader(ctx, node, dserv); err != ErrIsDir {
		t.Fatalf("excepted to get %v, got %v", ErrIsDir, err)
	}

	data, err := unixfs.SymlinkData("/somelink")
	if err != nil {
		t.Fatal(err)
	}
	node = mdag.NodeWithData(data)

	if _, err := NewDagReader(ctx, node, dserv); err != ErrCantReadSymlinks {
		t.Fatalf("excepted to get %v, got %v", ErrCantReadSymlinks, err)
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:20,代码来源:dagreader_test.go

示例13: Create

func (dir *Directory) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
	// New 'empty' file
	nd := dag.NodeWithData(ft.FilePBData(nil, 0))
	err := dir.dir.AddChild(req.Name, nd)
	if err != nil {
		return nil, nil, err
	}

	child, err := dir.dir.Child(req.Name)
	if err != nil {
		return nil, nil, err
	}

	fi, ok := child.(*mfs.File)
	if !ok {
		return nil, nil, errors.New("child creation failed")
	}

	nodechild := &FileNode{fi: fi}

	var openflag int
	switch {
	case req.Flags.IsReadOnly():
		openflag = mfs.OpenReadOnly
	case req.Flags.IsWriteOnly():
		openflag = mfs.OpenWriteOnly
	case req.Flags.IsReadWrite():
		openflag = mfs.OpenReadWrite
	default:
		return nil, nil, errors.New("unsupported open mode")
	}

	fd, err := fi.Open(openflag, true)
	if err != nil {
		return nil, nil, err
	}

	return nodechild, &File{fi: fd}, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:39,代码来源:ipns_unix.go

示例14: TestMetadataNode

func TestMetadataNode(t *testing.T) {
	dserv := testu.GetDAGServ()
	rdata, rnode := testu.GetRandomNode(t, dserv, 512)
	_, err := dserv.Add(rnode)
	if err != nil {
		t.Fatal(err)
	}

	ctx, closer := context.WithCancel(context.Background())
	defer closer()

	data, err := unixfs.BytesForMetadata(&unixfs.Metadata{"text", 125})
	if err != nil {
		t.Fatal(err)
	}
	node := mdag.NodeWithData(data)

	_, err = NewDagReader(ctx, node, dserv)
	if err == nil {
		t.Fatal("expected an error")
	}
	if !strings.Contains(err.Error(), "incorrectly formatted") {
		t.Fatal("expected different error")
	}

	node.AddNodeLink("", rnode)

	reader, err := NewDagReader(ctx, node, dserv)
	if err != nil {
		t.Fatal(err)
	}
	readdata, err := ioutil.ReadAll(reader)
	if err != nil {
		t.Fatal(err)
	}
	if err := testu.ArrComp(rdata, readdata); err != nil {
		t.Fatal(err)
	}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:39,代码来源:dagreader_test.go

示例15: PatchLink

//TODO: hrm, maybe this interface could be better
func (s *Shell) PatchLink(root, npath, childhash string, create bool) (string, error) {
	p, err := path.ParsePath(root)
	if err != nil {
		return "", err
	}

	rootnd, err := core.Resolve(s.ctx, s.node, p)
	if err != nil {
		return "", err
	}

	childpath, err := path.ParsePath(childhash)
	if err != nil {
		return "", err
	}

	nnode, err := core.Resolve(s.ctx, s.node, childpath)
	if err != nil {
		return "", err
	}

	e := dagutils.NewDagEditor(rootnd, s.node.DAG)
	err = e.InsertNodeAtPath(s.ctx, npath, nnode, func() *dag.Node {
		return dag.NodeWithData(ft.FolderPBData())
	})
	if err != nil {
		return "", err
	}

	_, err = e.Finalize(s.node.DAG)
	if err != nil {
		return "", err
	}

	return e.GetNode().Key().B58String(), nil
}
开发者ID:noffle,项目名称:ipfs-embedded-shell,代码行数:37,代码来源:object.go


注:本文中的github.com/ipfs/go-ipfs/merkledag.NodeWithData函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。