?? fingertable.java
字號:
} else {
this.logger.info("Added reference to finger table entries "
+ lowestWrittenIndex + " to " + highestWrittenIndex);
}
}
}
/**
* Returns a copy of the finger table entries.
*
* @return Copy of finger table entries.
*/
final Node[] getCopyOfReferences() {
this.logger.debug("Returning copy of references.");
Node[] copy = new Node[this.remoteNodes.length];
System.arraycopy(this.remoteNodes, 0, copy, 0, this.remoteNodes.length);
return copy;
}
/**
* Returns a formatted string representation of this finger table.
*
* @return String representation containing one line per reference, together
* with the annotation which table entries contain this reference.
*/
public final String toString() {
StringBuilder result = new StringBuilder("Finger table:\n");
int lastIndex = -1;
ID lastNodeID = null;
URL lastNodeURL = null;
for (int i = 0; i < this.remoteNodes.length; i++) {
Node next = this.remoteNodes[i];
if (next == null) {
// row ended or did not even start
if ((lastIndex != -1) && (lastNodeID != null)) {
// row ended
result.append(" "
+ lastNodeID
+ ", "
+ lastNodeURL
+ " "
+ ((i - 1 - lastIndex > 0) ? "(" + lastIndex + "-"
+ (i - 1) + ")" : "(" + (i - 1) + ")")
+ "\n");
lastIndex = -1;
lastNodeID = null;
lastNodeURL = null;
} else {
// null at beginning
}
} else if (lastNodeID == null) {
// found first reference in a row
lastIndex = i;
lastNodeID = next.getNodeID();
lastNodeURL = next.getNodeURL();
} else if (!lastNodeID.equals(next.getNodeID())) {
// found different reference in finger table
result.append(" "
+ lastNodeID
+ ", "
+ lastNodeURL
+ " "
+ ((i - 1 - lastIndex > 0) ? "(" + lastIndex + "-"
+ (i - 1) + ")" : "(" + (i - 1) + ")") + "\n");
lastNodeID = next.getNodeID();
lastNodeURL = next.getNodeURL();
lastIndex = i;
} else {
// found next reference in a row
}
}
// display last row
if (lastNodeID != null && lastIndex != -1) {
// row ended
result.append(" "
+ lastNodeID
+ ", "
+ lastNodeURL
+ " "
+ ((this.remoteNodes.length - 1 - lastIndex > 0) ? "("
+ lastIndex + "-" + (this.remoteNodes.length - 1)
+ ")" : "(" + (this.remoteNodes.length - 1) + ")")
+ "\n");
lastNodeID = null;
}
return result.toString();
}
/**
* Removes all occurences of the given node from finger table.
*
* @param node1
* Reference to be removed from the finger table.
* @throws NullPointerException
* If given reference is <code>null</code>.
*/
final void removeReference(Node node1) {
if (node1 == null) {
NullPointerException e = new NullPointerException(
"removeReference cannot be invoked with value null!");
this.logger.error("Null pointer", e);
throw e;
}
// for logging
int lowestWrittenIndex = -1;
int highestWrittenIndex = -1;
// determine node reference with next larger ID than ID of node
// reference to remove
Node referenceForReplacement = null;
for (int i = this.localID.getLength() - 1; i >= 0; i--) {
Node n = this.getEntry(i);
if (node1.equals(n)) {
break;
}
if (n != null) {
referenceForReplacement = n;
}
}
// remove reference(s)
for (int i = 0; i < this.remoteNodes.length; i++) {
if (node1.equals(this.remoteNodes[i])) {
// for logging
if (lowestWrittenIndex == -1) {
lowestWrittenIndex = i;
}
highestWrittenIndex = i;
if (referenceForReplacement == null) {
unsetEntry(i);
} else {
setEntry(i, referenceForReplacement);
}
}
}
// try to add references of successor list to fill 'holes' in finger
// table
List<Node> referencesOfSuccessorList = new ArrayList<Node>(
this.references.getSuccessors());
referencesOfSuccessorList.remove(node1);
for (Node referenceToAdd : referencesOfSuccessorList) {
this.addReference(referenceToAdd);
}
// logging
if (this.logger.isEnabledFor(DEBUG)) {
if (highestWrittenIndex == -1) {
this.logger
.debug("removeReference did not remove the given reference, "
+ "because it did not exist in finger table "
+ "anywhere!");
} else if (highestWrittenIndex == lowestWrittenIndex) {
this.logger.debug("Removed reference from finger table entry "
+ highestWrittenIndex);
} else {
this.logger
.debug("Removed reference from finger table entries "
+ lowestWrittenIndex + " to "
+ highestWrittenIndex);
}
}
}
/**
* Determines closest preceding node of given id.
*
* @param key
* ID of which the closest preceding node shall be determined.
* @throws NullPointerException
* If given key is null.
* @return Reference to the node which most closely precedes the given ID.
* <code>null</code> if no node has been found.
*/
final Node getClosestPrecedingNode(ID key) {
if (key == null) {
NullPointerException e = new NullPointerException(
"ID to determine the closest preceding node may not be "
+ "null!");
this.logger.error("Null pointer", e);
throw e;
}
boolean debug = this.logger.isEnabledFor(DEBUG);
for (int i = this.remoteNodes.length - 1; i >= 0; i--) {
if (this.remoteNodes[i] != null
&& this.remoteNodes[i].getNodeID().isInInterval(
this.localID, key)) {
if (debug) {
this.logger.debug("Closest preceding node for ID " + key
+ " is " + this.remoteNodes[i].toString());
}
return this.remoteNodes[i];
}
}
if (debug) {
this.logger.debug("There is no closest preceding node for ID "
+ key + " -- returning null!");
}
return null;
}
/**
* Determines if the given reference is stored somewhere in the finger
* table.
*
* @param newReference
* Reference of which existence shall be determined.
* @throws NullPointerException
* If reference to look for is <code>null</code>.
* @return <code>true</code>, if the given reference exists in the finger
* table, or <code>false</code>, else.
*/
final boolean containsReference(Node newReference) {
if (newReference == null) {
NullPointerException e = new NullPointerException(
"Reference to check must not be null!");
this.logger.error("Null pointer", e);
throw e;
}
for (int i = 0; i < this.remoteNodes.length; i++) {
if (newReference.equals(this.remoteNodes[i])) {
return true;
}
}
return false;
}
/**
* @param i
* @return The first (i+1) entries of finger table. If there are fewer then
* i+1 entries only these are returned.
*
*
*
*/
final List<Node> getFirstFingerTableEntries(int i) {
Set<Node> result = new HashSet<Node>();
for (int j = 0; j < this.remoteNodes.length; j++) {
if (this.getEntry(j) != null) {
result.add(this.getEntry(j));
}
if (result.size() >= i) {
break;
}
}
return new ArrayList<Node>(result);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -