Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

optimize CheckIfPinned #6

Merged
merged 1 commit into from
Feb 18, 2021
Merged
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
48 changes: 15 additions & 33 deletions dspinner/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ipfs/go-ipfs-pinner/dsindex"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-merkledag"
mdag "github.com/ipfs/go-merkledag"
"github.com/ipfs/go-merkledag/dagutils"
"github.com/polydawn/refmt/cbor"
Expand Down Expand Up @@ -489,49 +490,30 @@ func (p *pinner) CheckIfPinned(ctx context.Context, cids ...cid.Cid) ([]ipfspinn
}
}

// Now walk all recursive pins to check for indirect pins
var checkChildren func(cid.Cid, cid.Cid) error
checkChildren = func(rk, parentKey cid.Cid) error {
links, err := ipld.GetLinks(ctx, p.dserv, parentKey)
if err != nil {
return err
}
for _, lnk := range links {
c := lnk.Cid

if toCheck.Has(c) {
pinned = append(pinned,
ipfspinner.Pinned{Key: c, Mode: ipfspinner.Indirect, Via: rk})
toCheck.Remove(c)
}

err = checkChildren(rk, c)
if err != nil {
return err
}

if toCheck.Len() == 0 {
return nil
}
}
return nil
}

var e error
visited := cid.NewSet()
err := p.cidRIndex.ForEach(ctx, "", func(key, value string) bool {
var rk cid.Cid
rk, e = cid.Cast([]byte(key))
if e != nil {
return false
}
e = checkChildren(rk, rk)
e = merkledag.Walk(ctx, merkledag.GetLinksWithDAG(p.dserv), rk, func(c cid.Cid) bool {
if toCheck.Len() == 0 || !visited.Visit(c) {
return false
}

if toCheck.Has(c) {
pinned = append(pinned, ipfspinner.Pinned{Key: c, Mode: ipfspinner.Indirect, Via: rk})
toCheck.Remove(c)
}

return true
}, merkledag.Concurrent())
if e != nil {
return false
}
if toCheck.Len() == 0 {
return false
}
return true
return toCheck.Len() > 0
})
if err != nil {
return nil, err
Expand Down
34 changes: 10 additions & 24 deletions ipldpinner/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ds "github.com/ipfs/go-datastore"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-merkledag"
mdag "github.com/ipfs/go-merkledag"
"github.com/ipfs/go-merkledag/dagutils"

Expand Down Expand Up @@ -328,35 +329,20 @@ func (p *pinner) CheckIfPinned(ctx context.Context, cids ...cid.Cid) ([]ipfspinn
}

// Now walk all recursive pins to check for indirect pins
var checkChildren func(cid.Cid, cid.Cid) error
checkChildren = func(rk, parentKey cid.Cid) error {
links, err := ipld.GetLinks(ctx, p.dserv, parentKey)
if err != nil {
return err
}
for _, lnk := range links {
c := lnk.Cid
visited := cid.NewSet()
for _, rk := range p.recursePin.Keys() {
err := merkledag.Walk(ctx, merkledag.GetLinksWithDAG(p.dserv), rk, func(c cid.Cid) bool {
if toCheck.Len() == 0 || !visited.Visit(c) {
return false
}

if toCheck.Has(c) {
pinned = append(pinned,
ipfspinner.Pinned{Key: c, Mode: ipfspinner.Indirect, Via: rk})
pinned = append(pinned, ipfspinner.Pinned{Key: c, Mode: ipfspinner.Indirect, Via: rk})
toCheck.Remove(c)
}

err := checkChildren(rk, c)
if err != nil {
return err
}

if toCheck.Len() == 0 {
return nil
}
}
return nil
}

for _, rk := range p.recursePin.Keys() {
err := checkChildren(rk, rk)
return true
}, merkledag.Concurrent())
if err != nil {
return nil, err
}
Expand Down