Skip to content

Commit

Permalink
comments and optimize potential rebalances
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed May 18, 2017
1 parent 3158496 commit 8f97786
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
26 changes: 20 additions & 6 deletions merkledag/utils/diffenum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
41 changes: 41 additions & 0 deletions merkledag/utils/diffenum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}

0 comments on commit 8f97786

Please sign in to comment.