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
p2p
go-cidranger
Commits
07df2508
Unverified
Commit
07df2508
authored
Dec 23, 2019
by
Yulin Chen
Committed by
GitHub
Dec 23, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve readability for insert on trie (#22)
parent
6947c077
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
22 deletions
+34
-22
trie.go
trie.go
+34
-22
No files found.
trie.go
View file @
07df2508
...
...
@@ -208,40 +208,52 @@ func (p *prefixTrie) insert(network rnet.Network, entry RangerEntry) error {
p
.
entry
=
entry
return
nil
}
bit
,
err
:=
p
.
targetBitFromIP
(
network
.
Number
)
if
err
!=
nil
{
return
err
}
child
:=
p
.
children
[
bit
]
if
child
==
nil
{
return
p
.
insertPrefix
(
bit
,
newEntryTrie
(
network
,
entry
))
}
existingChild
:=
p
.
children
[
bit
]
lcb
,
err
:=
network
.
LeastCommonBitPosition
(
child
.
network
)
if
err
!=
nil
{
return
err
// No existing child, insert new leaf trie.
if
existingChild
==
nil
{
p
.
appendTrie
(
bit
,
newEntryTrie
(
network
,
entry
))
return
nil
}
if
int
(
lcb
)
>
child
.
targetBitPosition
()
+
1
{
child
=
newPathprefixTrie
(
network
,
p
.
totalNumberOfBits
()
-
lcb
)
err
:=
p
.
insertPrefix
(
bit
,
child
)
// Check whether it is necessary to insert additional path prefix between current trie and existing child,
// in the case that inserted network diverges on its path to existing child.
lcb
,
err
:=
network
.
LeastCommonBitPosition
(
existingChild
.
network
)
divergingBitPos
:=
int
(
lcb
)
-
1
if
divergingBitPos
>
existingChild
.
targetBitPosition
()
{
pathPrefix
:=
newPathprefixTrie
(
network
,
p
.
totalNumberOfBits
()
-
lcb
)
err
:=
p
.
insertPrefix
(
bit
,
pathPrefix
,
existingChild
)
if
err
!=
nil
{
return
err
}
// Update new child
existingChild
=
pathPrefix
}
return
c
hild
.
insert
(
network
,
entry
)
return
existingC
hild
.
insert
(
network
,
entry
)
}
func
(
p
*
prefixTrie
)
insertPrefix
(
bits
uint32
,
prefix
*
prefixTrie
)
error
{
child
:=
p
.
children
[
bits
]
if
child
!=
nil
{
prefixBit
,
err
:=
prefix
.
targetBitFromIP
(
child
.
network
.
Number
)
if
err
!=
nil
{
return
err
}
prefix
.
insertPrefix
(
prefixBit
,
child
)
}
p
.
children
[
bits
]
=
prefix
func
(
p
*
prefixTrie
)
appendTrie
(
bit
uint32
,
prefix
*
prefixTrie
)
{
p
.
children
[
bit
]
=
prefix
prefix
.
parent
=
p
}
func
(
p
*
prefixTrie
)
insertPrefix
(
bit
uint32
,
pathPrefix
,
child
*
prefixTrie
)
error
{
// Set parent/child relationship between current trie and inserted pathPrefix
p
.
children
[
bit
]
=
pathPrefix
pathPrefix
.
parent
=
p
// Set parent/child relationship between inserted pathPrefix and original child
pathPrefixBit
,
err
:=
pathPrefix
.
targetBitFromIP
(
child
.
network
.
Number
)
if
err
!=
nil
{
return
err
}
pathPrefix
.
children
[
pathPrefixBit
]
=
child
child
.
parent
=
pathPrefix
return
nil
}
...
...
@@ -272,7 +284,7 @@ func (p *prefixTrie) qualifiesForPathCompression() bool {
// 1. records no CIDR entry
// 2. has single or no child
// 3. is not root trie
return
!
p
.
hasEntry
()
&&
p
.
childrenCount
()
<=
1
&&
p
.
parent
!=
nil
;
return
!
p
.
hasEntry
()
&&
p
.
childrenCount
()
<=
1
&&
p
.
parent
!=
nil
}
func
(
p
*
prefixTrie
)
compressPathIfPossible
()
error
{
...
...
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