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

builder: add blockstoreOption for customizing the blockstore #3349

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 30 additions & 24 deletions core/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
Expand All @@ -17,7 +18,6 @@ import (
repo "github.com/ipfs/go-ipfs/repo"
cfg "github.com/ipfs/go-ipfs/repo/config"

context "context"
retry "gx/ipfs/QmPF5kxTYFkzhaY5LmkExood7aTTZBHWQC6cjdDQBuGrjp/retry-datastore"
metrics "gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
goprocessctx "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess/context"
Expand All @@ -42,9 +42,10 @@ type BuildCfg struct {
// If NilRepo is set, a repo backed by a nil datastore will be constructed
NilRepo bool

Routing RoutingOption
Host HostOption
Repo repo.Repo
Blockstore BlockstoreOption
Routing RoutingOption
Host HostOption
Repo repo.Repo
}

func (cfg *BuildCfg) getOpt(key string) bool {
Expand All @@ -60,6 +61,10 @@ func (cfg *BuildCfg) fillDefaults() error {
return errors.New("cannot set a repo and specify nilrepo at the same time")
}

if cfg.Blockstore == nil {
cfg.Blockstore = SetupBlockstore
}

if cfg.Repo == nil {
var d ds.Datastore
d = ds.NewMapDatastore()
Expand Down Expand Up @@ -153,6 +158,24 @@ func isTooManyFDError(err error) bool {
return false
}

type BlockstoreOption func(ctx context.Context, d ds.Batching, rcfg *cfg.Config, permanent bool) (bstore.Blockstore, error)

func SetupBlockstore(ctx context.Context, d ds.Batching, rcfg *cfg.Config, permanent bool) (bstore.Blockstore, error) {
bs := bstore.NewBlockstore(d)
opts := bstore.DefaultCacheOpts()

opts.HasBloomFilterSize = rcfg.Datastore.BloomFilterSize
if !permanent {
opts.HasBloomFilterSize = 0
}

if rcfg.Datastore.HashOnRead {
bs.HashOnRead(true)
}

return bstore.CachedBlockstore(bs, ctx, opts)
}

func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
// setup local peer ID (private key is loaded in online setup)
if err := n.loadID(); err != nil {
Expand All @@ -166,34 +189,17 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
TempErrFunc: isTooManyFDError,
}

var err error
bs := bstore.NewBlockstore(rds)
opts := bstore.DefaultCacheOpts()
conf, err := n.Repo.Config()
if err != nil {
return err
}

opts.HasBloomFilterSize = conf.Datastore.BloomFilterSize
if !cfg.Permament {
opts.HasBloomFilterSize = 0
}

cbs, err := bstore.CachedBlockstore(bs, ctx, opts)
rcfg, err := n.Repo.Config()
if err != nil {
return err
}

n.Blockstore = bstore.NewGCBlockstore(cbs, bstore.NewGCLocker())

rcfg, err := n.Repo.Config()
bs, err := cfg.Blockstore(ctx, rds, rcfg, cfg.Permament)
if err != nil {
return err
}

if rcfg.Datastore.HashOnRead {
bs.HashOnRead(true)
}
n.Blockstore = bstore.NewGCBlockstore(bs, bstore.NewGCLocker())

if cfg.Online {
do := setupDiscoveryOption(rcfg.Discovery)
Expand Down