Package net.i2p.kademlia
Class KBucketImpl<T extends SimpleDataStructure>
- java.lang.Object
-
- net.i2p.kademlia.KBucketImpl<T>
-
- All Implemented Interfaces:
KBucket<T>
class KBucketImpl<T extends SimpleDataStructure> extends Object implements KBucket<T>
A concurrent implementation using ConcurrentHashSet. The max size (K) may be temporarily exceeded due to concurrency, a pending split, or the behavior of the supplied trimmer, as explained below. The creator is responsible for splits. This class has no knowledge of the DHT base used for XORing, and thus there are no validity checks in add/remove. The begin and end values are immutable. All entries in this bucket will have at least one bit different from us in the range [begin, end] inclusive. Splits must be implemented by creating two new buckets and discarding this one. The keys are kept in a Set and are NOT sorted by last-seen. Per-key last-seen-time, failures, etc. must be tracked elsewhere. If this bucket is full (i.e. begin == end && size == max) then add() will call KBucketTrimmer.trim() do (possibly) remove older entries, and indicate whether to add the new entry. If the trimmer returns true without removing entries, this KBucket will exceed the max size. Refactored from net.i2p.router.networkdb.kademlia- Since:
- 0.9.2 in i2psnark, moved to core in 0.9.10
-
-
Constructor Summary
Constructors Constructor Description KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketTrimmer<T> trimmer)All entries in this bucket will have at least one bit different from us in the range [begin, end] inclusive.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T peer)Sets last-changed if rv is true OR if the peer is already present.voidclear()Set<T>getEntries()Retrieve all routing table entries stored in the bucketvoidgetEntries(SelectionCollector<T> collector)intgetKeyCount()Number of keys already contained in this kbucketlonggetLastChanged()The last-changed timestamp, which actually indicates last-added or last-seen.intgetRangeBegin()Lowest order high bit for difference keys.intgetRangeEnd()Highest high bit for the difference keys.booleanremove(T peer)Remove the key from the bucketvoidsetLastChanged()Update the last-changed timestamp to now.StringtoString()
-
-
-
Constructor Detail
-
KBucketImpl
public KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketTrimmer<T> trimmer)
All entries in this bucket will have at least one bit different from us in the range [begin, end] inclusive.
-
-
Method Detail
-
getRangeBegin
public int getRangeBegin()
Description copied from interface:KBucketLowest order high bit for difference keys. The lower-bounds distance of this bucket is 2**begin. If begin == 0, this is the closest bucket.- Specified by:
getRangeBeginin interfaceKBucket<T extends SimpleDataStructure>
-
getRangeEnd
public int getRangeEnd()
Description copied from interface:KBucketHighest high bit for the difference keys. The upper-bounds distance of this bucket is (2**(end+1)) - 1. If begin == end, the bucket cannot be split further. If end == (numbits - 1), this is the furthest bucket.- Specified by:
getRangeEndin interfaceKBucket<T extends SimpleDataStructure>
-
getKeyCount
public int getKeyCount()
Description copied from interface:KBucketNumber of keys already contained in this kbucket- Specified by:
getKeyCountin interfaceKBucket<T extends SimpleDataStructure>
-
getEntries
public Set<T> getEntries()
Description copied from interface:KBucketRetrieve all routing table entries stored in the bucket- Specified by:
getEntriesin interfaceKBucket<T extends SimpleDataStructure>- Returns:
- an unmodifiable view; not a copy
-
getEntries
public void getEntries(SelectionCollector<T> collector)
- Specified by:
getEntriesin interfaceKBucket<T extends SimpleDataStructure>
-
clear
public void clear()
- Specified by:
clearin interfaceKBucket<T extends SimpleDataStructure>
-
add
public boolean add(T peer)
Sets last-changed if rv is true OR if the peer is already present. Calls the trimmer if begin == end and we are full. If begin != end then add it and caller must do bucket splitting.- Specified by:
addin interfaceKBucket<T extends SimpleDataStructure>- Returns:
- true if added
-
remove
public boolean remove(T peer)
Description copied from interface:KBucketRemove the key from the bucket- Specified by:
removein interfaceKBucket<T extends SimpleDataStructure>- Returns:
- if removed. Does NOT set lastChanged.
-
setLastChanged
public void setLastChanged()
Update the last-changed timestamp to now.- Specified by:
setLastChangedin interfaceKBucket<T extends SimpleDataStructure>
-
getLastChanged
public long getLastChanged()
The last-changed timestamp, which actually indicates last-added or last-seen.- Specified by:
getLastChangedin interfaceKBucket<T extends SimpleDataStructure>
-
-