Commit 73c413e3 authored by Jeromy's avatar Jeromy

comments and optimize potential rebalances

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 55069e82
......@@ -28,13 +28,24 @@ func DiffEnumerate(ctx context.Context, dserv node.NodeGetter, from, to *cid.Cid
sset := cid.NewSet()
for _, c := range diff {
if c.a == nil {
err := mdag.EnumerateChildrenAsync(ctx, mdag.GetLinksDirect(dserv), c.b, sset.Visit)
// Since we're already assuming we have everything in the 'from' graph,
// add all those cids to our 'already seen' set to avoid potentially
// enumerating them later
if c.bef != nil {
sset.Add(c.bef)
}
}
for _, c := range diff {
if c.bef == nil {
if sset.Has(c.aft) {
continue
}
err := mdag.EnumerateChildrenAsync(ctx, mdag.GetLinksDirect(dserv), c.aft, sset.Visit)
if err != nil {
return err
}
} else {
err := DiffEnumerate(ctx, dserv, c.a, c.b)
err := DiffEnumerate(ctx, dserv, c.bef, c.aft)
if err != nil {
return err
}
......@@ -45,9 +56,12 @@ func DiffEnumerate(ctx context.Context, dserv node.NodeGetter, from, to *cid.Cid
}
type diffpair struct {
a, b *cid.Cid
bef, aft *cid.Cid
}
// getLinkDiff returns a changset (minimum edit distance style) between nodes
// 'a' and 'b'. Currently does not log deletions as our usecase doesnt call for
// this.
func getLinkDiff(a, b node.Node) []diffpair {
have := make(map[string]*node.Link)
names := make(map[string]*node.Link)
......@@ -65,11 +79,11 @@ func getLinkDiff(a, b node.Node) []diffpair {
match, ok := names[l.Name]
if !ok {
out = append(out, diffpair{b: l.Cid})
out = append(out, diffpair{aft: l.Cid})
continue
}
out = append(out, diffpair{a: match.Cid, b: l.Cid})
out = append(out, diffpair{bef: match.Cid, aft: l.Cid})
}
return out
}
......@@ -70,6 +70,20 @@ var tg2 = map[string]ndesc{
"d": ndesc{},
}
var tg3 = map[string]ndesc{
"a1": ndesc{
"foo": "b",
"bar": "c",
},
"b": ndesc{},
"a2": ndesc{
"foo": "b",
"bar": "d",
},
"c": ndesc{},
"d": ndesc{},
}
func TestDiffEnumBasic(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
......@@ -121,6 +135,7 @@ func assertCidList(a, b []*cid.Cid) error {
}
return nil
}
func TestDiffEnumFail(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
......@@ -147,3 +162,29 @@ func TestDiffEnumFail(t *testing.T) {
}
}
func TestDiffEnumRecurse(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nds := mkGraph(tg3)
ds := mdtest.Mock()
lgds := &getLogger{ds: ds}
for _, s := range []string{"a1", "a2", "b", "c", "d"} {
_, err := ds.Add(nds[s])
if err != nil {
t.Fatal(err)
}
}
err := DiffEnumerate(ctx, lgds, nds["a1"].Cid(), nds["a2"].Cid())
if err != nil {
t.Fatal(err)
}
err = assertCidList(lgds.log, []*cid.Cid{nds["a1"].Cid(), nds["a2"].Cid(), nds["c"].Cid(), nds["d"].Cid()})
if err != nil {
t.Fatal(err)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment