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 <jeff@thefirst.org>
  • Loading branch information
jefft0 committed Aug 1, 2023
1 parent 42862c5 commit 7482de4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
33 changes: 32 additions & 1 deletion 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 @@ -129,6 +131,32 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
opts.applyDefaultsGetDatastore()
opts.applyPushDefaults()

if opts.RootDatastore == nil && opts.DatastoreDir != "" {
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(); err == nil && dserr != nil {
// only return ds error if no error has been caught earlier
err = fmt.Errorf("unable to close datastore: %w", dserr)
}

return err
}
}

if opts.SecretStore == nil {
secretStore, err := secretstore.NewSecretStore(opts.RootDatastore, &secretstore.NewSecretStoreOptions{
Logger: opts.Logger,
Expand Down Expand Up @@ -278,7 +306,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 7482de4

Please sign in to comment.