本文整理汇总了Golang中github.com/ipfs/go-ipfs/merkledag.ProtoNode类的典型用法代码示例。如果您正苦于以下问题:Golang ProtoNode类的具体用法?Golang ProtoNode怎么用?Golang ProtoNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ProtoNode类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: walkItems
func walkItems(ctx context.Context, dag merkledag.DAGService, n *merkledag.ProtoNode, fn walkerFunc, children keyObserver) error {
hdr, err := readHdr(n)
if err != nil {
return err
}
// readHdr guarantees fanout is a safe value
fanout := hdr.GetFanout()
for i, l := range n.Links()[fanout:] {
if err := fn(i, l); err != nil {
return err
}
}
for _, l := range n.Links()[:fanout] {
c := l.Cid
children(c)
if c.Equals(emptyKey) {
continue
}
subtree, err := l.GetNode(ctx, dag)
if err != nil {
return err
}
stpb, ok := subtree.(*merkledag.ProtoNode)
if !ok {
return merkledag.ErrNotProtobuf
}
if err := walkItems(ctx, dag, stpb, fn, children); err != nil {
return err
}
}
return nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:34,代码来源:set.go
示例2: randNode
func randNode() (*merkledag.ProtoNode, key.Key) {
node := new(merkledag.ProtoNode)
node.SetData(make([]byte, 32))
util.NewTimeSeededRand().Read(node.Data())
k := node.Key()
return node, k
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:7,代码来源:resolver_test.go
示例3: loadSet
func loadSet(ctx context.Context, dag merkledag.DAGService, root *merkledag.ProtoNode, name string, internalKeys keyObserver) ([]*cid.Cid, error) {
l, err := root.GetNodeLink(name)
if err != nil {
return nil, err
}
lnkc := l.Cid
internalKeys(lnkc)
n, err := l.GetNode(ctx, dag)
if err != nil {
return nil, err
}
pbn, ok := n.(*merkledag.ProtoNode)
if !ok {
return nil, merkledag.ErrNotProtobuf
}
var res []*cid.Cid
walk := func(idx int, link *node.Link) error {
res = append(res, link.Cid)
return nil
}
if err := walkItems(ctx, dag, pbn, walk, internalKeys); err != nil {
return nil, err
}
return res, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:30,代码来源:set.go
示例4: Mkdir
func (d *Directory) Mkdir(name string) (*Directory, error) {
d.lock.Lock()
defer d.lock.Unlock()
fsn, err := d.childUnsync(name)
if err == nil {
switch fsn := fsn.(type) {
case *Directory:
return fsn, os.ErrExist
case *File:
return nil, os.ErrExist
default:
return nil, fmt.Errorf("unrecognized type: %#v", fsn)
}
}
ndir := new(dag.ProtoNode)
ndir.SetData(ft.FolderPBData())
_, err = d.dserv.Add(ndir)
if err != nil {
return nil, err
}
err = d.node.AddNodeLinkClean(name, ndir)
if err != nil {
return nil, err
}
dirobj := NewDirectory(d.ctx, name, ndir, d, d.dserv)
d.childDirs[name] = dirobj
return dirobj, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:33,代码来源:dir.go
示例5: NewRoot
// newRoot creates a new Root and starts up a republisher routine for it
func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf PubFunc) (*Root, error) {
var repub *Republisher
if pf != nil {
repub = NewRepublisher(parent, pf, time.Millisecond*300, time.Second*3)
repub.setVal(node.Cid())
go repub.Run()
}
root := &Root{
node: node,
repub: repub,
dserv: ds,
}
pbn, err := ft.FromBytes(node.Data())
if err != nil {
log.Error("IPNS pointer was not unixfs node")
return nil, err
}
switch pbn.GetType() {
case ft.TDirectory:
root.val = NewDirectory(parent, node.String(), node, root, ds)
case ft.TFile, ft.TMetadata, ft.TRaw:
fi, err := NewFile(node.String(), node, root, ds)
if err != nil {
return nil, err
}
root.val = fi
default:
panic("unrecognized! (NYI)")
}
return root, nil
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:36,代码来源:system.go
示例6: addLink
func addLink(ctx context.Context, ds dag.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
if childname == "" {
return nil, errors.New("cannot create link with no name!")
}
// ensure that the node we are adding is in the dagservice
_, err := ds.Add(childnd)
if err != nil {
return nil, err
}
_ = ds.Remove(root)
// ensure no link with that name already exists
_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound
if err := root.AddNodeLinkClean(childname, childnd); err != nil {
return nil, err
}
if _, err := ds.Add(root); err != nil {
return nil, err
}
return root, nil
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:25,代码来源:utils.go
示例7: copyDag
func copyDag(nd *dag.ProtoNode, from, to dag.DAGService) error {
_, err := to.Add(nd)
if err != nil {
return err
}
for _, lnk := range nd.Links() {
child, err := lnk.GetNode(context.Background(), from)
if err != nil {
if err == dag.ErrNotFound {
// not found means we didnt modify it, and it should
// already be in the target datastore
continue
}
return err
}
childpb, ok := child.(*dag.ProtoNode)
if !ok {
return dag.ErrNotProtobuf
}
err = copyDag(childpb, from, to)
if err != nil {
return err
}
}
return nil
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:29,代码来源:utils.go
示例8: AddMetadataTo
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
c, err := cid.Decode(skey)
if err != nil {
return "", err
}
nd, err := n.DAG.Get(n.Context(), c)
if err != nil {
return "", err
}
mdnode := new(dag.ProtoNode)
mdata, err := ft.BytesForMetadata(m)
if err != nil {
return "", err
}
mdnode.SetData(mdata)
if err := mdnode.AddNodeLinkClean("file", nd); err != nil {
return "", err
}
nk, err := n.DAG.Add(mdnode)
if err != nil {
return "", err
}
return nk.String(), nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:29,代码来源:metadata.go
示例9: ExportTar
func ExportTar(ctx context.Context, root *dag.ProtoNode, ds dag.DAGService) (io.Reader, error) {
if string(root.Data()) != "ipfs/tar" {
return nil, errors.New("not an IPFS tarchive")
}
return &tarReader{
links: root.Links(),
ds: ds,
ctx: ctx,
}, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:10,代码来源:format.go
示例10: deserializeNode
// converts the Node object into a real dag.ProtoNode
func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
dagnode := new(dag.ProtoNode)
switch dataFieldEncoding {
case "text":
dagnode.SetData([]byte(nd.Data))
case "base64":
data, _ := base64.StdEncoding.DecodeString(nd.Data)
dagnode.SetData(data)
default:
return nil, fmt.Errorf("Unkown data field encoding")
}
dagnode.SetLinks(make([]*node.Link, len(nd.Links)))
for i, link := range nd.Links {
c, err := cid.Decode(link.Hash)
if err != nil {
return nil, err
}
dagnode.Links()[i] = &node.Link{
Name: link.Name,
Size: link.Size,
Cid: c,
}
}
return dagnode, nil
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:28,代码来源:object.go
示例11: NewUnixfsNodeFromDag
// NewUnixfsNodeFromDag reconstructs a Unixfs node from a given dag node
func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) {
mb, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return nil, err
}
return &UnixfsNode{
node: nd,
ufmt: mb,
}, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:12,代码来源:helpers.go
示例12: PrintDag
func PrintDag(nd *mdag.ProtoNode, ds mdag.DAGService, indent int) {
pbd, err := ft.FromBytes(nd.Data())
if err != nil {
panic(err)
}
for i := 0; i < indent; i++ {
fmt.Print(" ")
}
fmt.Printf("{size = %d, type = %s, children = %d", pbd.GetFilesize(), pbd.GetType().String(), len(pbd.GetBlocksizes()))
if len(nd.Links()) > 0 {
fmt.Println()
}
for _, lnk := range nd.Links() {
child, err := lnk.GetNode(context.Background(), ds)
if err != nil {
panic(err)
}
PrintDag(child.(*mdag.ProtoNode), ds, indent+1)
}
if len(nd.Links()) > 0 {
for i := 0; i < indent; i++ {
fmt.Print(" ")
}
}
fmt.Println("}")
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:27,代码来源:utils.go
示例13: rmLink
func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string) (*dag.ProtoNode, error) {
if len(path) == 1 {
// base case, remove node in question
err := root.RemoveNodeLink(path[0])
if err != nil {
return nil, err
}
_, err = e.tmp.Add(root)
if err != nil {
return nil, err
}
return root, nil
}
// search for node in both tmp dagstore and source dagstore
nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
if err == dag.ErrNotFound {
nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
}
if err != nil {
return nil, err
}
nnode, err := e.rmLink(ctx, nd, path[1:])
if err != nil {
return nil, err
}
_ = e.tmp.Remove(root)
_ = root.RemoveNodeLink(path[0])
err = root.AddNodeLinkClean(path[0], nnode)
if err != nil {
return nil, err
}
_, err = e.tmp.Add(root)
if err != nil {
return nil, err
}
return root, nil
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:46,代码来源:utils.go
示例14: randNode
func randNode() (*mdag.ProtoNode, *cid.Cid) {
nd := new(mdag.ProtoNode)
nd.SetData(make([]byte, 32))
util.NewTimeSeededRand().Read(nd.Data())
k := nd.Cid()
return nd, k
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:7,代码来源:pin_test.go
示例15: writeHdr
func writeHdr(n *merkledag.ProtoNode, hdr *pb.Set) error {
hdrData, err := proto.Marshal(hdr)
if err != nil {
return err
}
// make enough space for the length prefix and the marshalled header data
data := make([]byte, binary.MaxVarintLen64, binary.MaxVarintLen64+len(hdrData))
// write the uvarint length of the header data
uvarlen := binary.PutUvarint(data, uint64(len(hdrData)))
// append the actual protobuf data *after* the length value we wrote
data = append(data[:uvarlen], hdrData...)
n.SetData(data)
return nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:18,代码来源:set.go
示例16: ImportTar
func ImportTar(r io.Reader, ds dag.DAGService) (*dag.ProtoNode, error) {
tr := tar.NewReader(r)
root := new(dag.ProtoNode)
root.SetData([]byte("ipfs/tar"))
e := dagutil.NewDagEditor(root, ds)
for {
h, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
header := new(dag.ProtoNode)
headerBytes, err := marshalHeader(h)
if err != nil {
return nil, err
}
header.SetData(headerBytes)
if h.Size > 0 {
spl := chunk.NewRabin(tr, uint64(chunk.DefaultBlockSize))
nd, err := importer.BuildDagFromReader(ds, spl)
if err != nil {
return nil, err
}
err = header.AddNodeLinkClean("data", nd)
if err != nil {
return nil, err
}
}
_, err = ds.Add(header)
if err != nil {
return nil, err
}
path := escapePath(h.Name)
err = e.InsertNodeAtPath(context.Background(), path, header, func() *dag.ProtoNode { return new(dag.ProtoNode) })
if err != nil {
return nil, err
}
}
return e.Finalize(ds)
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:53,代码来源:format.go
示例17: TestAppendSingleBytesToEmpty
func TestAppendSingleBytesToEmpty(t *testing.T) {
ds := mdtest.Mock()
data := []byte("AB")
nd := new(merkledag.ProtoNode)
nd.SetData(ft.FilePBData(nil, 0))
dbp := &h.DagBuilderParams{
Dagserv: ds,
Maxlinks: 4,
}
spl := chunk.SizeSplitterGen(500)
ctx := context.Background()
nnode, err := TrickleAppend(ctx, nd, dbp.New(spl(bytes.NewReader(data[:1]))))
if err != nil {
t.Fatal(err)
}
nnode, err = TrickleAppend(ctx, nnode, dbp.New(spl(bytes.NewReader(data[1:]))))
if err != nil {
t.Fatal(err)
}
fread, err := uio.NewDagReader(ctx, nnode, ds)
if err != nil {
t.Fatal(err)
}
out, err := ioutil.ReadAll(fread)
if err != nil {
t.Fatal(err)
}
fmt.Println(out, data)
err = arrComp(out, data)
if err != nil {
t.Fatal(err)
}
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:42,代码来源:trickle_test.go
示例18: WriteNode
func (w *Writer) WriteNode(nd *mdag.ProtoNode, fpath string) error {
pb := new(upb.Data)
if err := proto.Unmarshal(nd.Data(), pb); err != nil {
return err
}
switch pb.GetType() {
case upb.Data_Metadata:
fallthrough
case upb.Data_Directory:
return w.writeDir(nd, fpath)
case upb.Data_Raw:
fallthrough
case upb.Data_File:
return w.writeFile(nd, pb, fpath)
case upb.Data_Symlink:
return writeSymlinkHeader(w.TarW, string(pb.GetData()), fpath)
default:
return ft.ErrUnrecognizedType
}
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:21,代码来源:writer.go
示例19: getPaths
func getPaths(t *testing.T, ipfs *core.IpfsNode, name string, n *dag.ProtoNode) []string {
if len(n.Links()) == 0 {
return []string{name}
}
var out []string
for _, lnk := range n.Links() {
child, err := lnk.GetNode(ipfs.Context(), ipfs.DAG)
if err != nil {
t.Fatal(err)
}
childpb, ok := child.(*dag.ProtoNode)
if !ok {
t.Fatal(dag.ErrNotProtobuf)
}
sub := getPaths(t, ipfs, path.Join(name, lnk.Name), childpb)
out = append(out, sub...)
}
return out
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:21,代码来源:ipfs_test.go
示例20: readHdr
func readHdr(n *merkledag.ProtoNode) (*pb.Set, error) {
hdrLenRaw, consumed := binary.Uvarint(n.Data())
if consumed <= 0 {
return nil, errors.New("invalid Set header length")
}
pbdata := n.Data()[consumed:]
if hdrLenRaw > uint64(len(pbdata)) {
return nil, errors.New("impossibly large Set header length")
}
// as hdrLenRaw was <= an int, we now know it fits in an int
hdrLen := int(hdrLenRaw)
var hdr pb.Set
if err := proto.Unmarshal(pbdata[:hdrLen], &hdr); err != nil {
return nil, err
}
if v := hdr.GetVersion(); v != 1 {
return nil, fmt.Errorf("unsupported Set version: %d", v)
}
if uint64(hdr.GetFanout()) > uint64(len(n.Links())) {
return nil, errors.New("impossibly large Fanout")
}
return &hdr, nil
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:25,代码来源:set.go
注:本文中的github.com/ipfs/go-ipfs/merkledag.ProtoNode类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论