mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
coreapi/unixfs: don't create an additional IpfsNode for --only-hash
This commit is contained in:
parent
9a5f5e7352
commit
a3483e352e
@ -3,14 +3,6 @@ package coreapi
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/ipfs/kubo/core"
|
||||
"github.com/ipfs/kubo/tracing"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"github.com/ipfs/kubo/core/coreunix"
|
||||
|
||||
blockservice "github.com/ipfs/boxo/blockservice"
|
||||
bstore "github.com/ipfs/boxo/blockstore"
|
||||
@ -21,41 +13,23 @@ import (
|
||||
ft "github.com/ipfs/boxo/ipld/unixfs"
|
||||
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
|
||||
uio "github.com/ipfs/boxo/ipld/unixfs/io"
|
||||
mfs "github.com/ipfs/boxo/mfs"
|
||||
"github.com/ipfs/boxo/mfs"
|
||||
"github.com/ipfs/boxo/path"
|
||||
cid "github.com/ipfs/go-cid"
|
||||
cidutil "github.com/ipfs/go-cidutil"
|
||||
ds "github.com/ipfs/go-datastore"
|
||||
dssync "github.com/ipfs/go-datastore/sync"
|
||||
ipld "github.com/ipfs/go-ipld-format"
|
||||
coreiface "github.com/ipfs/kubo/core/coreiface"
|
||||
options "github.com/ipfs/kubo/core/coreiface/options"
|
||||
"github.com/ipfs/kubo/core/coreunix"
|
||||
"github.com/ipfs/kubo/tracing"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type UnixfsAPI CoreAPI
|
||||
|
||||
var (
|
||||
nilNode *core.IpfsNode
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func getOrCreateNilNode() (*core.IpfsNode, error) {
|
||||
once.Do(func() {
|
||||
if nilNode != nil {
|
||||
return
|
||||
}
|
||||
node, err := core.NewNode(context.Background(), &core.BuildCfg{
|
||||
// TODO: need this to be true or all files
|
||||
// hashed will be stored in memory!
|
||||
NilRepo: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
nilNode = node
|
||||
})
|
||||
|
||||
return nilNode, nil
|
||||
}
|
||||
|
||||
// Add builds a merkledag node from a reader, adds it to the blockstore,
|
||||
// and returns the key representing that node.
|
||||
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.ImmutablePath, error) {
|
||||
@ -108,13 +82,12 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
|
||||
pinning := api.pinning
|
||||
|
||||
if settings.OnlyHash {
|
||||
node, err := getOrCreateNilNode()
|
||||
if err != nil {
|
||||
return path.ImmutablePath{}, err
|
||||
}
|
||||
addblockstore = node.Blockstore
|
||||
exch = node.Exchange
|
||||
pinning = node.Pinning
|
||||
// setup a /dev/null pipeline to simulate adding the data
|
||||
dstore := dssync.MutexWrap(ds.NewNullDatastore())
|
||||
bs := bstore.NewBlockstore(dstore, bstore.WriteThrough())
|
||||
addblockstore = bstore.NewGCBlockstore(bs, nil) // gclocker will never be used
|
||||
exch = nil // exchange will never be used
|
||||
pinning = nil // pinner will never be used
|
||||
}
|
||||
|
||||
bserv := blockservice.New(addblockstore, exch) // hash security 001
|
||||
@ -133,11 +106,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
|
||||
syncDserv = &syncDagService{
|
||||
DAGService: dserv,
|
||||
syncFn: func() error {
|
||||
ds := api.repo.Datastore()
|
||||
if err := ds.Sync(ctx, bstore.BlockPrefix); err != nil {
|
||||
rds := api.repo.Datastore()
|
||||
if err := rds.Sync(ctx, bstore.BlockPrefix); err != nil {
|
||||
return err
|
||||
}
|
||||
return ds.Sync(ctx, filestore.FilestorePrefix)
|
||||
return rds.Sync(ctx, filestore.FilestorePrefix)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
|
||||
"go.uber.org/fx"
|
||||
|
||||
@ -34,9 +33,6 @@ type BuildCfg struct {
|
||||
// DO NOT SET THIS UNLESS YOU'RE TESTING.
|
||||
DisableEncryptedConnections bool
|
||||
|
||||
// If NilRepo is set, a Repo backed by a nil datastore will be constructed
|
||||
NilRepo bool
|
||||
|
||||
Routing libp2p.RoutingOption
|
||||
Host libp2p.HostOption
|
||||
Repo repo.Repo
|
||||
@ -51,18 +47,8 @@ func (cfg *BuildCfg) getOpt(key string) bool {
|
||||
}
|
||||
|
||||
func (cfg *BuildCfg) fillDefaults() error {
|
||||
if cfg.Repo != nil && cfg.NilRepo {
|
||||
return errors.New("cannot set a Repo and specify nilrepo at the same time")
|
||||
}
|
||||
|
||||
if cfg.Repo == nil {
|
||||
var d ds.Datastore
|
||||
if cfg.NilRepo {
|
||||
d = ds.NewNullDatastore()
|
||||
} else {
|
||||
d = ds.NewMapDatastore()
|
||||
}
|
||||
r, err := defaultRepo(dsync.MutexWrap(d))
|
||||
r, err := defaultRepo(dsync.MutexWrap(ds.NewMapDatastore()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -178,7 +178,7 @@ func Storage(bcfg *BuildCfg, cfg *config.Config) fx.Option {
|
||||
return fx.Options(
|
||||
fx.Provide(RepoConfig),
|
||||
fx.Provide(Datastore),
|
||||
fx.Provide(BaseBlockstoreCtor(cacheOpts, bcfg.NilRepo, cfg.Datastore.HashOnRead)),
|
||||
fx.Provide(BaseBlockstoreCtor(cacheOpts, cfg.Datastore.HashOnRead)),
|
||||
finalBstore,
|
||||
)
|
||||
}
|
||||
|
||||
@ -27,17 +27,14 @@ func Datastore(repo repo.Repo) datastore.Datastore {
|
||||
type BaseBlocks blockstore.Blockstore
|
||||
|
||||
// BaseBlockstoreCtor creates cached blockstore backed by the provided datastore
|
||||
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, nilRepo bool, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
|
||||
func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, hashOnRead bool) func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
|
||||
return func(mctx helpers.MetricsCtx, repo repo.Repo, lc fx.Lifecycle) (bs BaseBlocks, err error) {
|
||||
// hash security
|
||||
bs = blockstore.NewBlockstore(repo.Datastore())
|
||||
bs = &verifbs.VerifBS{Blockstore: bs}
|
||||
|
||||
if !nilRepo {
|
||||
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bs, err = blockstore.CachedBlockstore(helpers.LifecycleCtx(mctx, lc), bs, cacheOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bs = blockstore.NewIdStore(bs)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user