Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Update to use new Builder interface for creating CIDs. #7

Merged
merged 3 commits into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions 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
builder cid.Builder
hashFunc uint64

prefixPadStr string
Expand Down Expand Up @@ -124,25 +124,25 @@ 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.builder = ds.nd.CidBuilder()

return ds, nil
}

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

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

// Node serializes the HAMT structure into a merkledag node with unixfs formatting
func (ds *Shard) Node() (ipld.Node, error) {
out := new(dag.ProtoNode)
out.SetPrefix(ds.prefix)
out.SetCidBuilder(ds.builder)

cindex := 0
// TODO: optimized 'for each set bit'
Expand Down Expand Up @@ -494,7 +494,7 @@ func (ds *Shard) modifyValue(ctx context.Context, hv *hashBits, key string, val
if err != nil {
return err
}
ns.prefix = ds.prefix
ns.builder = ds.builder
chhv := &hashBits{
b: hash([]byte(child.key)),
consumed: hv.consumed,
Expand Down
52 changes: 26 additions & 26 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
// DagBuilderHelper wraps together a bunch of objects needed to
// efficiently create unixfs dag trees
type DagBuilderHelper struct {
dserv ipld.DAGService
spl chunker.Splitter
recvdErr error
rawLeaves bool
nextData []byte // the next item to return.
maxlinks int
batch *ipld.Batch
prefix *cid.Prefix
dserv ipld.DAGService
spl chunker.Splitter
recvdErr error
rawLeaves bool
nextData []byte // the next item to return.
maxlinks int
batch *ipld.Batch
cidBuilder cid.Builder

// Filestore support variables.
// ----------------------------
Expand All @@ -53,8 +53,8 @@ type DagBuilderParams struct {
// instead of using the unixfs TRaw type
RawLeaves bool

// CID Prefix to use if set
Prefix *cid.Prefix
// CID Builder to use if set
CidBuilder cid.Builder

// DAGService to write blocks to (required)
Dagserv ipld.DAGService
Expand All @@ -73,12 +73,12 @@ type DagBuilderParams struct {
// chunker.Splitter as data source.
func (dbp *DagBuilderParams) New(spl chunker.Splitter) *DagBuilderHelper {
db := &DagBuilderHelper{
dserv: dbp.Dagserv,
spl: spl,
rawLeaves: dbp.RawLeaves,
prefix: dbp.Prefix,
maxlinks: dbp.Maxlinks,
batch: ipld.NewBatch(context.TODO(), dbp.Dagserv),
dserv: dbp.Dagserv,
spl: spl,
rawLeaves: dbp.RawLeaves,
cidBuilder: dbp.CidBuilder,
maxlinks: dbp.Maxlinks,
batch: ipld.NewBatch(context.TODO(), dbp.Dagserv),
}
if fi, ok := spl.Reader().(files.FileInfo); dbp.NoCopy && ok {
db.fullPath = fi.AbsPath()
Expand Down Expand Up @@ -141,13 +141,13 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
node: new(dag.ProtoNode),
ufmt: ft.NewFSNode(ft.TFile),
}
n.SetPrefix(db.prefix)
n.SetCidBuilder(db.cidBuilder)
return n
}

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

// NewLeaf creates a leaf node filled with data. If rawLeaves is
Expand All @@ -160,13 +160,13 @@ func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {
}

if db.rawLeaves {
if db.prefix == nil {
if db.cidBuilder == nil {
return &UnixfsNode{
rawnode: dag.NewRawNode(data),
raw: true,
}, nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
rawnode, err := dag.NewRawNodeWPrefix(data, db.cidBuilder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,10 +194,10 @@ func (db *DagBuilderHelper) NewLeafNode(data []byte) (ipld.Node, error) {

if db.rawLeaves {
// Encapsulate the data in a raw node.
if db.prefix == nil {
if db.cidBuilder == nil {
return dag.NewRawNode(data), nil
}
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
rawnode, err := dag.NewRawNodeWPrefix(data, db.cidBuilder)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func (db *DagBuilderHelper) newUnixfsBlock() *UnixfsNode {
node: new(dag.ProtoNode),
ufmt: ft.NewFSNode(ft.TRaw),
}
n.SetPrefix(db.prefix)
n.SetCidBuilder(db.cidBuilder)
return n
}

Expand Down Expand Up @@ -401,7 +401,7 @@ type FSNodeOverDag struct {
func (db *DagBuilderHelper) NewFSNodeOverDag(fsNodeType pb.Data_DataType) *FSNodeOverDag {
node := new(FSNodeOverDag)
node.dag = new(dag.ProtoNode)
node.dag.SetPrefix(db.GetPrefix())
node.dag.SetCidBuilder(db.GetCidBuilder())

node.file = ft.NewFSNode(fsNodeType)

Expand Down
6 changes: 3 additions & 3 deletions importer/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) {
}, nil
}

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

// NumChildren returns the number of children referenced by this UnixfsNode.
Expand Down
2 changes: 1 addition & 1 deletion importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getTrickleDag(t testing.TB, size int64, blksize int64) (ipld.Node, ipld.DAG

func TestStableCid(t *testing.T) {
ds := mdtest.Mock()
buf := make([]byte, 10 * 1024 * 1024)
buf := make([]byte, 10*1024*1024)
u.NewSeededRand(0xdeadbeef).Read(buf)
r := bytes.NewReader(buf)

Expand Down
34 changes: 17 additions & 17 deletions io/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var DefaultShardWidth = 256
// (which is the main consumer of this interface).
type Directory interface {

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

// AddChild adds a (name, key) pair to the root node.
AddChild(context.Context, string, ipld.Node) error
Expand All @@ -51,8 +51,8 @@ type Directory interface {
// GetNode returns the root of this directory.
GetNode() (ipld.Node, error)

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

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

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

// AddChild implements the `Directory` interface. It adds (or replaces)
Expand Down Expand Up @@ -179,9 +179,9 @@ func (d *BasicDirectory) GetNode() (ipld.Node, error) {
return d.node, nil
}

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

// 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.SetCidBuilder(d.node.CidBuilder())
hamtDir.shard = shard

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

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

// AddChild implements the `Directory` interface.
Expand Down Expand Up @@ -251,7 +251,7 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
return d.shard.Node()
}

// GetPrefix implements the `Directory` interface.
func (d *HAMTDirectory) GetPrefix() *cid.Prefix {
return d.shard.Prefix()
// GetCidBuilder implements the `Directory` interface.
func (d *HAMTDirectory) GetCidBuilder() cid.Builder {
return d.shard.CidBuilder()
}
10 changes: 5 additions & 5 deletions 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.SetCidBuilder(nd0.CidBuilder())
err = dm.dagserv.Add(dm.ctx, nd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -345,10 +345,10 @@ func (dm *DagModifier) appendData(nd ipld.Node, spl chunker.Splitter) (ipld.Node
switch nd := nd.(type) {
case *mdag.ProtoNode, *mdag.RawNode:
dbp := &help.DagBuilderParams{
Dagserv: dm.dagserv,
Maxlinks: help.DefaultLinksPerBlock,
Prefix: &dm.Prefix,
RawLeaves: dm.RawLeaves,
Dagserv: dm.dagserv,
Maxlinks: help.DefaultLinksPerBlock,
CidBuilder: dm.Prefix,
RawLeaves: dm.RawLeaves,
}
return trickle.Append(dm.ctx, nd, dbp.New(spl))
default:
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
},
{
"author": "why",
"hash": "QmXkZeJmx4c3ddjw81DQMUpM1e5LjAack5idzZYWUb2qAJ",
"hash": "QmXhrNaxjxNLwAHnWQScc6GxvpJMyn8wfdRmGDbUQwpfth",
"name": "go-merkledag",
"version": "1.0.6"
"version": "1.0.7"
},
{
"author": "hector",
"hash": "QmSHjPDw8yNgLZ7cBfX7w3Smn7PHwYhNEpd4LHQQxUg35L",
"hash": "QmWjvVuzD3Dqf3VznGAXUs1VqpG4dd35ounVCnLam8v7Xt",
"name": "go-ipfs-posinfo",
"version": "0.0.3"
"version": "0.0.4"
},
{
"author": "Stebalien",
Expand All @@ -56,9 +56,9 @@
},
{
"author": "whyrusleeping",
"hash": "QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP",
"hash": "Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y",
"name": "go-cid",
"version": "0.7.22"
"version": "0.7.24"
},
{
"author": "whyrusleeping",
Expand All @@ -68,9 +68,9 @@
},
{
"author": "whyrusleeping",
"hash": "QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ",
"hash": "QmUSyMZ8Vt4vTZr5HdDEgEfpwAXfQRuDdfCFTt7XBzhxpQ",
"name": "go-ipld-format",
"version": "0.5.5"
"version": "0.5.6"
}
],
"gxVersion": "0.12.1",
Expand Down
8 changes: 4 additions & 4 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func GetNode(t testing.TB, dserv ipld.DAGService, data []byte, opts NodeOpts) ip
in := bytes.NewReader(data)

dbp := h.DagBuilderParams{
Dagserv: dserv,
Maxlinks: h.DefaultLinksPerBlock,
Prefix: &opts.Prefix,
RawLeaves: opts.RawLeavesUsed,
Dagserv: dserv,
Maxlinks: h.DefaultLinksPerBlock,
CidBuilder: opts.Prefix,
RawLeaves: opts.RawLeavesUsed,
}

node, err := trickle.Layout(dbp.New(SizeSplitterGen(500)(in)))
Expand Down