@cryptotaxi247 / kubo / commits / 2b9d67df8

core construction

Juan Batiz-Benet committed Jul 5, 2014 at 13:21 UTC 2b9d67df812c6310c99c2a5e7211040a048fcc28
3 files changed +105 -12
core/core.go
+41 -12
@@ -1,33 +1,62 @@
1 package core
2
3 +import (
4 + config "github.com/jbenet/go-ipfs/config"
5 + ds "github.com/jbenet/datastore.go"
6 + "fmt"
7 + peer "github.com/jbenet/go-ipfs/peer"
8 +)
9 +
10 // IPFS Core module. It represents an IPFS instance.
11
5 -type IPFSNode struct {
12 +type IpfsNode struct {
13 +
14 + // the node's configuration
15 + Config *config.Config
16
7 - // the local node's identity (a Peer instance)
17 + // the local node's identity
18 Identity *peer.Peer
19
10 - // the book of other nodes (a hashtable of Peer instances)
11 - PeerBook *peerbook.PeerBook
20 + // the book of other nodes (a map of Peer instances)
21 + PeerBook *peer.PeerBook
22
13 - // the local database (uses datastore)
14 - Storage *storage.Storage
23 + // the local datastore
24 + Datastore ds.Datastore
25
26 // the network message stream
17 - Network *netmux.Netux
27 + // Network *netmux.Netux
28
29 // the routing system. recommend ipfs-dht
20 - Routing *routing.Routing
30 + // Routing *routing.Routing
31
32 // the block exchange + strategy (bitswap)
23 - BitSwap *bitswap.BitSwap
33 + // BitSwap *bitswap.BitSwap
34
35 // the block service, get/add blocks.
26 - Blocks *blocks.BlockService
36 + // Blocks *blocks.BlockService
37
38 // the path resolution system
29 - Resolver *resolver.PathResolver
39 + // Resolver *resolver.PathResolver
40
41 // the name system, resolves paths to hashes
32 - Namesys *namesys.Namesys
42 + // Namesys *namesys.Namesys
43 +}
44 +
45 +func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
46 + if cfg == nil {
47 + return nil, fmt.Errorf("configuration required.")
48 + }
49 +
50 + d, err := makeDatastore(cfg.Datastore)
51 + if err != nil {
52 + return nil, err
53 + }
54 +
55 + n := &IpfsNode{
56 + Config: cfg,
57 + PeerBook: &peer.PeerBook{},
58 + Datastore: d,
59 + }
60 +
61 + return n, nil
62 }
core/core_test.go new
+35
@@ -0,0 +1,35 @@
1 +package core
2 +
3 +import (
4 + "testing"
5 + config "github.com/jbenet/go-ipfs/config"
6 +)
7 +
8 +func TestDatastores(t *testing.T) {
9 +
10 + good := []*config.Config {
11 + &config.Config{ Datastore: &config.Datastore{Type: "memory"} },
12 + &config.Config{ Datastore: &config.Datastore{Type: "leveldb", Path: ".testdb"} },
13 + }
14 +
15 + bad := []*config.Config {
16 + &config.Config{ Datastore: &config.Datastore{} },
17 + &config.Config{ Datastore: &config.Datastore{Type: "badtype"} },
18 + &config.Config{ },
19 + nil,
20 + }
21 +
22 + for i, c := range(good) {
23 + n, err := NewIpfsNode(c)
24 + if n == nil || err != nil {
25 + t.Error("Should have constructed.", i, err)
26 + }
27 + }
28 +
29 + for i, c := range(bad) {
30 + n, err := NewIpfsNode(c)
31 + if n != nil || err == nil {
32 + t.Error("Should have failed to construct.", i)
33 + }
34 + }
35 +}
core/datastore.go new
+29
@@ -0,0 +1,29 @@
1 +package core
2 +
3 +import (
4 + config "github.com/jbenet/go-ipfs/config"
5 + ds "github.com/jbenet/datastore.go"
6 + "fmt"
7 + lds "github.com/jbenet/datastore.go/leveldb"
8 +)
9 +
10 +func makeDatastore(cfg *config.Datastore) (ds.Datastore, error) {
11 + if cfg == nil || len(cfg.Type) == 0 {
12 + return nil, fmt.Errorf("config datastore.type required")
13 + }
14 +
15 + switch cfg.Type {
16 + case "leveldb": return makeLevelDBDatastore(cfg)
17 + case "memory": return ds.NewMapDatastore(), nil
18 + }
19 +
20 + return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
21 +}
22 +
23 +func makeLevelDBDatastore(cfg *config.Datastore) (ds.Datastore, error) {
24 + if len(cfg.Path) == 0 {
25 + return nil, fmt.Errorf("config datastore.path required for leveldb")
26 + }
27 +
28 + return lds.NewDatastore(cfg.Path, nil)
29 +}