raw.go 2.65 KB
Newer Older
1 2 3
package merkledag

import (
4
	"encoding/json"
5
	"fmt"
Jeromy's avatar
Jeromy committed
6
	"github.com/ipfs/go-block-format"
7

Jeromy's avatar
Jeromy committed
8 9 10
	cid "github.com/ipfs/go-cid"
	u "github.com/ipfs/go-ipfs-util"
	ipld "github.com/ipfs/go-ipld-format"
11 12
)

13
// RawNode represents a node which only contains data.
14 15 16 17
type RawNode struct {
	blocks.Block
}

Łukasz Magiera's avatar
Łukasz Magiera committed
18
// NewRawNode creates a RawNode using the default sha2-256 hash function.
19 20 21 22 23 24 25 26
func NewRawNode(data []byte) *RawNode {
	h := u.Hash(data)
	c := cid.NewCidV1(cid.Raw, h)
	blk, _ := blocks.NewBlockWithCid(data, c)

	return &RawNode{blk}
}

27
// DecodeRawBlock is a block decoder for raw IPLD nodes conforming to `node.DecodeBlockFunc`.
28
func DecodeRawBlock(block blocks.Block) (ipld.Node, error) {
29 30 31 32 33 34 35
	if block.Cid().Type() != cid.Raw {
		return nil, fmt.Errorf("raw nodes cannot be decoded from non-raw blocks: %d", block.Cid().Type())
	}
	// Once you "share" a block, it should be immutable. Therefore, we can just use this block as-is.
	return &RawNode{block}, nil
}

36
var _ ipld.DecodeBlockFunc = DecodeRawBlock
37

38 39 40 41
// NewRawNodeWPrefix creates a RawNode using the provided cid builder
func NewRawNodeWPrefix(data []byte, builder cid.Builder) (*RawNode, error) {
	builder = builder.WithCodec(cid.Raw)
	c, err := builder.Sum(data)
42 43 44 45 46 47 48 49 50 51
	if err != nil {
		return nil, err
	}
	blk, err := blocks.NewBlockWithCid(data, c)
	if err != nil {
		return nil, err
	}
	return &RawNode{blk}, nil
}

52
// Links returns nil.
53
func (rn *RawNode) Links() []*ipld.Link {
54 55 56
	return nil
}

57
// ResolveLink returns an error.
58
func (rn *RawNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
59 60 61
	return nil, nil, ErrLinkNotFound
}

62
// Resolve returns an error.
63
func (rn *RawNode) Resolve(path []string) (interface{}, []string, error) {
64 65 66
	return nil, nil, ErrLinkNotFound
}

67
// Tree returns nil.
68
func (rn *RawNode) Tree(p string, depth int) []string {
69 70 71
	return nil
}

72
// Copy performs a deep copy of this node and returns it as an ipld.Node
73
func (rn *RawNode) Copy() ipld.Node {
74 75 76 77 78 79 80 81 82 83 84
	copybuf := make([]byte, len(rn.RawData()))
	copy(copybuf, rn.RawData())
	nblk, err := blocks.NewBlockWithCid(rn.RawData(), rn.Cid())
	if err != nil {
		// programmer error
		panic("failure attempting to clone raw block: " + err.Error())
	}

	return &RawNode{nblk}
}

85
// Size returns the size of this node
86 87 88 89
func (rn *RawNode) Size() (uint64, error) {
	return uint64(len(rn.RawData())), nil
}

90
// Stat returns some Stats about this node.
91 92
func (rn *RawNode) Stat() (*ipld.NodeStat, error) {
	return &ipld.NodeStat{
93 94 95 96 97
		CumulativeSize: len(rn.RawData()),
		DataSize:       len(rn.RawData()),
	}, nil
}

98 99 100 101 102
// MarshalJSON is required for our "ipfs dag" commands.
func (rn *RawNode) MarshalJSON() ([]byte, error) {
	return json.Marshal(string(rn.RawData()))
}

103
var _ ipld.Node = (*RawNode)(nil)