-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package importer | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"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" | ||
"github.com/jbenet/go-ipfs/util" | ||
) | ||
|
||
|
@@ -72,3 +75,41 @@ func NewDagFromFile(fpath string) (*dag.Node, error) { | |
|
||
return NewDagFromReader(f) | ||
} | ||
|
||
func NewDagFromFileWServer(fpath string, dserv dag.DAGService, p pin.Pinner) (*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 NewDagFromReaderWServer(f, dserv, p) | ||
} | ||
|
||
func NewDagFromReaderWServer(r io.Reader, dserv dag.DAGService, p pin.Pinner) (*dag.Node, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or |
||
dw := uio.NewDagWriter(dserv, chunk.DefaultSplitter) | ||
|
||
mp, ok := p.(pin.ManualPinner) | ||
if !ok { | ||
return nil, errors.New("Needed to be passed a manual pinner!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if really only works with a ManualPinner, could make this a param of the function. If this is liable to change later, then keep the |
||
} | ||
dw.Pinner = mp | ||
_, err := io.Copy(dw, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = dw.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dw.GetNode(), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...WServer
?...WithService
?Since these are more complicated construction funcs, no need to adhere to the
New...
form. Could easily be named:ImportFileDag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Ill give them nicer names now, lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, seems like you could just pass the DagWriter in directly. As in, maybe this function should just be taking a dagwriter.