Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-merkledag
Commits
73c413e3
Commit
73c413e3
authored
Apr 01, 2017
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
comments and optimize potential rebalances
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
55069e82
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
6 deletions
+61
-6
utils/diffenum.go
utils/diffenum.go
+20
-6
utils/diffenum_test.go
utils/diffenum_test.go
+41
-0
No files found.
utils/diffenum.go
View file @
73c413e3
...
...
@@ -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
}
utils/diffenum_test.go
View file @
73c413e3
...
...
@@ -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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment