Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite add command to use dagwriter #236

Merged
merged 2 commits into from
Oct 30, 2014
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
14 changes: 12 additions & 2 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (

"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/importer"
"github.com/jbenet/go-ipfs/importer/chunk"
dag "github.com/jbenet/go-ipfs/merkledag"
"github.com/jbenet/go-ipfs/pin"
ft "github.com/jbenet/go-ipfs/unixfs"
uio "github.com/jbenet/go-ipfs/unixfs/io"
)

// Error indicating the max depth has been exceded.
Expand Down Expand Up @@ -87,7 +90,14 @@ func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node
}

func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
root, err := importer.NewDagFromFile(fpath)
dw := uio.NewDagWriter(n.DAG, chunk.DefaultSplitter)
mp, ok := n.Pinning.(pin.ManualPinner)
if !ok {
return nil, errors.New("invalid pinner type! expected manual pinner")
}
dw.Pinner = mp

root, err := importer.ImportFileDag(fpath, dw)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +108,7 @@ func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Nod
log.Info("adding subblock: %s %s", l.Name, l.Hash.B58String())
}

return root, addNode(n, root, fpath, out)
return root, nil
}

// addNode adds the node to the graph + local storage
Expand Down
32 changes: 32 additions & 0 deletions importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jbenet/go-ipfs/importer/chunk"
dag "github.com/jbenet/go-ipfs/merkledag"
ft "github.com/jbenet/go-ipfs/unixfs"
uio "github.com/jbenet/go-ipfs/unixfs/io"
"github.com/jbenet/go-ipfs/util"
)

Expand Down Expand Up @@ -72,3 +73,34 @@ func NewDagFromFile(fpath string) (*dag.Node, error) {

return NewDagFromReader(f)
}

func ImportFileDag(fpath string, dw *uio.DagWriter) (*dag.Node, error) {
stat, err := os.Stat(fpath)
if err != nil {
return nil, err
}

if stat.IsDir() {
return nil, fmt.Errorf("`%s` is a directory", fpath)
}

f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()

return ImportReaderDag(f, dw)
}

func ImportReaderDag(r io.Reader, dw *uio.DagWriter) (*dag.Node, error) {
_, err := io.Copy(dw, r)
if err != nil {
return nil, err
}
err = dw.Close()
if err != nil {
return nil, err
}
return dw.GetNode(), nil
}
26 changes: 26 additions & 0 deletions pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ var recursePinDatastoreKey = ds.NewKey("/local/pins/recursive/keys")
var directPinDatastoreKey = ds.NewKey("/local/pins/direct/keys")
var indirectPinDatastoreKey = ds.NewKey("/local/pins/indirect/keys")

type PinMode int

const (
Recursive PinMode = iota
Direct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong, weak? (common vocabulary for this operation)

Indirect
)

type Pinner interface {
IsPinned(util.Key) bool
Pin(*mdag.Node, bool) error
Unpin(util.Key, bool) error
Flush() error
}

// ManualPinner is for manually editing the pin structure
// Use with care
type ManualPinner interface {
PinWithMode(util.Key, PinMode)
Pinner
}

type pinner struct {
lock sync.RWMutex
recursePin set.BlockSet
Expand Down Expand Up @@ -228,3 +243,14 @@ func loadSet(d ds.Datastore, k ds.Key, val interface{}) error {
}
return json.Unmarshal(bf, val)
}

func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
switch mode {
case Recursive:
p.recursePin.AddBlock(k)
case Direct:
p.directPin.AddBlock(k)
case Indirect:
p.indirPin.Increment(k)
}
}
12 changes: 10 additions & 2 deletions unixfs/io/dagwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io
import (
"github.com/jbenet/go-ipfs/importer/chunk"
dag "github.com/jbenet/go-ipfs/merkledag"
"github.com/jbenet/go-ipfs/pin"
ft "github.com/jbenet/go-ipfs/unixfs"
"github.com/jbenet/go-ipfs/util"
)
Expand All @@ -17,6 +18,7 @@ type DagWriter struct {
done chan struct{}
splitter chunk.BlockSplitter
seterr error
Pinner pin.ManualPinner
}

func NewDagWriter(ds dag.DAGService, splitter chunk.BlockSplitter) *DagWriter {
Expand Down Expand Up @@ -48,7 +50,10 @@ func (dw *DagWriter) startSplitter() {
// Store the block size in the root node
mbf.AddBlockSize(uint64(len(blkData)))
node := &dag.Node{Data: ft.WrapData(blkData)}
_, err := dw.dagserv.Add(node)
nk, err := dw.dagserv.Add(node)
if dw.Pinner != nil {
dw.Pinner.PinWithMode(nk, pin.Indirect)
}
if err != nil {
dw.seterr = err
log.Critical("Got error adding created node to dagservice: %s", err)
Expand All @@ -75,12 +80,15 @@ func (dw *DagWriter) startSplitter() {
root.Data = data

// Add root node to the dagservice
_, err = dw.dagserv.Add(root)
rootk, err := dw.dagserv.Add(root)
if err != nil {
dw.seterr = err
log.Critical("Got error adding created node to dagservice: %s", err)
return
}
if dw.Pinner != nil {
dw.Pinner.PinWithMode(rootk, pin.Recursive)
}
dw.node = root
dw.done <- struct{}{}
}
Expand Down