Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
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
savoirfairelinux
opendht
Commits
d26a34f4
Commit
d26a34f4
authored
8 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
node_cache: use std::map
parent
6652f27a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/node_cache.h
+6
-12
6 additions, 12 deletions
include/opendht/node_cache.h
src/node_cache.cpp
+20
-62
20 additions, 62 deletions
src/node_cache.cpp
with
26 additions
and
74 deletions
include/opendht/node_cache.h
+
6
−
12
View file @
d26a34f4
...
...
@@ -38,21 +38,15 @@ struct NodeCache {
void
clearBadNodes
(
sa_family_t
family
=
0
);
private:
struct
NodeTree
{
std
::
shared_ptr
<
Node
>
get
(
const
InfoHash
&
id
);
std
::
shared_ptr
<
Node
>
get
(
const
InfoHash
&
id
,
const
sockaddr
*
sa
,
socklen_t
sa_len
,
time_point
now
,
int
confirmed
);
class
NodeMap
:
public
std
::
map
<
InfoHash
,
std
::
weak_ptr
<
Node
>>
{
public:
std
::
shared_ptr
<
Node
>
get
Node
(
const
InfoHash
&
id
);
std
::
shared_ptr
<
Node
>
getNode
(
const
InfoHash
&
id
,
const
sockaddr
*
sa
,
socklen_t
sa_len
,
time_point
now
,
int
confirmed
);
void
clearBadNodes
();
private:
std
::
shared_ptr
<
Node
>
getLocal
(
const
InfoHash
&
id
);
std
::
vector
<
NodeTree
>
childs
;
std
::
vector
<
std
::
weak_ptr
<
Node
>>
nodes
;
};
Node
Tree
cache_4
;
Node
Tree
cache_6
;
Node
Map
cache_4
;
Node
Map
cache_6
;
};
}
This diff is collapsed.
Click to expand it.
src/node_cache.cpp
+
20
−
62
View file @
d26a34f4
...
...
@@ -23,12 +23,12 @@ namespace dht {
std
::
shared_ptr
<
Node
>
NodeCache
::
getNode
(
const
InfoHash
&
id
,
sa_family_t
family
)
{
return
(
family
==
AF_INET
?
cache_4
:
cache_6
).
get
(
id
);
return
(
family
==
AF_INET
?
cache_4
:
cache_6
).
get
Node
(
id
);
}
std
::
shared_ptr
<
Node
>
NodeCache
::
getNode
(
const
InfoHash
&
id
,
const
sockaddr
*
sa
,
socklen_t
sa_len
,
time_point
now
,
int
confirm
)
{
return
(
sa
->
sa_family
==
AF_INET
?
cache_4
:
cache_6
).
get
(
id
,
sa
,
sa_len
,
now
,
confirm
);
return
(
sa
->
sa_family
==
AF_INET
?
cache_4
:
cache_6
).
get
Node
(
id
,
sa
,
sa_len
,
now
,
confirm
);
}
void
...
...
@@ -43,82 +43,40 @@ NodeCache::clearBadNodes(sa_family_t family)
}
std
::
shared_ptr
<
Node
>
NodeCache
::
Node
Tree
::
get
Local
(
const
InfoHash
&
id
)
NodeCache
::
Node
Map
::
get
Node
(
const
InfoHash
&
id
)
{
for
(
auto
it
=
nodes
.
begin
();
it
!=
nodes
.
end
();)
{
if
(
auto
n
=
it
->
lock
())
{
if
(
n
->
id
==
id
)
return
n
;
++
it
;
}
else
{
it
=
nodes
.
erase
(
it
);
}
}
auto
wn
=
find
(
id
);
if
(
wn
==
end
())
return
{};
if
(
auto
n
=
wn
->
second
.
lock
())
return
n
;
erase
(
wn
);
return
{};
}
std
::
shared_ptr
<
Node
>
NodeCache
::
Node
Tree
::
get
(
const
InfoHash
&
id
)
NodeCache
::
Node
Map
::
get
Node
(
const
InfoHash
&
id
,
const
sockaddr
*
sa
,
socklen_t
sa_len
,
time_point
now
,
int
confirm
)
{
NodeTree
*
t
=
this
;
for
(
auto
b
:
id
)
{
if
(
t
->
childs
.
empty
())
return
t
->
getLocal
(
id
);
else
t
=
&
t
->
childs
[
b
];
}
return
{};
}
std
::
shared_ptr
<
Node
>
NodeCache
::
NodeTree
::
get
(
const
InfoHash
&
id
,
const
sockaddr
*
sa
,
socklen_t
sa_len
,
time_point
now
,
int
confirm
)
{
// find the bucket
NodeTree
*
t
=
this
;
size_t
offset
=
0
;
while
(
not
t
->
childs
.
empty
()
and
offset
<
4
)
t
=
&
t
->
childs
[
id
[
offset
++
]];
// find node in bucket
auto
node
=
t
->
getLocal
(
id
);
auto
it
=
emplace
(
id
,
std
::
weak_ptr
<
Node
>
{});
auto
node
=
it
.
first
->
second
.
lock
();
if
(
not
node
)
{
node
=
std
::
make_shared
<
Node
>
(
id
,
sa
,
sa_len
);
// insert node in bucket
if
(
t
->
nodes
.
size
()
>=
8
&&
offset
<
4
)
{
offset
++
;
t
->
childs
.
resize
(
256
);
for
(
auto
&
w
:
t
->
nodes
)
{
if
(
auto
tn
=
w
.
lock
())
{
t
->
childs
[
tn
->
id
[
offset
]].
nodes
.
emplace_back
(
std
::
move
(
w
));
}
}
t
->
nodes
=
{};
t
->
childs
[
id
[
offset
]].
nodes
.
emplace_back
(
node
);
}
else
{
t
->
nodes
.
emplace_back
(
node
);
}
it
.
first
->
second
=
node
;
}
else
if
(
confirm
||
node
->
time
<
now
-
Node
::
NODE_EXPIRE_TIME
)
{
node
->
update
(
sa
,
sa_len
);
}
/*if (confirm)
node->received(now, confirm >= 2);*/
return
node
;
}
void
NodeCache
::
NodeTree
::
clearBadNodes
()
{
if
(
childs
.
empty
())
{
for
(
auto
it
=
nodes
.
begin
();
it
!=
nodes
.
end
();)
{
if
(
auto
n
=
it
->
lock
())
{
n
->
reset
();
++
it
;
}
else
{
it
=
nodes
.
erase
(
it
);
}
NodeCache
::
NodeMap
::
clearBadNodes
()
{
for
(
auto
it
=
cbegin
();
it
!=
cend
();)
{
if
(
auto
n
=
it
->
second
.
lock
())
{
n
->
reset
();
++
it
;
}
else
{
erase
(
it
++
);
}
}
else
{
for
(
auto
&
c
:
childs
)
c
.
clearBadNodes
();
}
}
...
...
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