Skip to content

Commit

Permalink
detecting of loops in kelondroTree during last/first-Node search
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1038 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Nov 6, 2005
1 parent 17d2830 commit 6dc42a2
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions source/de/anomic/kelondro/kelondroTree.java
Expand Up @@ -136,7 +136,7 @@ private void commitNode(Node n) throws IOException {
}

// Returns the value to which this map maps the specified key.
public byte[][] get(byte[] key) throws IOException {
public synchronized byte[][] get(byte[] key) throws IOException {
//System.out.println("kelondroTree.get " + new String(key) + " in " + filename);
Search search = new Search();
search.process(key);
Expand All @@ -148,7 +148,7 @@ public byte[][] get(byte[] key) throws IOException {
}
}

public long[] getLong(byte[] key) throws IOException {
public synchronized long[] getLong(byte[] key) throws IOException {
byte[][] row = get(key);
long[] longs = new long[columns() - 1];
if (row == null) {
Expand Down Expand Up @@ -295,14 +295,14 @@ public boolean isRight() throws IOException {
}
}

public boolean isChild(Node childn, Node parentn, int child) throws IOException {
public synchronized boolean isChild(Node childn, Node parentn, int child) throws IOException {
if (childn == null) throw new IllegalArgumentException("isLeftChild: Node parameter is NULL");
Handle lc = parentn.getOHHandle(child);
if (lc == null) return false;
return (lc.equals(childn.handle()));
}

public long[] putLong(byte[] key, long[] newlongs) throws IOException {
public synchronized long[] putLong(byte[] key, long[] newlongs) throws IOException {
byte[][] newrow = new byte[newlongs.length + 1][];
newrow[0] = key;
for (int i = 0; i < newlongs.length; i++) {
Expand Down Expand Up @@ -579,7 +579,7 @@ private void RR_LeftRotation(Node parentNode, Node childNode) throws IOException
}

// Associates the specified value with the specified key in this map
public byte[] put(byte[] key, byte[] value) throws IOException {
public synchronized byte[] put(byte[] key, byte[] value) throws IOException {
byte[][] row = new byte[2][];
row[0] = key;
row[1] = value;
Expand All @@ -602,7 +602,7 @@ public byte[][] remove(byte[] key) throws IOException {
}
}

public void removeAll() throws IOException {
public synchronized void removeAll() throws IOException {
while (size() > 0) remove(lastNode(), null);
}

Expand Down Expand Up @@ -716,9 +716,14 @@ private Node firstNode() throws IOException {
private Node firstNode(Node node) throws IOException {
if (node == null) throw new IllegalArgumentException("firstNode: node=null");
Handle h = node.getOHHandle(leftchild);
while (h != null) {
HashSet visitedNodeKeys = new HashSet(); // to detect loops
String nodeKey;
while (h != null) {
try {
node = getNode(h, node, leftchild);
nodeKey = new String(node.getKey());
if (visitedNodeKeys.contains(nodeKey)) throw new kelondroException(this.filename, "firstNode: database contains loops: '" + nodeKey + "' appears twice.");
visitedNodeKeys.add(nodeKey);
} catch (IllegalArgumentException e) {
// return what we have
return node;
Expand All @@ -737,9 +742,14 @@ private Node lastNode() throws IOException {
private Node lastNode(Node node) throws IOException {
if (node == null) throw new IllegalArgumentException("lastNode: node=null");
Handle h = node.getOHHandle(rightchild);
HashSet visitedNodeKeys = new HashSet(); // to detect loops
String nodeKey;
while (h != null) {
try {
node = getNode(h, node, rightchild);
nodeKey = new String(node.getKey());
if (visitedNodeKeys.contains(nodeKey)) throw new kelondroException(this.filename, "lastNode: database contains loops: '" + nodeKey + "' appears twice.");
visitedNodeKeys.add(nodeKey);
} catch (IllegalArgumentException e) {
// return what we have
return node;
Expand All @@ -758,7 +768,7 @@ public synchronized Iterator nodeIterator(boolean up, boolean rotating) {
}
}

public Iterator nodeIterator(boolean up, boolean rotating, byte[] firstKey) {
public synchronized Iterator nodeIterator(boolean up, boolean rotating, byte[] firstKey) {
// iterates the elements in a sorted way. returns Node - type Objects
try {
Search search = new Search();
Expand Down Expand Up @@ -939,14 +949,14 @@ public void remove() {
}
}

public rowIterator rows(boolean up, boolean rotating) throws IOException {
public synchronized rowIterator rows(boolean up, boolean rotating) throws IOException {
// iterates the rows of the Nodes
// enumerated objects are of type byte[][]
// iterates the elements in a sorted way.
return new rowIterator(new nodeIterator(up, rotating));
}

public Iterator rows(boolean up, boolean rotating, byte[] firstKey) throws IOException {
public synchronized Iterator rows(boolean up, boolean rotating, byte[] firstKey) throws IOException {
Search search = new Search();
search.process(firstKey);
if (search.found()) {
Expand Down

0 comments on commit 6dc42a2

Please sign in to comment.