Skip to content

Commit

Permalink
feat: In NewService, optionally create a persistent data store at opt…
Browse files Browse the repository at this point in the history
…s.DatastoreDir. Use this in NewServiceClient.

Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Aug 1, 2023
1 parent 2d0ab23 commit 73658cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
30 changes: 28 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"unsafe"

pubsub_fix "github.com/berty/go-libp2p-pubsub"
"github.com/dgraph-io/badger/v2/options"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
badger "github.com/ipfs/go-ds-badger2"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -116,7 +118,28 @@ func (opts *Opts) applyDefaultsGetDatastore() error {
if opts.DatastoreDir == "" || opts.DatastoreDir == InMemoryDirectory {
opts.RootDatastore = ds_sync.MutexWrap(ds.NewMapDatastore())
} else {
opts.RootDatastore = nil
bopts := badger.DefaultOptions
bopts.ValueLogLoadingMode = options.FileIO

ds, err := badger.NewDatastore(opts.DatastoreDir, &bopts)
if err != nil {
return fmt.Errorf("unable to init badger datastore: %w", err)
}
opts.RootDatastore = ds

oldClose := opts.close
opts.close = func() error {
var err error
if oldClose != nil {
err = oldClose()
}

if dserr := ds.Close(); dserr != nil {
err = multierr.Append(err, fmt.Errorf("unable to close datastore: %w", dserr))
}

return err
}
}
}

Expand Down Expand Up @@ -285,7 +308,10 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
return nil
}

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

Expand Down
24 changes: 5 additions & 19 deletions service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"os"
"time"

"github.com/dgraph-io/badger/v2/options"
"github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger2"
"go.uber.org/zap"
"google.golang.org/grpc"

Expand All @@ -31,6 +28,10 @@ type ServiceClient interface {
io.Closer
}

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

Expand Down Expand Up @@ -85,13 +86,7 @@ func NewInMemoryServiceClient() (ServiceClient, error) {
func NewPersistentServiceClient(path string) (ServiceClient, error) {
var opts Opts

bopts := badger.DefaultOptions
bopts.ValueLogLoadingMode = options.FileIO

ds, err := badger.NewDatastore(path, &bopts)
if err != nil {
return nil, fmt.Errorf("unable to init badger datastore: %w", err)
}
opts.DatastoreDir = path

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

opts.RootDatastore = ds

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

return &persistentServiceClient{
ServiceClient: cl,
ds: ds,
cleanup: cleanupLogger,
}, nil
}
Expand All @@ -140,18 +132,12 @@ type serviceClient struct {

type persistentServiceClient struct {
ServiceClient
ds datastore.Batching
cleanup func()
}

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

if dserr := p.ds.Close(); err == nil && dserr != nil {
// only return ds error if no error have been catch earlier
err = fmt.Errorf("unable to close datastore: %w", dserr)
}

if p.cleanup != nil {
p.cleanup()
}
Expand Down

0 comments on commit 73658cf

Please sign in to comment.