refactor trimmers to their own files

This commit is contained in:
zzz
2012-08-29 14:05:02 +00:00
parent d05f1ca2c8
commit d2a7af2884
9 changed files with 99 additions and 69 deletions

View File

@@ -768,70 +768,6 @@ public class KBucketSet<T extends SimpleDataStructure> {
}
}
/**
* Called when a kbucket can no longer be split and is too big
*/
public interface KBucketTrimmer<K extends SimpleDataStructure> {
/**
* Called from add() just before adding the entry.
* You may call getEntries() and/or remove() from here.
* Do NOT call add().
* To always discard a newer entry, always return false.
*
* @param kbucket the kbucket that is now too big
* @return true to actually add the entry.
*/
public boolean trim(KBucket<K> kbucket, K toAdd);
}
/**
* Removes a random element. Not resistant to flooding.
*/
public static class RandomTrimmer<T extends SimpleDataStructure> implements KBucketTrimmer<T> {
protected final I2PAppContext _ctx;
private final int _max;
public RandomTrimmer(I2PAppContext ctx, int max) {
_ctx = ctx;
_max = max;
}
public boolean trim(KBucket<T> kbucket, T toAdd) {
List<T> e = new ArrayList(kbucket.getEntries());
int sz = e.size();
// concurrency
if (sz < _max)
return true;
T toRemove = e.get(_ctx.random().nextInt(sz));
return kbucket.remove(toRemove);
}
}
/**
* Removes a random element, but only if the bucket hasn't changed in 5 minutes.
*/
public static class RandomIfOldTrimmer<T extends SimpleDataStructure> extends RandomTrimmer<T> {
public RandomIfOldTrimmer(I2PAppContext ctx, int max) {
super(ctx, max);
}
public boolean trim(KBucket<T> kbucket, T toAdd) {
if (kbucket.getLastChanged() > _ctx.clock().now() - 5*60*1000)
return false;
return super.trim(kbucket, toAdd);
}
}
/**
* Removes nothing and always rejects the add. Flood resistant..
*/
public static class RejectTrimmer<T extends SimpleDataStructure> implements KBucketTrimmer<T> {
public boolean trim(KBucket<T> kbucket, T toAdd) {
return false;
}
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(1024);