@cryptotaxi247 / kubo / commits / 223ee4df1

refactor(core): move Add, Cat to core/io

Brian Tiger Chow committed Jan 10, 2015 at 23:12 UTC 223ee4df1a778dbe5654787e12221f6616b2354c
5 files changed +72 -38
core/core.go
-33
@@ -2,7 +2,6 @@ package core
2
3 import (
4 "fmt"
5 - "io"
5
6 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
@@ -19,8 +18,6 @@ import (
18 bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
19 offline "github.com/jbenet/go-ipfs/exchange/offline"
20 mount "github.com/jbenet/go-ipfs/fuse/mount"
22 - importer "github.com/jbenet/go-ipfs/importer"
23 - chunk "github.com/jbenet/go-ipfs/importer/chunk"
21 merkledag "github.com/jbenet/go-ipfs/merkledag"
22 namesys "github.com/jbenet/go-ipfs/namesys"
23 ic "github.com/jbenet/go-ipfs/p2p/crypto"
@@ -32,8 +29,6 @@ import (
29 pin "github.com/jbenet/go-ipfs/pin"
30 routing "github.com/jbenet/go-ipfs/routing"
31 dht "github.com/jbenet/go-ipfs/routing/dht"
35 - uio "github.com/jbenet/go-ipfs/unixfs/io"
36 - u "github.com/jbenet/go-ipfs/util"
32 ds2 "github.com/jbenet/go-ipfs/util/datastore2"
33 debugerror "github.com/jbenet/go-ipfs/util/debugerror"
34 eventlog "github.com/jbenet/go-ipfs/util/eventlog"
@@ -281,34 +276,6 @@ func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
276 return nil
277 }
278
284 -// TODO we may not want to add these methods to the core. Maybe they should be
285 -// defined as free functions in another package that use public fields on the
286 -// node.
287 -//
288 -// e.g. reader, err := unix.Cat(node)
289 -
290 -func (n *IpfsNode) Cat(k u.Key) (io.Reader, error) {
291 - catterdag := n.DAG
292 - nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
293 - if err != nil {
294 - return nil, err
295 - }
296 - return uio.NewDagReader(nodeCatted, catterdag)
297 -}
298 -
299 -func (n *IpfsNode) Add(r io.Reader) (u.Key, error) {
300 - nodeAdded, err := importer.BuildDagFromReader(
301 - r,
302 - n.DAG,
303 - nil,
304 - chunk.DefaultSplitter,
305 - )
306 - if err != nil {
307 - return "", err
308 - }
309 - return nodeAdded.Key()
310 -}
311 -
279 func (n *IpfsNode) loadID() error {
280 if n.Identity != "" {
281 return debugerror.New("identity already loaded")
core/io/add.go new
+35
@@ -0,0 +1,35 @@
1 +package core_io
2 +
3 +// TODO rename package to something that doesn't conflict with io/ioutil.
4 +// Pretty names are hard to find.
5 +//
6 +// Candidates:
7 +//
8 +// go-ipfs/core/unix
9 +// go-ipfs/core/io
10 +// go-ipfs/core/ioutil
11 +// go-ipfs/core/coreio
12 +// go-ipfs/core/coreunix
13 +
14 +import (
15 + "io"
16 +
17 + core "github.com/jbenet/go-ipfs/core"
18 + importer "github.com/jbenet/go-ipfs/importer"
19 + chunk "github.com/jbenet/go-ipfs/importer/chunk"
20 + u "github.com/jbenet/go-ipfs/util"
21 +)
22 +
23 +func Add(n *core.IpfsNode, r io.Reader) (u.Key, error) {
24 + // TODO more attractive function signature importer.BuildDagFromReader
25 + dagNode, err := importer.BuildDagFromReader(
26 + r,
27 + n.DAG,
28 + nil,
29 + chunk.DefaultSplitter,
30 + )
31 + if err != nil {
32 + return "", err
33 + }
34 + return dagNode.Key()
35 +}
core/io/cat.go new
+30
@@ -0,0 +1,30 @@
1 +package core_io
2 +
3 +// TODO rename package to something that doesn't conflict with io/ioutil.
4 +// Pretty names are hard to find.
5 +//
6 +// Candidates:
7 +//
8 +// go-ipfs/core/unix
9 +// go-ipfs/core/io
10 +// go-ipfs/core/ioutil
11 +// go-ipfs/core/coreio
12 +// go-ipfs/core/coreunix
13 +
14 +import (
15 + "io"
16 +
17 + core "github.com/jbenet/go-ipfs/core"
18 + path "github.com/jbenet/go-ipfs/path"
19 + uio "github.com/jbenet/go-ipfs/unixfs/io"
20 + u "github.com/jbenet/go-ipfs/util"
21 +)
22 +
23 +func Cat(n *core.IpfsNode, k u.Key) (io.Reader, error) {
24 + dag := n.DAG
25 + dagNode, err := (&path.Resolver{dag}).ResolvePath(k.String())
26 + if err != nil {
27 + return nil, err
28 + }
29 + return uio.NewDagReader(dagNode, dag)
30 +}
test/epictest/addcat_test.go
+3 -2
@@ -12,6 +12,7 @@ import (
12 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
13 random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
14 "github.com/jbenet/go-ipfs/core"
15 + core_io "github.com/jbenet/go-ipfs/core/io"
16 mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
17 "github.com/jbenet/go-ipfs/p2p/peer"
18 errors "github.com/jbenet/go-ipfs/util/debugerror"
@@ -114,12 +115,12 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
115 catter.Bootstrap(ctx, []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)})
116 adder.Bootstrap(ctx, []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)})
117
117 - keyAdded, err := adder.Add(bytes.NewReader(data))
118 + keyAdded, err := core_io.Add(adder, bytes.NewReader(data))
119 if err != nil {
120 return err
121 }
122
122 - readerCatted, err := catter.Cat(keyAdded)
123 + readerCatted, err := core_io.Cat(catter, keyAdded)
124 if err != nil {
125 return err
126 }
test/epictest/three_legged_cat_test.go
+4 -3
@@ -7,7 +7,8 @@ import (
7 "testing"
8
9 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10 - "github.com/jbenet/go-ipfs/core"
10 + core "github.com/jbenet/go-ipfs/core"
11 + core_io "github.com/jbenet/go-ipfs/core/io"
12 mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
13 "github.com/jbenet/go-ipfs/p2p/peer"
14 errors "github.com/jbenet/go-ipfs/util/debugerror"
@@ -61,12 +62,12 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
62 adder.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
63 catter.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
64
64 - keyAdded, err := adder.Add(bytes.NewReader(data))
65 + keyAdded, err := core_io.Add(adder, bytes.NewReader(data))
66 if err != nil {
67 return err
68 }
69
69 - readerCatted, err := catter.Cat(keyAdded)
70 + readerCatted, err := core_io.Cat(catter, keyAdded)
71 if err != nil {
72 return err
73 }