Skip to content

Commit

Permalink
fix: use ipfs datastore as root datastore for persistent service
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jun 27, 2023
1 parent c219763 commit 8b3c757
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"os"
"time"

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

Expand Down Expand Up @@ -85,14 +84,6 @@ 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)
}

repo, err := ipfsutil.LoadRepoFromPath(path)
if err != nil {
return nil, err
Expand All @@ -113,21 +104,20 @@ 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)
}

// use ipfs datastore as root datastore for wesh
opts.RootDatastore = namespace.Wrap(repo.Datastore(), datastore.NewKey("wesh"))
cl, err := NewServiceClient(opts)
if err != nil {
return nil, err
}

return &persistentServiceClient{
ServiceClient: cl,
ds: ds,
cleanup: cleanupLogger,
}, nil
}
Expand All @@ -144,22 +134,14 @@ 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()
}

return err
}

Expand Down

0 comments on commit 8b3c757

Please sign in to comment.