@@ -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
128149func (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 .
291315func NewService (opts Opts ) (_ Service , err error ) {
292316 ctx , cancel := context .WithCancel (context .Background ())
293317
0 commit comments