Skip to content

Commit

Permalink
fix(diff): modify diff logic and comment
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Sep 23, 2018
1 parent 7cda005 commit df706f4
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions dagutils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,23 @@ func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs
}

// Diff returns a set of changes that transform node 'a' into node 'b'.
// It supports two nodes forms: ProtoNode and RawNode. Because we treat
// the nodes as IPLD nodes as long as possible and only convert them
// to ProtoNode when necessary: when we need to remove links, and at that point
// (if they have links to remove) we know they are not raw nodes.
// It only traverses links in the following cases:
// 1. two node's links number are greater than 0.
// 2. both of two nodes are ProtoNode.
// Otherwise, it compares the cid and emits a Mod change object.
func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, error) {
// Base case where both nodes are leaves, just compare
// their CIDs.
if len(a.Links()) == 0 && len(b.Links()) == 0 {
if a.Cid().Equals(b.Cid()) {
return []*Change{}, nil
}
return []*Change{
{
Type: Mod,
Before: a.Cid(),
After: b.Cid(),
},
}, nil
return getChange(a, b)
}

var out []*Change
cleanA := a.Copy()
cleanB := b.Copy()
cleanA, okA := a.Copy().(*dag.ProtoNode)
cleanB, okB := b.Copy().(*dag.ProtoNode)
if !okA || !okB {
return getChange(a, b)
}

// strip out unchanged stuff
for _, lnk := range a.Links() {
Expand Down Expand Up @@ -146,12 +140,8 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
out = append(out, subc)
}
}
if cleanA, ok := cleanA.(*dag.ProtoNode); ok {
cleanA.RemoveNodeLink(l.Name)
}
if cleanB, ok := cleanB.(*dag.ProtoNode); ok {
cleanB.RemoveNodeLink(l.Name)
}
cleanA.RemoveNodeLink(l.Name)
cleanB.RemoveNodeLink(l.Name)
}
}

Expand Down Expand Up @@ -207,3 +197,16 @@ func MergeDiffs(a, b []*Change) ([]*Change, []Conflict) {
}
return out, conflicts
}

func getChange(a, b ipld.Node) ([]*Change, error) {
if a.Cid().Equals(b.Cid()) {
return []*Change{}, nil
}
return []*Change{
{
Type: Mod,
Before: a.Cid(),
After: b.Cid(),
},
}, nil
}

0 comments on commit df706f4

Please sign in to comment.