Skip to content

Commit

Permalink
adder: allowing setting of the CID prefix instead of just the version
Browse files Browse the repository at this point in the history
eliminates need for CidVersion type

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Mar 19, 2017
1 parent 44b6c80 commit 4df10c0
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 74 deletions.
8 changes: 4 additions & 4 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ You can now refer to the added file in a gateway, like so:
rawblks, rbset, _ := req.Option(rawLeavesOptionName).Bool()
nocopy, _, _ := req.Option(noCopyOptionName).Bool()
fscache, _, _ := req.Option(fstoreCacheOptionName).Bool()
cidVerNum, _, _ := req.Option(cidVersionOptionName).Int()
cidVer, _, _ := req.Option(cidVersionOptionName).Int()

if nocopy && !cfg.Experimental.FilestoreEnabled {
res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"),
Expand All @@ -175,11 +175,11 @@ You can now refer to the added file in a gateway, like so:
return
}

if cidVerNum >= 1 && !rbset {
if cidVer >= 1 && !rbset {
rawblks = true
}

cidVer, err := dag.NewCidVersion(cidVerNum)
prefix, err := dag.PrefixForCidVersion(cidVer)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -232,7 +232,7 @@ You can now refer to the added file in a gateway, like so:
fileAdder.RawLeaves = rawblks
fileAdder.NoCopy = nocopy

fileAdder.SetCidVersion(cidVer)
fileAdder.SetPrefix(prefix)

if hash {
md := dagtest.Mock()
Expand Down
20 changes: 10 additions & 10 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ type Adder struct {
mr *mfs.Root
unlocker bs.Unlocker
tempRoot *cid.Cid
cidVersion dag.CidVersion
prefix cid.Prefix
}

func (adder *Adder) SetCidVersion(cidVer dag.CidVersion) {
adder.mr.SetCidVersion(cidVer)
adder.cidVersion = cidVer
func (adder *Adder) SetPrefix(prefix cid.Prefix) {
adder.mr.SetPrefix(prefix)
adder.prefix = prefix
}

func (adder *Adder) SetMfsRoot(r *mfs.Root) {
Expand All @@ -130,11 +130,11 @@ func (adder Adder) add(reader io.Reader) (node.Node, error) {
}

params := ihelper.DagBuilderParams{
Dagserv: adder.dagService,
RawLeaves: adder.RawLeaves,
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: adder.NoCopy,
CidVersion: adder.cidVersion,
Dagserv: adder.dagService,
RawLeaves: adder.RawLeaves,
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: adder.NoCopy,
Prefix: adder.prefix,
}

if adder.Trickle {
Expand Down Expand Up @@ -402,7 +402,7 @@ func (adder *Adder) addFile(file files.File) error {
}

dagnode := dag.NodeWithData(sdata)
dagnode.SetCidVersion(adder.cidVersion)
dagnode.SetPrefix(adder.prefix)
_, err = adder.dagService.Add(dagnode)
if err != nil {
return err
Expand Down
41 changes: 21 additions & 20 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import (
dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"

cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
)

// DagBuilderHelper wraps together a bunch of objects needed to
// efficiently create unixfs dag trees
type DagBuilderHelper struct {
dserv dag.DAGService
spl chunk.Splitter
recvdErr error
rawLeaves bool
nextData []byte // the next item to return.
maxlinks int
batch *dag.Batch
fullPath string
stat os.FileInfo
cidVersion dag.CidVersion
dserv dag.DAGService
spl chunk.Splitter
recvdErr error
rawLeaves bool
nextData []byte // the next item to return.
maxlinks int
batch *dag.Batch
fullPath string
stat os.FileInfo
prefix cid.Prefix
}

type DagBuilderParams struct {
Expand All @@ -35,8 +36,8 @@ type DagBuilderParams struct {
// instead of using the unixfs TRaw type
RawLeaves bool

// CID Version to use
CidVersion dag.CidVersion
// CID Prefix to use
Prefix cid.Prefix

// DAGService to write blocks to (required)
Dagserv dag.DAGService
Expand All @@ -50,12 +51,12 @@ type DagBuilderParams struct {
// from chunks object
func (dbp *DagBuilderParams) New(spl chunk.Splitter) *DagBuilderHelper {
db := &DagBuilderHelper{
dserv: dbp.Dagserv,
spl: spl,
rawLeaves: dbp.RawLeaves,
cidVersion: dbp.CidVersion,
maxlinks: dbp.Maxlinks,
batch: dbp.Dagserv.Batch(),
dserv: dbp.Dagserv,
spl: spl,
rawLeaves: dbp.RawLeaves,
prefix: dbp.Prefix,
maxlinks: dbp.Maxlinks,
batch: dbp.Dagserv.Batch(),
}
if fi, ok := spl.Reader().(files.FileInfo); dbp.NoCopy && ok {
db.fullPath = fi.AbsPath()
Expand Down Expand Up @@ -115,7 +116,7 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
node: new(dag.ProtoNode),
ufmt: &ft.FSNode{Type: ft.TFile},
}
n.SetCidVersion(db.cidVersion)
n.SetPrefix(db.prefix)
return n
}

Expand All @@ -125,7 +126,7 @@ func (db *DagBuilderHelper) NewUnixfsBlock() *UnixfsNode {
node: new(dag.ProtoNode),
ufmt: &ft.FSNode{Type: ft.TRaw},
}
n.SetCidVersion(db.cidVersion)
n.SetPrefix(db.prefix)
return n
}

Expand Down
5 changes: 3 additions & 2 deletions importer/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
pi "github.com/ipfs/go-ipfs/thirdparty/posinfo"
ft "github.com/ipfs/go-ipfs/unixfs"

cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
)

Expand Down Expand Up @@ -61,8 +62,8 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) {
}, nil
}

func (n *UnixfsNode) SetCidVersion(v dag.CidVersion) {
n.node.SetCidVersion(v)
func (n *UnixfsNode) SetPrefix(prefix cid.Prefix) {
n.node.SetPrefix(prefix)
}

func (n *UnixfsNode) NumChildren() int {
Expand Down
23 changes: 0 additions & 23 deletions merkledag/cidversion.go

This file was deleted.

16 changes: 10 additions & 6 deletions merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ var v1CidPrefix = cid.Prefix{
Version: 1,
}

func (n *ProtoNode) SetCidVersion(v CidVersion) {
switch v {
func PrefixForCidVersion(version int) (cid.Prefix, error) {
switch version {
case 0:
n.Prefix = v0CidPrefix
return v0CidPrefix, nil
case 1:
n.Prefix = v1CidPrefix
return v1CidPrefix, nil
default:
// should not happen
panic("unhanded prefix version")
return cid.Prefix{}, fmt.Errorf("unknown CID version: %d", version)
}
}

func (n *ProtoNode) SetPrefix(prefix cid.Prefix) {
n.Prefix = prefix
n.Prefix.Codec = cid.DagProtobuf
n.encoded = nil
n.cached = nil
}
Expand Down
5 changes: 3 additions & 2 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ft "github.com/ipfs/go-ipfs/unixfs"
ufspb "github.com/ipfs/go-ipfs/unixfs/pb"

cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
)

Expand Down Expand Up @@ -50,8 +51,8 @@ func NewDirectory(ctx context.Context, name string, node *dag.ProtoNode, parent
}
}

func (d *Directory) SetCidVersion(cidVer dag.CidVersion) {
d.node.SetCidVersion(cidVer)
func (d *Directory) SetPrefix(prefix cid.Prefix) {
d.node.SetPrefix(prefix)
}

// closeChild updates the child by the given name to the dag node 'nd'
Expand Down
4 changes: 2 additions & 2 deletions mfs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error {
if err != nil {
return err
}
mkd.SetCidVersion(r.CidVersion)
mkd.SetPrefix(r.Prefix)
fsn = mkd
} else if err != nil {
return err
Expand All @@ -153,7 +153,7 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error {
return err
}
}
final.SetCidVersion(r.CidVersion)
final.SetPrefix(r.Prefix)

if flush {
err := final.Flush()
Expand Down
10 changes: 5 additions & 5 deletions mfs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type Root struct {

Type string

// CID version to use for any children created
CidVersion dag.CidVersion
// Prefix to use for any children created
Prefix cid.Prefix
}

type PubFunc func(context.Context, *cid.Cid) error
Expand Down Expand Up @@ -107,9 +107,9 @@ func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf
// SetCidVersion sets the CidVersion of the root node, and any
// children created. To just set the CID version of future children,
// set CidVersion directly
func (kr *Root) SetCidVersion(cidVer dag.CidVersion) {
kr.node.SetCidVersion(cidVer)
kr.CidVersion = cidVer
func (kr *Root) SetPrefix(prefix cid.Prefix) {
kr.node.SetPrefix(prefix)
kr.Prefix = prefix
}

func (kr *Root) GetValue() FSNode {
Expand Down

0 comments on commit 4df10c0

Please sign in to comment.