-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2960 from tediou5/feat/remove-unique-record-binar…
…y-heap feat: remove unique record binary heap
- Loading branch information
Showing
9 changed files
with
279 additions
and
439 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use libp2p::kad::KBucketDistance; | ||
pub use libp2p::kad::RecordKey; | ||
pub use libp2p::PeerId; | ||
use std::cmp::Ordering; | ||
use std::hash::Hash; | ||
|
||
type KademliaBucketKey<T> = libp2p::kad::KBucketKey<T>; | ||
|
||
/// Helper structure. It wraps Kademlia distance to a given peer for heap-metrics. | ||
#[derive(Debug, Clone, Eq)] | ||
pub struct KeyWithDistance { | ||
key: RecordKey, | ||
distance: KBucketDistance, | ||
} | ||
|
||
impl KeyWithDistance { | ||
/// Creates a new [`KeyWithDistance`] instance with the given `PeerId` and `K` key. | ||
/// | ||
/// The `distance` is calculated as the distance between the `KademliaBucketKey` derived | ||
/// from the `PeerId` and the `KademliaBucketKey` derived from the `K` key. | ||
pub fn new<K>(peer_id: PeerId, key: K) -> Self | ||
where | ||
RecordKey: From<K>, | ||
{ | ||
Self::new_with_record_key(peer_id, RecordKey::from(key)) | ||
} | ||
|
||
/// Creates a new [`KeyWithDistance`] instance with the given `PeerId` and `RecordKey`. | ||
pub fn new_with_record_key(peer_id: PeerId, key: RecordKey) -> Self { | ||
let peer_key = KademliaBucketKey::from(peer_id); | ||
let distance = KademliaBucketKey::new(key.as_ref()).distance(&peer_key); | ||
Self { key, distance } | ||
} | ||
|
||
/// Returns a reference to the record key. | ||
pub fn record_key(&self) -> &RecordKey { | ||
&self.key | ||
} | ||
} | ||
|
||
impl PartialEq for KeyWithDistance { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.key == other.key | ||
} | ||
} | ||
|
||
impl PartialOrd for KeyWithDistance { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for KeyWithDistance { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.distance.cmp(&other.distance) | ||
} | ||
} | ||
|
||
impl Hash for KeyWithDistance { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.key.hash(state); | ||
} | ||
} |
Oops, something went wrong.