This commit is contained in:
Juan Batiz-Benet 2014-07-05 13:21:30 -07:00
parent 2b9d67df81
commit 2d7f2473a5
3 changed files with 79 additions and 77 deletions

View File

@ -1,62 +1,62 @@
package core
import (
config "github.com/jbenet/go-ipfs/config"
ds "github.com/jbenet/datastore.go"
"fmt"
peer "github.com/jbenet/go-ipfs/peer"
"fmt"
ds "github.com/jbenet/datastore.go"
config "github.com/jbenet/go-ipfs/config"
peer "github.com/jbenet/go-ipfs/peer"
)
// IPFS Core module. It represents an IPFS instance.
type IpfsNode struct {
// the node's configuration
Config *config.Config
// the node's configuration
Config *config.Config
// the local node's identity
Identity *peer.Peer
// the local node's identity
Identity *peer.Peer
// the book of other nodes (a map of Peer instances)
PeerBook *peer.PeerBook
// the book of other nodes (a map of Peer instances)
PeerBook *peer.PeerBook
// the local datastore
Datastore ds.Datastore
// the local datastore
Datastore ds.Datastore
// the network message stream
// Network *netmux.Netux
// the network message stream
// Network *netmux.Netux
// the routing system. recommend ipfs-dht
// Routing *routing.Routing
// the routing system. recommend ipfs-dht
// Routing *routing.Routing
// the block exchange + strategy (bitswap)
// BitSwap *bitswap.BitSwap
// the block exchange + strategy (bitswap)
// BitSwap *bitswap.BitSwap
// the block service, get/add blocks.
// Blocks *blocks.BlockService
// the block service, get/add blocks.
// Blocks *blocks.BlockService
// the path resolution system
// Resolver *resolver.PathResolver
// the path resolution system
// Resolver *resolver.PathResolver
// the name system, resolves paths to hashes
// Namesys *namesys.Namesys
// the name system, resolves paths to hashes
// Namesys *namesys.Namesys
}
func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
if cfg == nil {
return nil, fmt.Errorf("configuration required.")
}
if cfg == nil {
return nil, fmt.Errorf("configuration required.")
}
d, err := makeDatastore(cfg.Datastore)
if err != nil {
return nil, err
}
d, err := makeDatastore(cfg.Datastore)
if err != nil {
return nil, err
}
n := &IpfsNode{
Config: cfg,
PeerBook: &peer.PeerBook{},
Datastore: d,
}
n := &IpfsNode{
Config: cfg,
PeerBook: &peer.PeerBook{},
Datastore: d,
}
return n, nil
return n, nil
}

View File

@ -1,35 +1,35 @@
package core
import (
"testing"
config "github.com/jbenet/go-ipfs/config"
config "github.com/jbenet/go-ipfs/config"
"testing"
)
func TestDatastores(t *testing.T) {
good := []*config.Config {
&config.Config{ Datastore: &config.Datastore{Type: "memory"} },
&config.Config{ Datastore: &config.Datastore{Type: "leveldb", Path: ".testdb"} },
}
good := []*config.Config{
&config.Config{Datastore: &config.Datastore{Type: "memory"}},
&config.Config{Datastore: &config.Datastore{Type: "leveldb", Path: ".testdb"}},
}
bad := []*config.Config {
&config.Config{ Datastore: &config.Datastore{} },
&config.Config{ Datastore: &config.Datastore{Type: "badtype"} },
&config.Config{ },
nil,
}
bad := []*config.Config{
&config.Config{Datastore: &config.Datastore{}},
&config.Config{Datastore: &config.Datastore{Type: "badtype"}},
&config.Config{},
nil,
}
for i, c := range(good) {
n, err := NewIpfsNode(c)
if n == nil || err != nil {
t.Error("Should have constructed.", i, err)
}
}
for i, c := range good {
n, err := NewIpfsNode(c)
if n == nil || err != nil {
t.Error("Should have constructed.", i, err)
}
}
for i, c := range(bad) {
n, err := NewIpfsNode(c)
if n != nil || err == nil {
t.Error("Should have failed to construct.", i)
}
}
for i, c := range bad {
n, err := NewIpfsNode(c)
if n != nil || err == nil {
t.Error("Should have failed to construct.", i)
}
}
}

View File

@ -1,29 +1,31 @@
package core
import (
config "github.com/jbenet/go-ipfs/config"
ds "github.com/jbenet/datastore.go"
"fmt"
lds "github.com/jbenet/datastore.go/leveldb"
"fmt"
ds "github.com/jbenet/datastore.go"
lds "github.com/jbenet/datastore.go/leveldb"
config "github.com/jbenet/go-ipfs/config"
)
func makeDatastore(cfg *config.Datastore) (ds.Datastore, error) {
if cfg == nil || len(cfg.Type) == 0 {
return nil, fmt.Errorf("config datastore.type required")
}
if cfg == nil || len(cfg.Type) == 0 {
return nil, fmt.Errorf("config datastore.type required")
}
switch cfg.Type {
case "leveldb": return makeLevelDBDatastore(cfg)
case "memory": return ds.NewMapDatastore(), nil
}
switch cfg.Type {
case "leveldb":
return makeLevelDBDatastore(cfg)
case "memory":
return ds.NewMapDatastore(), nil
}
return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
}
func makeLevelDBDatastore(cfg *config.Datastore) (ds.Datastore, error) {
if len(cfg.Path) == 0 {
return nil, fmt.Errorf("config datastore.path required for leveldb")
}
if len(cfg.Path) == 0 {
return nil, fmt.Errorf("config datastore.path required for leveldb")
}
return lds.NewDatastore(cfg.Path, nil)
return lds.NewDatastore(cfg.Path, nil)
}