Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nicolas Dandrimont
swh-model
Commits
153c6e84
Commit
153c6e84
authored
3 years ago
by
Daniele Serafini
Browse files
Options
Downloads
Patches
Plain Diff
make deduplication optional when iterating over the merkle tree
parent
cfb3073c
No related branches found
Branches containing commit
Tags
v2.6.4
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/model/merkle.py
+9
-7
9 additions, 7 deletions
swh/model/merkle.py
swh/model/tests/test_merkle.py
+9
-1
9 additions, 1 deletion
swh/model/tests/test_merkle.py
with
18 additions
and
8 deletions
swh/model/merkle.py
+
9
−
7
View file @
153c6e84
...
...
@@ -277,18 +277,20 @@ class MerkleNode(dict, metaclass=abc.ABCMeta):
for
child
in
self
.
values
():
child
.
reset_collect
()
def
iter_tree
(
self
)
->
Iterator
[
"
MerkleNode
"
]:
"""
Yields all children nodes, recursively. Common nodes are
deduplicated.
def
iter_tree
(
self
,
dedup
=
True
)
->
Iterator
[
"
MerkleNode
"
]:
"""
Yields all children nodes, recursively. Common nodes are deduplicated
by default (deduplication can be turned off setting the given argument
'
dedup
'
to False).
"""
yield
from
self
.
_iter_tree
(
set
())
yield
from
self
.
_iter_tree
(
set
()
,
dedup
)
def
_iter_tree
(
self
,
seen
:
Set
[
bytes
])
->
Iterator
[
"
MerkleNode
"
]:
def
_iter_tree
(
self
,
seen
:
Set
[
bytes
]
,
dedup
)
->
Iterator
[
"
MerkleNode
"
]:
if
self
.
hash
not
in
seen
:
seen
.
add
(
self
.
hash
)
if
dedup
:
seen
.
add
(
self
.
hash
)
yield
self
for
child
in
self
.
values
():
yield
from
child
.
_iter_tree
(
seen
=
seen
)
yield
from
child
.
_iter_tree
(
seen
=
seen
,
dedup
=
dedup
)
class
MerkleLeaf
(
MerkleNode
):
...
...
This diff is collapsed.
Click to expand it.
swh/model/tests/test_merkle.py
+
9
−
1
View file @
153c6e84
...
...
@@ -172,10 +172,18 @@ class TestMerkleNode(unittest.TestCase):
collected2
=
self
.
root
.
collect
()
self
.
assertEqual
(
collected2
,
{})
def
test_iter_tree
(
self
):
def
test_iter_tree
_with_deduplication
(
self
):
nodes
=
list
(
self
.
root
.
iter_tree
())
self
.
assertCountEqual
(
nodes
,
self
.
nodes
.
values
())
def
test_iter_tree_without_deduplication
(
self
):
# duplicate existing hash in merkle tree
self
.
root
[
b
"
d
"
]
=
MerkleTestNode
({
"
value
"
:
b
"
root/c/c/c
"
})
nodes_dedup
=
list
(
self
.
root
.
iter_tree
())
nodes
=
list
(
self
.
root
.
iter_tree
(
dedup
=
False
))
assert
nodes
!=
nodes_dedup
assert
len
(
nodes
)
==
len
(
nodes_dedup
)
+
1
def
test_get
(
self
):
for
key
in
(
b
"
a
"
,
b
"
b
"
,
b
"
c
"
):
self
.
assertEqual
(
self
.
root
[
key
],
self
.
nodes
[
b
"
root/
"
+
key
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment