refactor: move add and cat to the core
Brian Tiger Chow committed
Jan 10, 2015 at 19:52 UTC
efb75ee5ba5b87fb4755b884c807be9652e48097
2 files changed
+33
-29
core/core.go
+33
@@ -3,6 +3,7 @@ package core
3
import (
4
"errors"
5
"fmt"
6
+ "io"
7
8
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
@@ -18,6 +19,8 @@ import (
19
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
20
"github.com/jbenet/go-ipfs/exchange/offline"
21
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"
24
merkledag "github.com/jbenet/go-ipfs/merkledag"
25
namesys "github.com/jbenet/go-ipfs/namesys"
26
ic "github.com/jbenet/go-ipfs/p2p/crypto"
@@ -29,6 +32,8 @@ import (
32
pin "github.com/jbenet/go-ipfs/pin"
33
routing "github.com/jbenet/go-ipfs/routing"
34
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"
37
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
38
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
39
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
@@ -274,6 +279,34 @@ func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
279
return nil
280
}
281
282
+// TODO we may not want to add these methods to the core. Maybe they should be
283
+// defined as free functions in another package that use public fields on the
284
+// node.
285
+//
286
+// e.g. reader, err := unix.Cat(node)
287
+
288
+func (n *IpfsNode) Cat(k u.Key) (io.Reader, error) {
289
+ catterdag := n.DAG
290
+ nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
291
+ if err != nil {
292
+ return nil, err
293
+ }
294
+ return uio.NewDagReader(nodeCatted, catterdag)
295
+}
296
+
297
+func (n *IpfsNode) Add(r io.Reader) (u.Key, error) {
298
+ nodeAdded, err := importer.BuildDagFromReader(
299
+ r,
300
+ n.DAG,
301
+ nil,
302
+ chunk.DefaultSplitter,
303
+ )
304
+ if err != nil {
305
+ return "", err
306
+ }
307
+ return nodeAdded.Key()
308
+}
309
+
310
func (n *IpfsNode) loadID() error {
311
if n.Identity != "" {
312
return debugerror.New("identity already loaded")
test/epictest/core.go
-29
@@ -1,8 +1,6 @@
1
package epictest
2
3
import (
4
- "io"
5
-
4
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
6
sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
@@ -13,15 +11,10 @@ import (
11
exchange "github.com/jbenet/go-ipfs/exchange"
12
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
13
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
16
- importer "github.com/jbenet/go-ipfs/importer"
17
- chunk "github.com/jbenet/go-ipfs/importer/chunk"
14
merkledag "github.com/jbenet/go-ipfs/merkledag"
15
host "github.com/jbenet/go-ipfs/p2p/host"
16
peer "github.com/jbenet/go-ipfs/p2p/peer"
21
- path "github.com/jbenet/go-ipfs/path"
17
dht "github.com/jbenet/go-ipfs/routing/dht"
23
- uio "github.com/jbenet/go-ipfs/unixfs/io"
24
- util "github.com/jbenet/go-ipfs/util"
18
"github.com/jbenet/go-ipfs/util/datastore2"
19
delay "github.com/jbenet/go-ipfs/util/delay"
20
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
@@ -43,28 +36,6 @@ func (c *Core) Bootstrap(ctx context.Context, p peer.PeerInfo) error {
36
return c.IpfsNode.Bootstrap(ctx, []peer.PeerInfo{p})
37
}
38
46
-func (c *Core) Cat(k util.Key) (io.Reader, error) {
47
- catterdag := c.IpfsNode.DAG
48
- nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
49
- if err != nil {
50
- return nil, err
51
- }
52
- return uio.NewDagReader(nodeCatted, catterdag)
53
-}
54
-
55
-func (c *Core) Add(r io.Reader) (util.Key, error) {
56
- nodeAdded, err := importer.BuildDagFromReader(
57
- r,
58
- c.IpfsNode.DAG,
59
- nil,
60
- chunk.DefaultSplitter,
61
- )
62
- if err != nil {
63
- return "", err
64
- }
65
- return nodeAdded.Key()
66
-}
67
-
39
func makeCore(ctx context.Context, rf RepoFactory) (*Core, error) {
40
node, err := rf(ctx)
41
if err != nil {