Skip to content

Commit

Permalink
Bundle services needed by the adder into DataServices.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed May 23, 2016
1 parent 6cadac6 commit c41a47e
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
return nil, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}

s, err := coreunix.Add(nd, bytes.NewBuffer(d))
s, err := coreunix.Add(nd.DataServices(nil), bytes.NewBuffer(d))
if err != nil {
return nil, fmt.Errorf("assets: could not Add '%s': %s", p, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func run(ipfsPath, watchPath string) error {
log.Println(err)
}
defer file.Close()
k, err := coreunix.Add(node, file)
k, err := coreunix.Add(node.DataServices(nil), file)
if err != nil {
log.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ You can now refer to the added file in a gateway, like so:
outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))

fileAdder, err := coreunix.NewAdder(req.Context(), n, outChan)
fileAdder, err := coreunix.NewAdder(n.DataServices(req.Context()), outChan)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
1 change: 0 additions & 1 deletion core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func (w *chanWriterByFile) Read(p []byte) (int, error) {
return sz, nil
}


var verifyFileStore = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Verify objects in filestore",
Expand Down
14 changes: 14 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,17 @@ type DiscoveryOption func(p2phost.Host) (discovery.Service, error)

var DHTOption RoutingOption = constructDHTRouting
var NilRouterOption RoutingOption = nilrouting.ConstructNilRouting

func (n *IpfsNode) DataServices(ctx context.Context) *DataServices {
if ctx == nil {
ctx = n.Context()
}
return &DataServices{ctx, n.Pinning, n.Blockstore, n.DAG}
}

type DataServices struct {
Context context.Context
Pinning pin.Pinner
Blockstore bstore.GCBlockstore
DAG merkledag.DAGService
}
12 changes: 6 additions & 6 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestGatewayGet(t *testing.T) {
ts, n := newTestServerAndNode(t, ns)
defer ts.Close()

k, err := coreunix.Add(n, strings.NewReader("fnord"))
k, err := coreunix.Add(n.DataServices(nil), strings.NewReader("fnord"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -168,11 +168,11 @@ func TestIPNSHostnameRedirect(t *testing.T) {
defer ts.Close()

// create /ipns/example.net/foo/index.html
_, dagn1, err := coreunix.AddWrapped(n, strings.NewReader("_"), "_")
_, dagn1, err := coreunix.AddWrapped(n.DataServices(nil), strings.NewReader("_"), "_")
if err != nil {
t.Fatal(err)
}
_, dagn2, err := coreunix.AddWrapped(n, strings.NewReader("_"), "index.html")
_, dagn2, err := coreunix.AddWrapped(n.DataServices(nil), strings.NewReader("_"), "index.html")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -253,15 +253,15 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
defer ts.Close()

// create /ipns/example.net/foo/
_, dagn1, err := coreunix.AddWrapped(n, strings.NewReader("1"), "file.txt")
_, dagn1, err := coreunix.AddWrapped(n.DataServices(nil), strings.NewReader("1"), "file.txt")
if err != nil {
t.Fatal(err)
}
_, dagn2, err := coreunix.AddWrapped(n, strings.NewReader("2"), "file.txt")
_, dagn2, err := coreunix.AddWrapped(n.DataServices(nil), strings.NewReader("2"), "file.txt")
if err != nil {
t.Fatal(err)
}
_, dagn3, err := coreunix.AddWrapped(n, strings.NewReader("3"), "file.txt")
_, dagn3, err := coreunix.AddWrapped(n.DataServices(nil), strings.NewReader("3"), "file.txt")
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 11 additions & 11 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package coreunix

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"errors"
gopath "path"

ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -68,15 +68,15 @@ type AddedObject struct {
Bytes int64 `json:",omitempty"`
}

func NewAdder(ctx context.Context, n *core.IpfsNode, out chan interface{}) (*Adder, error) {
mr, err := mfs.NewRoot(ctx, n.DAG, newDirNode(), nil)
func NewAdder(n *core.DataServices, out chan interface{}) (*Adder, error) {
mr, err := mfs.NewRoot(n.Context, n.DAG, newDirNode(), nil)
if err != nil {
return nil, err
}

return &Adder{
mr: mr,
ctx: ctx,
ctx: n.Context,
node: n,
out: out,
Progress: false,
Expand All @@ -91,7 +91,7 @@ func NewAdder(ctx context.Context, n *core.IpfsNode, out chan interface{}) (*Add
// Internal structure for holding the switches passed to the `add` call
type Adder struct {
ctx context.Context
node *core.IpfsNode
node *core.DataServices
out chan interface{}
Progress bool
Hidden bool
Expand Down Expand Up @@ -259,10 +259,10 @@ func (adder *Adder) outputDirs(path string, fs mfs.FSNode) error {

// Add builds a merkledag from the a reader, pinning all objects to the local
// datastore. Returns a key representing the root node.
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
func Add(n *core.DataServices, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()

fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n, nil)
if err != nil {
return "", err
}
Expand All @@ -282,7 +282,7 @@ func Add(n *core.IpfsNode, r io.Reader) (string, error) {
}

// AddR recursively adds files in |path|.
func AddR(n *core.IpfsNode, root string) (key string, err error) {
func AddR(n *core.DataServices, root string) (key string, err error) {
n.Blockstore.PinLock().Unlock()

stat, err := os.Lstat(root)
Expand All @@ -296,7 +296,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
}
defer f.Close()

fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n, nil)
if err != nil {
return "", err
}
Expand All @@ -323,9 +323,9 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
// to preserve the filename.
// Returns the path of the added file ("<dir hash>/filename"), the DAG node of
// the directory, and and error if any.
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *dag.Node, error) {
func AddWrapped(n *core.DataServices, r io.Reader, filename string) (string, *dag.Node, error) {
file := files.NewReaderFile(filename, filename, ioutil.NopCloser(r), nil)
fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n, nil)
if err != nil {
return "", nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/coreunix/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAddRecursive(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if k, err := AddR(node, "test_data"); err != nil {
if k, err := AddR(node.DataServices(nil), "test_data"); err != nil {
t.Fatal(err)
} else if k != "QmWCCga8AbTyfAQ7pTnGT6JgmRMAB3Qp8ZmTEFi5q5o8jC" {
t.Fatal("keys do not match: ", k)
Expand All @@ -54,7 +54,7 @@ func TestAddGCLive(t *testing.T) {

errs := make(chan error)
out := make(chan interface{})
adder, err := NewAdder(context.Background(), node, out)
adder, err := NewAdder(node.DataServices(context.Background()), out)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/addcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
return err
}

added, err := coreunix.Add(adder, bytes.NewReader(data))
added, err := coreunix.Add(adder.DataServices(nil), bytes.NewReader(data))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/bench_cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
return err
}

added, err := coreunix.Add(adder, bytes.NewReader(data))
added, err := coreunix.Add(adder.DataServices(nil), bytes.NewReader(data))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/grandcentral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func RunSupernodeBootstrappedAddCat(data []byte, conf testutil.LatencyConfig) er
log.Info("adder is", adder.Identity)
log.Info("catter is", catter.Identity)

keyAdded, err := coreunix.Add(adder, bytes.NewReader(data))
keyAdded, err := coreunix.Add(adder.DataServices(nil), bytes.NewReader(data))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/three_legged_cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
return err
}

added, err := coreunix.Add(adder, bytes.NewReader(data))
added, err := coreunix.Add(adder.DataServices(nil), bytes.NewReader(data))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions test/supernode_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func runFileAddingWorker(n *core.IpfsNode) error {
errs <- err
}
}()
k, err := coreunix.Add(n, piper)
k, err := coreunix.Add(n.DataServices(nil), piper)
if err != nil {
errs <- err
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func runFileCattingWorker(ctx context.Context, n *core.IpfsNode) error {
errs <- err
}
// add to a dummy node to discover the key
k, err := coreunix.Add(dummy, bytes.NewReader(buf.Bytes()))
k, err := coreunix.Add(dummy.DataServices(nil), bytes.NewReader(buf.Bytes()))
if err != nil {
errs <- err
}
Expand Down

0 comments on commit c41a47e

Please sign in to comment.