提交 6e4fd937 作者: Kevin Atkinson

Refactor EnumerateChildren to avoid need for bestEffort parameter.

License: MIT
Signed-off-by: 's avatarKevin Atkinson <k@kevina.org>
上级 57e4e210
...@@ -400,7 +400,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string ...@@ -400,7 +400,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string
if typeStr == "indirect" || typeStr == "all" { if typeStr == "indirect" || typeStr == "all" {
set := cid.NewSet() set := cid.NewSet()
for _, k := range n.Pinning.RecursiveKeys() { for _, k := range n.Pinning.RecursiveKeys() {
err := dag.EnumerateChildren(n.Context(), n.DAG, k, set.Visit, false) err := dag.EnumerateChildren(n.Context(), n.DAG.GetLinks, k, set.Visit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -163,7 +163,7 @@ func TestAddGCLive(t *testing.T) { ...@@ -163,7 +163,7 @@ func TestAddGCLive(t *testing.T) {
defer cancel() defer cancel()
set := cid.NewSet() set := cid.NewSet()
err = dag.EnumerateChildren(ctx, node.DAG, last, set.Visit, false) err = dag.EnumerateChildren(ctx, node.DAG.GetLinks, last, set.Visit)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -383,17 +383,16 @@ func (t *Batch) Commit() error { ...@@ -383,17 +383,16 @@ func (t *Batch) Commit() error {
// EnumerateChildren will walk the dag below the given root node and add all // EnumerateChildren will walk the dag below the given root node and add all
// unseen children to the passed in set. // unseen children to the passed in set.
// TODO: parallelize to avoid disk latency perf hits? // TODO: parallelize to avoid disk latency perf hits?
func EnumerateChildren(ctx context.Context, ds LinkService, root *cid.Cid, visit func(*cid.Cid) bool, bestEffort bool) error { type GetLinks func(context.Context, *cid.Cid) ([]*node.Link, error)
links, err := ds.GetLinks(ctx, root) func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, visit func(*cid.Cid) bool) error {
if bestEffort && err == ErrNotFound { links, err := getLinks(ctx, root)
return nil if err != nil {
} else if err != nil {
return err return err
} }
for _, lnk := range links { for _, lnk := range links {
c := lnk.Cid c := lnk.Cid
if visit(c) { if visit(c) {
err = EnumerateChildren(ctx, ds, c, visit, bestEffort) err = EnumerateChildren(ctx, getLinks, c, visit)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -249,7 +249,7 @@ func TestFetchGraph(t *testing.T) { ...@@ -249,7 +249,7 @@ func TestFetchGraph(t *testing.T) {
offline_ds := NewDAGService(bs) offline_ds := NewDAGService(bs)
err = EnumerateChildren(context.Background(), offline_ds, root.Cid(), func(_ *cid.Cid) bool { return true }, false) err = EnumerateChildren(context.Background(), offline_ds.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -266,7 +266,7 @@ func TestEnumerateChildren(t *testing.T) { ...@@ -266,7 +266,7 @@ func TestEnumerateChildren(t *testing.T) {
} }
set := cid.NewSet() set := cid.NewSet()
err = EnumerateChildren(context.Background(), ds, root.Cid(), set.Visit, false) err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
) )
var log = logging.Logger("gc") var log = logging.Logger("gc")
...@@ -68,12 +69,12 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin. ...@@ -68,12 +69,12 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
return output, nil return output, nil
} }
func Descendants(ctx context.Context, ls dag.LinkService, set *cid.Set, roots []*cid.Cid, bestEffort bool) error { func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots []*cid.Cid) error {
for _, c := range roots { for _, c := range roots {
set.Add(c) set.Add(c)
// EnumerateChildren recursively walks the dag and adds the keys to the given set // EnumerateChildren recursively walks the dag and adds the keys to the given set
err := dag.EnumerateChildren(ctx, ls, c, set.Visit, bestEffort) err := dag.EnumerateChildren(ctx, getLinks, c, set.Visit)
if err != nil { if err != nil {
return err return err
} }
...@@ -86,12 +87,19 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo ...@@ -86,12 +87,19 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
// KeySet currently implemented in memory, in the future, may be bloom filter or // KeySet currently implemented in memory, in the future, may be bloom filter or
// disk backed to conserve memory. // disk backed to conserve memory.
gcs := cid.NewSet() gcs := cid.NewSet()
err := Descendants(ctx, ls, gcs, pn.RecursiveKeys(), false) err := Descendants(ctx, ls.GetLinks, gcs, pn.RecursiveKeys())
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = Descendants(ctx, ls, gcs, bestEffortRoots, true) bestEffortGetLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
links, err := ls.GetLinks(ctx, cid)
if err == dag.ErrNotFound {
err = nil
}
return links, err
}
err = Descendants(ctx, bestEffortGetLinks, gcs, bestEffortRoots)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -100,7 +108,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo ...@@ -100,7 +108,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
gcs.Add(k) gcs.Add(k)
} }
err = Descendants(ctx, ls, gcs, pn.InternalPins(), false) err = Descendants(ctx, ls.GetLinks, gcs, pn.InternalPins())
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论