私は修正された Kademlia P2P システムをここに書いていますが、ここで説明している問題は元のシステムの実装と非常によく似ています。
では、k-Buckets を実装する最も効率的な方法は何でしょうか? 私にとって重要なのは、アクセス時間、並列処理 (読み取りと書き込み)、およびメモリ消費です。
ConcurrentLinkedQueue と ConcurrentHashMap でそれを行うことを考えましたが、それはかなり冗長で厄介ですよね?
現時点では、LinkedList を同期しているだけです。
これが私のコードです:
import java.util.LinkedList;
class Bucket {
private final LinkedList<Neighbour> neighbours;
private final Object lock;
Bucket() {
neighbours = new LinkedList<>();
lock = new Object();
}
void sync(Neighbour n) {
synchronized(lock) {
int index = neighbours.indexOf(n);
if(index == -1) {
neighbours.add(n);
n.updateLastSeen();
} else {
Neighbour old = neighbours.remove(index);
neighbours.add(old);
old.updateLastSeen();
}
}
}
void remove(Neighbour n) {
synchronized(lock) {
neighbours.remove(n);
}
}
Neighbour resolve(Node n) throws ResolveException {
Neighbour nextHop;
synchronized(lock) {
int index = neighbours.indexOf(n);
if(index == -1) {
nextHop = neighbours.poll();
neighbours.add(nextHop);
return nextHop;
} else {
return neighbours.get(index);
}
}
}
}
不思議に思わないでください、私は別のネイバー立ち退きプロセスを実装しました。