Package net.i2p.kademlia
Class KBucketSet<T extends SimpleDataStructure>
java.lang.Object
net.i2p.kademlia.KBucketSet<T>
In-memory storage of buckets sorted by the XOR metric from the base (us)
passed in via the constructor.
This starts with one bucket covering the whole key space, and
may eventually be split to a max of the number of bits in the data type
(160 for SHA1Hash or 256 for Hash),
times 2**(B-1) for Kademlia value B.
Refactored from net.i2p.router.networkdb.kademlia
- Since:
- 0.9.2 in i2psnark, moved to core in 0.9.10
-
Constructor Summary
ConstructorDescriptionKBucketSet
(I2PAppContext context, T us, int max, int b) Use the default trim strategy, which removes a random entry.KBucketSet
(I2PAppContext context, T us, int max, int b, KBucketTrimmer<T> trimmer) Use the supplied trim strategy. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
clear()
(package private) T
generateRandomKey
(KBucket<T> bucket) Generate a random key to go within this bucket Package private for testing only.getAll()
void
getAll
(SelectionCollector<T> collector) Returned list is a copy of the bucket list, closest first, with the actual buckets (not a copy).getClosest
(int max) The keys closest to us.getClosest
(int max, Collection<T> toIgnore) The keys closest to us.getClosest
(T key, int max) The keys closest to the key.getClosest
(T key, int max, Collection<T> toIgnore) The keys closest to the key.getExploreKeys
(long age) For every bucket that hasn't been updated in this long, or isn't close to full, generate a random key that would be a member of that bucket.(package private) int
The number of bits minus 1 (range number) for the xor of the key.boolean
int
size()
The current number of entries.toString()
-
Constructor Details
-
KBucketSet
Use the default trim strategy, which removes a random entry.- Parameters:
us
- the local identity (typically a SHA1Hash or Hash) The class must have a zero-argument constructor.max
- the Kademlia value "k", the max per bucket, k >= 4b
- the Kademlia value "b", split buckets an extra 2**(b-1) times, b > 0, use 1 for bittorrent, Kademlia paper recommends 5
-
KBucketSet
Use the supplied trim strategy.
-
-
Method Details
-
add
- Returns:
- true if the peer is new to the bucket it goes in, or false if it was already in it. Always returns false on an attempt to add ourselves.
-
size
public int size()The current number of entries. -
remove
-
clear
public void clear()- Since:
- 0.8.8
-
getAll
- Returns:
- a copy in a new set
-
getAll
- Returns:
- a copy in a new set
-
getAll
-
getClosest
The keys closest to us. Returned list will never contain us.- Returns:
- non-null, closest first
-
getClosest
The keys closest to us. Returned list will never contain us.- Returns:
- non-null, closest first
-
getClosest
The keys closest to the key. Returned list will never contain us.- Returns:
- non-null, closest first
-
getClosest
The keys closest to the key. Returned list will never contain us.- Returns:
- non-null, closest first
-
getBuckets
Returned list is a copy of the bucket list, closest first, with the actual buckets (not a copy). Primarily for testing. You shouldn't ever need to get all the buckets. Use getClosest() or getAll() instead to get the keys.- Returns:
- non-null
-
getRange
The number of bits minus 1 (range number) for the xor of the key. Package private for testing only. Others shouldn't need this.- Returns:
- 0 to max-1 or -1 for us
-
getExploreKeys
For every bucket that hasn't been updated in this long, or isn't close to full, generate a random key that would be a member of that bucket. The returned keys may be searched for to "refresh" the buckets.- Returns:
- non-null, closest first
-
generateRandomKey
Generate a random key to go within this bucket Package private for testing only. Others shouldn't need this. -
toString
-