mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 05:17:49 +08:00
feat(util) add datastore Closer Wrapper
@jbenet License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
parent
f26388e0e5
commit
bacf3ecc6a
@ -7,7 +7,6 @@ import (
|
||||
|
||||
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"
|
||||
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"
|
||||
|
||||
bserv "github.com/jbenet/go-ipfs/blockservice"
|
||||
@ -48,7 +47,7 @@ type IpfsNode struct {
|
||||
Peerstore peer.Peerstore
|
||||
|
||||
// the local datastore
|
||||
Datastore ds.ThreadSafeDatastoreCloser
|
||||
Datastore u.ThreadSafeDatastoreCloser
|
||||
|
||||
// the network message stream
|
||||
Network inet.Network
|
||||
|
||||
@ -14,7 +14,7 @@ import (
|
||||
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 {
|
||||
return nil, fmt.Errorf("config datastore.type required")
|
||||
}
|
||||
@ -24,7 +24,7 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) {
|
||||
return makeLevelDBDatastore(cfg)
|
||||
|
||||
case "memory":
|
||||
return syncds.MutexWrap(ds.NewMapDatastore()), nil
|
||||
return u.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
|
||||
|
||||
case "fs":
|
||||
log.Warning("using fs.Datastore at .datastore for testing.")
|
||||
@ -33,13 +33,13 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) {
|
||||
return nil, err
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
func makeLevelDBDatastore(cfg config.Datastore) (ds.ThreadSafeDatastoreCloser, error) {
|
||||
func makeLevelDBDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
|
||||
if len(cfg.Path) == 0 {
|
||||
return nil, fmt.Errorf("config datastore.path required for leveldb")
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
path "github.com/jbenet/go-ipfs/path"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
mdht "github.com/jbenet/go-ipfs/routing/mock"
|
||||
"github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
// NewMockNode constructs an IpfsNode for use in tests.
|
||||
@ -35,7 +36,7 @@ func NewMockNode() (*IpfsNode, error) {
|
||||
|
||||
// Temp Datastore
|
||||
dstore := ds.NewMapDatastore()
|
||||
nd.Datastore = syncds.MutexWrap(dstore)
|
||||
nd.Datastore = util.CloserWrap(syncds.MutexWrap(dstore))
|
||||
|
||||
// Routing
|
||||
dht := mdht.NewMockRouter(nd.Identity, nd.Datastore)
|
||||
|
||||
24
util/datastore_closer.go
Normal file
24
util/datastore_closer.go
Normal file
@ -0,0 +1,24 @@
|
||||
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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user