Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ipfs datastore as root datastore for persistent service #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/berty/go-libp2p-rendezvous v0.5.0
github.com/buicongtan1997/protoc-gen-swagger-config v0.0.0-20200705084907-1342b78c1a7e
github.com/daixiang0/gci v0.8.2
github.com/dgraph-io/badger/v2 v2.2007.3
github.com/gofrs/uuid v4.3.1+incompatible
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.3
Expand All @@ -22,7 +21,6 @@ require (
github.com/hyperledger/aries-framework-go v0.1.9-0.20221202141134-083803ecf0a3
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger2 v0.1.3
github.com/ipfs/go-ipfs-keystore v0.1.0
github.com/ipfs/go-ipld-cbor v0.0.6
github.com/ipfs/go-log/v2 v2.5.1
Expand Down Expand Up @@ -72,7 +70,6 @@ require (
cloud.google.com/go/compute/metadata v0.2.1 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/DataDog/zstd v1.4.1 // indirect
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
Expand All @@ -99,7 +96,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/dgraph-io/badger v1.6.2 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
Expand Down
9 changes: 0 additions & 9 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"sync/atomic"
"time"

"github.com/ipfs/go-datastore"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
ds_sync "github.com/ipfs/go-datastore/sync"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"
pubsub "github.com/libp2p/go-libp2p-pubsub"
Expand Down Expand Up @@ -128,6 +130,8 @@ func (opts *Opts) applyDefaultsGetDatastore() {
opts.RootDatastore = nil
}
}

opts.RootDatastore = namespace.Wrap(opts.RootDatastore, datastore.NewKey("wesh"))
}

func (opts *Opts) applyDefaults(ctx context.Context) error {
Expand Down
33 changes: 11 additions & 22 deletions service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/dgraph-io/badger/v2/options"
"github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger2"
leveldb "github.com/ipfs/go-ds-leveldb"
"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,27 @@ 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)
}

dspath := filepath.Join(path, "weshds")
ds, err := leveldb.NewDatastore(dspath, &leveldb.Options{})
if err != nil {
_ = mnode.Close()
return nil, fmt.Errorf("unable to open leveldb datastore: %w", err)
}

// use ipfs datastore as root datastore for wesh
opts.RootDatastore = ds
cl, err := NewServiceClient(opts)
if err != nil {
return nil, err
}

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