Commit bacf3ecc authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

feat(util) add datastore Closer Wrapper

@jbenet

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent f26388e0
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ 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"
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"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
bserv "github.com/jbenet/go-ipfs/blockservice" bserv "github.com/jbenet/go-ipfs/blockservice"
...@@ -48,7 +47,7 @@ type IpfsNode struct { ...@@ -48,7 +47,7 @@ type IpfsNode struct {
Peerstore peer.Peerstore Peerstore peer.Peerstore
// the local datastore // the local datastore
Datastore ds.ThreadSafeDatastoreCloser Datastore u.ThreadSafeDatastoreCloser
// the network message stream // the network message stream
Network inet.Network Network inet.Network
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) { func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
if len(cfg.Type) == 0 { if len(cfg.Type) == 0 {
return nil, fmt.Errorf("config datastore.type required") return nil, fmt.Errorf("config datastore.type required")
} }
...@@ -24,7 +24,7 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) { ...@@ -24,7 +24,7 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) {
return makeLevelDBDatastore(cfg) return makeLevelDBDatastore(cfg)
case "memory": case "memory":
return syncds.MutexWrap(ds.NewMapDatastore()), nil return u.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
case "fs": case "fs":
log.Warning("using fs.Datastore at .datastore for testing.") log.Warning("using fs.Datastore at .datastore for testing.")
...@@ -33,13 +33,13 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) { ...@@ -33,13 +33,13 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) {
return nil, err return nil, err
} }
ktd := ktds.Wrap(d, u.B58KeyConverter) ktd := ktds.Wrap(d, u.B58KeyConverter)
return syncds.MutexWrap(ktd), nil return u.CloserWrap(syncds.MutexWrap(ktd)), 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.ThreadSafeDatastoreCloser, error) { func makeLevelDBDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
if len(cfg.Path) == 0 { if len(cfg.Path) == 0 {
return nil, fmt.Errorf("config datastore.path required for leveldb") return nil, fmt.Errorf("config datastore.path required for leveldb")
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
path "github.com/jbenet/go-ipfs/path" path "github.com/jbenet/go-ipfs/path"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
mdht "github.com/jbenet/go-ipfs/routing/mock" mdht "github.com/jbenet/go-ipfs/routing/mock"
"github.com/jbenet/go-ipfs/util"
) )
// NewMockNode constructs an IpfsNode for use in tests. // NewMockNode constructs an IpfsNode for use in tests.
...@@ -35,7 +36,7 @@ func NewMockNode() (*IpfsNode, error) { ...@@ -35,7 +36,7 @@ func NewMockNode() (*IpfsNode, error) {
// Temp Datastore // Temp Datastore
dstore := ds.NewMapDatastore() dstore := ds.NewMapDatastore()
nd.Datastore = syncds.MutexWrap(dstore) nd.Datastore = util.CloserWrap(syncds.MutexWrap(dstore))
// Routing // Routing
dht := mdht.NewMockRouter(nd.Identity, nd.Datastore) dht := mdht.NewMockRouter(nd.Identity, nd.Datastore)
......
package util
import (
"io"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
)
type ThreadSafeDatastoreCloser interface {
datastore.ThreadSafeDatastore
io.Closer
}
func CloserWrap(ds datastore.ThreadSafeDatastore) ThreadSafeDatastoreCloser {
return &datastoreCloserWrapper{ds}
}
type datastoreCloserWrapper struct {
datastore.ThreadSafeDatastore
}
func (w *datastoreCloserWrapper) Close() error {
return nil // no-op
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment