mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 01:52:26 +08:00
refactor(core): move Add, Cat to core/io
This commit is contained in:
33
core/core.go
33
core/core.go
@ -2,7 +2,6 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
|
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
|
||||||
@ -19,8 +18,6 @@ import (
|
|||||||
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
||||||
offline "github.com/jbenet/go-ipfs/exchange/offline"
|
offline "github.com/jbenet/go-ipfs/exchange/offline"
|
||||||
mount "github.com/jbenet/go-ipfs/fuse/mount"
|
mount "github.com/jbenet/go-ipfs/fuse/mount"
|
||||||
importer "github.com/jbenet/go-ipfs/importer"
|
|
||||||
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
|
||||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
namesys "github.com/jbenet/go-ipfs/namesys"
|
namesys "github.com/jbenet/go-ipfs/namesys"
|
||||||
ic "github.com/jbenet/go-ipfs/p2p/crypto"
|
ic "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
@ -32,8 +29,6 @@ import (
|
|||||||
pin "github.com/jbenet/go-ipfs/pin"
|
pin "github.com/jbenet/go-ipfs/pin"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
|
||||||
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||||
@ -281,34 +276,6 @@ func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO we may not want to add these methods to the core. Maybe they should be
|
|
||||||
// defined as free functions in another package that use public fields on the
|
|
||||||
// node.
|
|
||||||
//
|
|
||||||
// e.g. reader, err := unix.Cat(node)
|
|
||||||
|
|
||||||
func (n *IpfsNode) Cat(k u.Key) (io.Reader, error) {
|
|
||||||
catterdag := n.DAG
|
|
||||||
nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return uio.NewDagReader(nodeCatted, catterdag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *IpfsNode) Add(r io.Reader) (u.Key, error) {
|
|
||||||
nodeAdded, err := importer.BuildDagFromReader(
|
|
||||||
r,
|
|
||||||
n.DAG,
|
|
||||||
nil,
|
|
||||||
chunk.DefaultSplitter,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return nodeAdded.Key()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *IpfsNode) loadID() error {
|
func (n *IpfsNode) loadID() error {
|
||||||
if n.Identity != "" {
|
if n.Identity != "" {
|
||||||
return debugerror.New("identity already loaded")
|
return debugerror.New("identity already loaded")
|
||||||
|
35
core/io/add.go
Normal file
35
core/io/add.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package core_io
|
||||||
|
|
||||||
|
// TODO rename package to something that doesn't conflict with io/ioutil.
|
||||||
|
// Pretty names are hard to find.
|
||||||
|
//
|
||||||
|
// Candidates:
|
||||||
|
//
|
||||||
|
// go-ipfs/core/unix
|
||||||
|
// go-ipfs/core/io
|
||||||
|
// go-ipfs/core/ioutil
|
||||||
|
// go-ipfs/core/coreio
|
||||||
|
// go-ipfs/core/coreunix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
|
importer "github.com/jbenet/go-ipfs/importer"
|
||||||
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Add(n *core.IpfsNode, r io.Reader) (u.Key, error) {
|
||||||
|
// TODO more attractive function signature importer.BuildDagFromReader
|
||||||
|
dagNode, err := importer.BuildDagFromReader(
|
||||||
|
r,
|
||||||
|
n.DAG,
|
||||||
|
nil,
|
||||||
|
chunk.DefaultSplitter,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return dagNode.Key()
|
||||||
|
}
|
30
core/io/cat.go
Normal file
30
core/io/cat.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package core_io
|
||||||
|
|
||||||
|
// TODO rename package to something that doesn't conflict with io/ioutil.
|
||||||
|
// Pretty names are hard to find.
|
||||||
|
//
|
||||||
|
// Candidates:
|
||||||
|
//
|
||||||
|
// go-ipfs/core/unix
|
||||||
|
// go-ipfs/core/io
|
||||||
|
// go-ipfs/core/ioutil
|
||||||
|
// go-ipfs/core/coreio
|
||||||
|
// go-ipfs/core/coreunix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
|
path "github.com/jbenet/go-ipfs/path"
|
||||||
|
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||||
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Cat(n *core.IpfsNode, k u.Key) (io.Reader, error) {
|
||||||
|
dag := n.DAG
|
||||||
|
dagNode, err := (&path.Resolver{dag}).ResolvePath(k.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return uio.NewDagReader(dagNode, dag)
|
||||||
|
}
|
@ -12,6 +12,7 @@ import (
|
|||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
|
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
|
||||||
"github.com/jbenet/go-ipfs/core"
|
"github.com/jbenet/go-ipfs/core"
|
||||||
|
core_io "github.com/jbenet/go-ipfs/core/io"
|
||||||
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
|
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
|
||||||
"github.com/jbenet/go-ipfs/p2p/peer"
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
@ -114,12 +115,12 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
|
|||||||
catter.Bootstrap(ctx, []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)})
|
catter.Bootstrap(ctx, []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)})
|
||||||
adder.Bootstrap(ctx, []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)})
|
adder.Bootstrap(ctx, []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)})
|
||||||
|
|
||||||
keyAdded, err := adder.Add(bytes.NewReader(data))
|
keyAdded, err := core_io.Add(adder, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
readerCatted, err := catter.Cat(keyAdded)
|
readerCatted, err := core_io.Cat(catter, keyAdded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
"github.com/jbenet/go-ipfs/core"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
|
core_io "github.com/jbenet/go-ipfs/core/io"
|
||||||
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
|
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
|
||||||
"github.com/jbenet/go-ipfs/p2p/peer"
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
@ -61,12 +62,12 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
|
|||||||
adder.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
|
adder.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
|
||||||
catter.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
|
catter.Bootstrap(ctx, []peer.PeerInfo{boostrapInfo})
|
||||||
|
|
||||||
keyAdded, err := adder.Add(bytes.NewReader(data))
|
keyAdded, err := core_io.Add(adder, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
readerCatted, err := catter.Cat(keyAdded)
|
readerCatted, err := core_io.Cat(catter, keyAdded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user