Skip to content

Commit 0588c1a

Browse files
authored
Merge pull request #92 from jefft0/feat/opts.RootDatastore-default
In NewService, optionally set opts.RootDatastore to a persistent data store
2 parents 89c06bb + 73658cf commit 0588c1a

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

service.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"unsafe"
1212

1313
pubsub_fix "github.com/berty/go-libp2p-pubsub"
14+
"github.com/dgraph-io/badger/v2/options"
1415
ds "github.com/ipfs/go-datastore"
1516
ds_sync "github.com/ipfs/go-datastore/sync"
17+
badger "github.com/ipfs/go-ds-badger2"
1618
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"
1719
pubsub "github.com/libp2p/go-libp2p-pubsub"
1820
"github.com/libp2p/go-libp2p/core/crypto"
@@ -101,28 +103,47 @@ type Opts struct {
101103
GroupMessageStoreType string
102104
}
103105

104-
func (opts *Opts) applyPushDefaults() error {
106+
func (opts *Opts) applyPushDefaults() {
105107
if opts.Logger == nil {
106108
opts.Logger = zap.NewNop()
107109
}
108110

109111
if opts.PrometheusRegister == nil {
110112
opts.PrometheusRegister = prometheus.DefaultRegisterer
111113
}
112-
113-
opts.applyDefaultsGetDatastore()
114-
115-
return nil
116114
}
117115

118-
func (opts *Opts) applyDefaultsGetDatastore() {
116+
func (opts *Opts) applyDefaultsGetDatastore() error {
119117
if opts.RootDatastore == nil {
120118
if opts.DatastoreDir == "" || opts.DatastoreDir == InMemoryDirectory {
121119
opts.RootDatastore = ds_sync.MutexWrap(ds.NewMapDatastore())
122120
} else {
123-
opts.RootDatastore = nil
121+
bopts := badger.DefaultOptions
122+
bopts.ValueLogLoadingMode = options.FileIO
123+
124+
ds, err := badger.NewDatastore(opts.DatastoreDir, &bopts)
125+
if err != nil {
126+
return fmt.Errorf("unable to init badger datastore: %w", err)
127+
}
128+
opts.RootDatastore = ds
129+
130+
oldClose := opts.close
131+
opts.close = func() error {
132+
var err error
133+
if oldClose != nil {
134+
err = oldClose()
135+
}
136+
137+
if dserr := ds.Close(); dserr != nil {
138+
err = multierr.Append(err, fmt.Errorf("unable to close datastore: %w", dserr))
139+
}
140+
141+
return err
142+
}
124143
}
125144
}
145+
146+
return nil
126147
}
127148

128149
func (opts *Opts) applyDefaults(ctx context.Context) error {
@@ -132,12 +153,12 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
132153

133154
rng := mrand.New(mrand.NewSource(srand.MustSecure())) // nolint:gosec // we need to use math/rand here, but it is seeded from crypto/rand
134155

135-
opts.applyDefaultsGetDatastore()
136-
137-
if err := opts.applyPushDefaults(); err != nil {
156+
if err := opts.applyDefaultsGetDatastore(); err != nil {
138157
return err
139158
}
140159

160+
opts.applyPushDefaults()
161+
141162
if opts.SecretStore == nil {
142163
secretStore, err := secretstore.NewSecretStore(opts.RootDatastore, &secretstore.NewSecretStoreOptions{
143164
Logger: opts.Logger,
@@ -287,7 +308,10 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
287308
return nil
288309
}
289310

290-
// NewService initializes a new Service
311+
// NewService initializes a new Service using the opts.
312+
// If opts.RootDatastore is nil and opts.DatastoreDir is "" or InMemoryDirectory, then set
313+
// opts.RootDatastore to an in-memory data store. Otherwise, if opts.RootDatastore is nil then set
314+
// opts.RootDatastore to a persistent data store at opts.DatastoreDir .
291315
func NewService(opts Opts) (_ Service, err error) {
292316
ctx, cancel := context.WithCancel(context.Background())
293317

service_client.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"os"
88
"time"
99

10-
"github.com/dgraph-io/badger/v2/options"
11-
"github.com/ipfs/go-datastore"
12-
badger "github.com/ipfs/go-ds-badger2"
1310
"go.uber.org/zap"
1411
"google.golang.org/grpc"
1512

@@ -31,6 +28,10 @@ type ServiceClient interface {
3128
io.Closer
3229
}
3330

31+
// NewServiceClient initializes a new ServiceClient using the opts.
32+
// If opts.RootDatastore is nil and opts.DatastoreDir is "" or InMemoryDirectory, then set
33+
// opts.RootDatastore to an in-memory data store. Otherwise, if opts.RootDatastore is nil then set
34+
// opts.RootDatastore to a persistent data store at opts.DatastoreDir .
3435
func NewServiceClient(opts Opts) (ServiceClient, error) {
3536
var err error
3637

@@ -85,13 +86,7 @@ func NewInMemoryServiceClient() (ServiceClient, error) {
8586
func NewPersistentServiceClient(path string) (ServiceClient, error) {
8687
var opts Opts
8788

88-
bopts := badger.DefaultOptions
89-
bopts.ValueLogLoadingMode = options.FileIO
90-
91-
ds, err := badger.NewDatastore(path, &bopts)
92-
if err != nil {
93-
return nil, fmt.Errorf("unable to init badger datastore: %w", err)
94-
}
89+
opts.DatastoreDir = path
9590

9691
repo, err := ipfsutil.LoadRepoFromPath(path)
9792
if err != nil {
@@ -109,8 +104,6 @@ func NewPersistentServiceClient(path string) (ServiceClient, error) {
109104
return nil, err
110105
}
111106

112-
opts.RootDatastore = ds
113-
114107
var cleanupLogger func()
115108
if opts.Logger, cleanupLogger, err = setupDefaultLogger(); err != nil {
116109
return nil, fmt.Errorf("uanble to setup logger: %w", err)
@@ -123,7 +116,6 @@ func NewPersistentServiceClient(path string) (ServiceClient, error) {
123116

124117
return &persistentServiceClient{
125118
ServiceClient: cl,
126-
ds: ds,
127119
cleanup: cleanupLogger,
128120
}, nil
129121
}
@@ -140,18 +132,12 @@ type serviceClient struct {
140132

141133
type persistentServiceClient struct {
142134
ServiceClient
143-
ds datastore.Batching
144135
cleanup func()
145136
}
146137

147138
func (p *persistentServiceClient) Close() error {
148139
err := p.ServiceClient.Close()
149140

150-
if dserr := p.ds.Close(); err == nil && dserr != nil {
151-
// only return ds error if no error have been catch earlier
152-
err = fmt.Errorf("unable to close datastore: %w", dserr)
153-
}
154-
155141
if p.cleanup != nil {
156142
p.cleanup()
157143
}

0 commit comments

Comments
 (0)