Skip to content

Commit

Permalink
Change from using hardcoded Prefixes to the more flex able cid.Format…
Browse files Browse the repository at this point in the history
… type.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jul 25, 2018
1 parent f420578 commit a03dd74
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 51 deletions.
2 changes: 1 addition & 1 deletion core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ You can now check what blocks have been created by:
md := dagtest.Mock()
emptyDirNode := ft.EmptyDirNode()
// Use the same prefix for the "empty" MFS root as for the file adder.
emptyDirNode.Prefix = *fileAdder.Prefix
emptyDirNode.SetPrefix(fileAdder.Prefix)
mr, err := mfs.NewRoot(req.Context, md, emptyDirNode, nil)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand Down
8 changes: 4 additions & 4 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ Change the cid version or hash function of the root node of a given path.
},
}

func updatePath(rt *mfs.Root, pth string, prefix *cid.Prefix, flush bool) error {
func updatePath(rt *mfs.Root, pth string, prefix cid.Format, flush bool) error {
if prefix == nil {
return nil
}
Expand Down Expand Up @@ -1088,7 +1088,7 @@ Remove files or directories.
},
}

func getPrefixNew(req *cmds.Request) (*cid.Prefix, error) {
func getPrefixNew(req *cmds.Request) (cid.Format, error) {
cidVer, cidVerSet := req.Options["cid-version"].(int)
hashFunStr, hashFunSet := req.Options["hash"].(string)

Expand Down Expand Up @@ -1117,7 +1117,7 @@ func getPrefixNew(req *cmds.Request) (*cid.Prefix, error) {
return &prefix, nil
}

func getPrefix(req oldcmds.Request) (*cid.Prefix, error) {
func getPrefix(req oldcmds.Request) (cid.Format, error) {
cidVer, cidVerSet, _ := req.Option("cid-version").Int()
hashFunStr, hashFunSet, _ := req.Option("hash").String()

Expand Down Expand Up @@ -1146,7 +1146,7 @@ func getPrefix(req oldcmds.Request) (*cid.Prefix, error) {
return &prefix, nil
}

func getFileHandle(r *mfs.Root, path string, create bool, prefix *cid.Prefix) (*mfs.File, error) {
func getFileHandle(r *mfs.Root, path string, create bool, prefix cid.Format) (*mfs.File, error) {
target, err := mfs.Lookup(r, path)
switch err {
case nil:
Expand Down
2 changes: 1 addition & 1 deletion core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Adder struct {
mroot *mfs.Root
unlocker bstore.Unlocker
tempRoot *cid.Cid
Prefix *cid.Prefix
Prefix cid.Format
liveNodes uint64
}

Expand Down
10 changes: 5 additions & 5 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DagBuilderHelper struct {
nextData []byte // the next item to return.
maxlinks int
batch *ipld.Batch
prefix *cid.Prefix
prefix cid.Format

// Filestore support variables.
// ----------------------------
Expand Down Expand Up @@ -54,7 +54,7 @@ type DagBuilderParams struct {
RawLeaves bool

// CID Prefix to use if set
Prefix *cid.Prefix
Prefix cid.Format

// DAGService to write blocks to (required)
Dagserv ipld.DAGService
Expand Down Expand Up @@ -146,7 +146,7 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
}

// GetPrefix returns the internal `cid.Prefix` set in the builder.
func (db *DagBuilderHelper) GetPrefix() *cid.Prefix {
func (db *DagBuilderHelper) GetPrefix() cid.Format {
return db.prefix
}

Expand All @@ -166,7 +166,7 @@ func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {
raw: true,
}, nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
rawnode, err := dag.NewRawNodeWOpts(data, db.prefix)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (db *DagBuilderHelper) NewLeafNode(data []byte) (ipld.Node, error) {
if db.prefix == nil {
return dag.NewRawNode(data), nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
rawnode, err := dag.NewRawNodeWOpts(data, db.prefix)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion importer/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) {
}

// SetPrefix sets the CID Prefix
func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) {
func (n *UnixfsNode) SetPrefix(prefix cid.Format) {
n.node.SetPrefix(prefix)
}

Expand Down
7 changes: 2 additions & 5 deletions merkledag/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
}

if n.cached == nil {
if n.Prefix.Codec == 0 { // unset
n.Prefix = v0CidPrefix
}
c, err := n.Prefix.Sum(n.encoded)
c, err := n.Prefix().Sum(n.encoded)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -129,7 +126,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
}

decnd.cached = c
decnd.Prefix = c.Prefix()
decnd.SetPrefix(c.Prefix())
return decnd, nil
}

Expand Down
25 changes: 14 additions & 11 deletions merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ProtoNode struct {
cached *cid.Cid

// Prefix specifies cid version and hashing function
Prefix cid.Prefix
prefix cid.Format
}

var v0CidPrefix = cid.Prefix{
Expand Down Expand Up @@ -63,14 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) {
}
}

// Prefix returns the CID Prefix for this ProtoNode, it is never nil
func (n *ProtoNode) Prefix() cid.Format {
if n.prefix == nil {
n.prefix = v0CidPrefix
}
return n.prefix
}

// SetPrefix sets the CID prefix if it is non nil, if prefix is nil then
// it resets it the default value
func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) {
func (n *ProtoNode) SetPrefix(prefix cid.Format) {
if prefix == nil {
n.Prefix = v0CidPrefix
n.prefix = v0CidPrefix
} else {
n.Prefix = *prefix
n.Prefix.Codec = cid.DagProtobuf
n.prefix = prefix.WithCodec(cid.DagProtobuf)
n.encoded = nil
n.cached = nil
}
Expand Down Expand Up @@ -191,7 +198,7 @@ func (n *ProtoNode) Copy() ipld.Node {
copy(nnode.links, n.links)
}

nnode.Prefix = n.Prefix
nnode.prefix = n.prefix

return nnode
}
Expand Down Expand Up @@ -301,11 +308,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
return n.cached
}

if n.Prefix.Codec == 0 {
n.SetPrefix(nil)
}

c, err := n.Prefix.Sum(n.RawData())
c, err := n.Prefix().Sum(n.RawData())
if err != nil {
// programmer error
err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
Expand Down
7 changes: 2 additions & 5 deletions merkledag/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ var _ ipld.DecodeBlockFunc = DecodeRawBlock

// NewRawNodeWPrefix creates a RawNode with the hash function
// specified in prefix.
func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) {
prefix.Codec = cid.Raw
if prefix.Version == 0 {
prefix.Version = 1
}
func NewRawNodeWOpts(data []byte, prefix cid.Format) (*RawNode, error) {
prefix = prefix.WithCodec(cid.Raw)
c, err := prefix.Sum(data)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child
}

// GetPrefix gets the CID prefix of the root node
func (d *Directory) GetPrefix() *cid.Prefix {
func (d *Directory) GetPrefix() cid.Format {
return d.unixfsDir.GetPrefix()
}

// SetPrefix sets the CID prefix
func (d *Directory) SetPrefix(prefix *cid.Prefix) {
func (d *Directory) SetPrefix(prefix cid.Format) {
d.unixfsDir.SetPrefix(prefix)
}

Expand Down
2 changes: 1 addition & 1 deletion mfs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd ipld.Node) error {
type MkdirOpts struct {
Mkparents bool
Flush bool
Prefix *cid.Prefix
Prefix cid.Format
}

// Mkdir creates a directory at 'path' under the directory 'd', creating
Expand Down
8 changes: 4 additions & 4 deletions unixfs/hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Shard struct {
tableSize int
tableSizeLg2 int

prefix *cid.Prefix
prefix cid.Format
hashFunc uint64

prefixPadStr string
Expand Down Expand Up @@ -124,18 +124,18 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
ds.children = make([]child, len(pbnd.Links()))
ds.bitfield.SetBytes(pbd.GetData())
ds.hashFunc = pbd.GetHashType()
ds.prefix = &ds.nd.Prefix
ds.prefix = ds.nd.Prefix()

return ds, nil
}

// SetPrefix sets the CID Prefix
func (ds *Shard) SetPrefix(prefix *cid.Prefix) {
func (ds *Shard) SetPrefix(prefix cid.Format) {
ds.prefix = prefix
}

// Prefix gets the CID Prefix, may be nil if unset
func (ds *Shard) Prefix() *cid.Prefix {
func (ds *Shard) Prefix() cid.Format {
return ds.prefix
}

Expand Down
16 changes: 8 additions & 8 deletions unixfs/io/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var DefaultShardWidth = 256
type Directory interface {

// SetPrefix sets the CID prefix of the root node.
SetPrefix(*cid.Prefix)
SetPrefix(cid.Format)

// AddChild adds a (name, key) pair to the root node.
AddChild(context.Context, string, ipld.Node) error
Expand All @@ -52,7 +52,7 @@ type Directory interface {
GetNode() (ipld.Node, error)

// GetPrefix returns the CID Prefix used.
GetPrefix() *cid.Prefix
GetPrefix() cid.Format
}

// TODO: Evaluate removing `dserv` from this layer and providing it in MFS.
Expand Down Expand Up @@ -128,7 +128,7 @@ func NewDirectoryFromNode(dserv ipld.DAGService, node ipld.Node) (Directory, err
}

// SetPrefix implements the `Directory` interface.
func (d *BasicDirectory) SetPrefix(prefix *cid.Prefix) {
func (d *BasicDirectory) SetPrefix(prefix cid.Format) {
d.node.SetPrefix(prefix)
}

Expand Down Expand Up @@ -180,8 +180,8 @@ func (d *BasicDirectory) GetNode() (ipld.Node, error) {
}

// GetPrefix implements the `Directory` interface.
func (d *BasicDirectory) GetPrefix() *cid.Prefix {
return &d.node.Prefix
func (d *BasicDirectory) GetPrefix() cid.Format {
return d.node.Prefix()
}

// SwitchToSharding returns a HAMT implementation of this directory.
Expand All @@ -193,7 +193,7 @@ func (d *BasicDirectory) SwitchToSharding(ctx context.Context) (Directory, error
if err != nil {
return nil, err
}
shard.SetPrefix(&d.node.Prefix)
shard.SetPrefix(d.node.Prefix())
hamtDir.shard = shard

for _, lnk := range d.node.Links() {
Expand All @@ -212,7 +212,7 @@ func (d *BasicDirectory) SwitchToSharding(ctx context.Context) (Directory, error
}

// SetPrefix implements the `Directory` interface.
func (d *HAMTDirectory) SetPrefix(prefix *cid.Prefix) {
func (d *HAMTDirectory) SetPrefix(prefix cid.Format) {
d.shard.SetPrefix(prefix)
}

Expand Down Expand Up @@ -252,6 +252,6 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
}

// GetPrefix implements the `Directory` interface.
func (d *HAMTDirectory) GetPrefix() *cid.Prefix {
func (d *HAMTDirectory) GetPrefix() cid.Format {
return d.shard.Prefix()
}
6 changes: 3 additions & 3 deletions unixfs/mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (*cid.Cid, error) {

nd := new(mdag.ProtoNode)
nd.SetData(b)
nd.SetPrefix(&nd0.Prefix)
nd.SetPrefix(nd0.Prefix())
err = dm.dagserv.Add(dm.ctx, nd)
if err != nil {
return nil, err
Expand All @@ -282,7 +282,7 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (*cid.Cid, error) {
copy(bytes[offsetPlusN:], origData[offsetPlusN:])
}

nd, err := mdag.NewRawNodeWPrefix(bytes, nd0.Cid().Prefix())
nd, err := mdag.NewRawNodeWOpts(bytes, nd0.Cid().Prefix())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -517,7 +517,7 @@ func dagTruncate(ctx context.Context, n ipld.Node, size uint64, ds ipld.DAGServi
nd.SetData(ft.WrapData(pbn.Data[:size]))
return nd, nil
case *mdag.RawNode:
return mdag.NewRawNodeWPrefix(nd.RawData()[:size], nd.Cid().Prefix())
return mdag.NewRawNodeWOpts(nd.RawData()[:size], nd.Cid().Prefix())
}
}

Expand Down

0 comments on commit a03dd74

Please sign in to comment.