- All Implemented Interfaces:
Serializable, Comparator<NodeInfo>
This comparator implements the Kademlia distance metric to determine which of two nodes is closer to a target key (either an info hash or node ID). The distance is calculated using XOR distance, which is the standard metric used in Kademlia-based DHTs.
The XOR distance metric has the important property that it forms a metric space, meaning it satisfies the triangle inequality and other mathematical properties that make it suitable for routing in distributed hash tables.
This comparator is primarily used for:
- Finding the k closest nodes to a target info hash
- Maintaining sorted lists of nodes by distance
- Selecting the best nodes for DHT queries and responses
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]The target key (info hash or node ID) to measure distances against.private static final longPinned for serialized-form compatibility.Constructor Summary
ConstructorsConstructorDescriptionCreates a new comparator for measuring distance to the specified target key.Method Summary
All MethodsMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparator
equals, max, min, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
Field Details
serialVersionUID
private static final long serialVersionUIDPinned for serialized-form compatibility.- See Also:
_base
private final byte[] _baseThe target key (info hash or node ID) to measure distances against.
Constructor Details
NodeInfoComparator
Creates a new comparator for measuring distance to the specified target key.- Parameters:
h- the target key (info hash or node ID) to compare distances against
Method Details
compare
Compares two nodes based on their XOR distance to the target key.The comparison calculates the XOR distance between each node's ID and the target key, then compares these distances lexicographically. The node with the smaller XOR distance is considered "closer" to the target.
This method implements the standard Kademlia distance metric used throughout the DHT for routing and node selection.
- Specified by:
comparein interfaceComparator<NodeInfo>- Parameters:
lhs- the first node to comparerhs- the second node to compare- Returns:
- negative value if lhs is closer to target, positive if rhs is closer, 0 if equal distance