mirror of
https://github.com/ipfs/kubo.git
synced 2025-12-19 01:30:17 +08:00
This isn't perfect (we only use sessions after resolving the root cid) but it's better than what we have. The real solution is #7198 so we can use sessions everywhere.
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package coreapi
|
|
|
|
import (
|
|
"context"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
"github.com/ipfs/go-ipfs-pinner"
|
|
ipld "github.com/ipfs/go-ipld-format"
|
|
dag "github.com/ipfs/go-merkledag"
|
|
)
|
|
|
|
type dagAPI struct {
|
|
ipld.DAGService
|
|
|
|
core *CoreAPI
|
|
}
|
|
|
|
type pinningAdder CoreAPI
|
|
|
|
func (adder *pinningAdder) Add(ctx context.Context, nd ipld.Node) error {
|
|
defer adder.blockstore.PinLock().Unlock()
|
|
|
|
if err := adder.dag.Add(ctx, nd); err != nil {
|
|
return err
|
|
}
|
|
|
|
adder.pinning.PinWithMode(nd.Cid(), pin.Recursive)
|
|
|
|
return adder.pinning.Flush(ctx)
|
|
}
|
|
|
|
func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
|
|
defer adder.blockstore.PinLock().Unlock()
|
|
|
|
if err := adder.dag.AddMany(ctx, nds); err != nil {
|
|
return err
|
|
}
|
|
|
|
cids := cid.NewSet()
|
|
|
|
for _, nd := range nds {
|
|
c := nd.Cid()
|
|
if cids.Visit(c) {
|
|
adder.pinning.PinWithMode(c, pin.Recursive)
|
|
}
|
|
}
|
|
|
|
return adder.pinning.Flush(ctx)
|
|
}
|
|
|
|
func (api *dagAPI) Pinning() ipld.NodeAdder {
|
|
return (*pinningAdder)(api.core)
|
|
}
|
|
|
|
func (api *dagAPI) Session(ctx context.Context) ipld.NodeGetter {
|
|
return dag.NewSession(ctx, api.DAGService)
|
|
}
|
|
|
|
var (
|
|
_ ipld.DAGService = (*dagAPI)(nil)
|
|
_ dag.SessionMaker = (*dagAPI)(nil)
|
|
)
|