|
|
|
@ -76,6 +76,8 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
|
|
|
|
|
private static int root = 0; // pointer for FHandles-array: pointer to root node
|
|
|
|
|
|
|
|
|
|
private Search writeSearchObj = new Search();
|
|
|
|
|
|
|
|
|
|
public kelondroTree(File file, long buffersize, int key, int value) throws IOException {
|
|
|
|
|
this(file, buffersize, new int[] {key, value}, 1, 8);
|
|
|
|
|
}
|
|
|
|
@ -134,15 +136,14 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the value to which this map maps the specified key.
|
|
|
|
|
public synchronized byte[][] get(byte[] key) throws IOException {
|
|
|
|
|
public byte[][] get(byte[] key) throws IOException {
|
|
|
|
|
//System.out.println("kelondroTree.get " + new String(key) + " in " + filename);
|
|
|
|
|
Search search = new Search(key);
|
|
|
|
|
Search search = new Search();
|
|
|
|
|
search.process(key);
|
|
|
|
|
if (search.found()) {
|
|
|
|
|
byte[][] result = search.getMatcher().getValues();
|
|
|
|
|
search = null;
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
search = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -174,21 +175,16 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
private Node thenode, parentnode;
|
|
|
|
|
private boolean found; // property if node was found
|
|
|
|
|
private byte child; // -1: left child; 0: root node; 1: right child
|
|
|
|
|
private Handle thisHandle;
|
|
|
|
|
|
|
|
|
|
protected Search(byte[] key) throws IOException {
|
|
|
|
|
this.key = key;
|
|
|
|
|
searchproc();
|
|
|
|
|
}
|
|
|
|
|
protected Search(Node node) throws IOException {
|
|
|
|
|
this.key = node.getKey();
|
|
|
|
|
searchproc();
|
|
|
|
|
protected Search() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void searchproc() throws IOException {
|
|
|
|
|
protected void process(byte[] key) throws IOException {
|
|
|
|
|
// searchs the database for the key and stores the result in the thisHandle
|
|
|
|
|
// if the key was found, then found=true, thisHandle and leftchild is set;
|
|
|
|
|
// else found=false and thisHandle and leftchild is undefined
|
|
|
|
|
Handle thisHandle = getHandle(root);
|
|
|
|
|
thisHandle = getHandle(root);
|
|
|
|
|
parentnode = null;
|
|
|
|
|
if (key == null) {
|
|
|
|
|
child = 0;
|
|
|
|
@ -204,7 +200,6 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
child = 0;
|
|
|
|
|
found = false;
|
|
|
|
|
int c;
|
|
|
|
|
Handle[] handles;
|
|
|
|
|
HashMap visitedNodeKeys = new HashMap(); // to detect loops
|
|
|
|
|
String otherkey;
|
|
|
|
|
//System.out.println("Starting Compare Loop in Database " + filename); // debug
|
|
|
|
@ -328,18 +323,18 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Associates the specified value with the specified key in this map
|
|
|
|
|
public synchronized byte[][] put(byte[][] newrow) throws IOException {
|
|
|
|
|
public byte[][] put(byte[][] newrow) throws IOException {
|
|
|
|
|
if (newrow.length != columns()) throw new IllegalArgumentException("put: wrong row length " + newrow.length + "; must be " + columns());
|
|
|
|
|
// first try to find the key element in the database
|
|
|
|
|
Search searchResult = new Search(newrow[0]);
|
|
|
|
|
if (searchResult.found()) {
|
|
|
|
|
synchronized(writeSearchObj) {
|
|
|
|
|
writeSearchObj.process(newrow[0]);
|
|
|
|
|
if (writeSearchObj.found()) {
|
|
|
|
|
// a node with this key exist. simply overwrite the content and return old content
|
|
|
|
|
Node e = searchResult.getMatcher();
|
|
|
|
|
Node e = writeSearchObj.getMatcher();
|
|
|
|
|
byte[][] result = e.setValues(newrow);
|
|
|
|
|
commitNode(e);
|
|
|
|
|
searchResult = null;
|
|
|
|
|
return result;
|
|
|
|
|
} else if (searchResult.isRoot()) {
|
|
|
|
|
} else if (writeSearchObj.isRoot()) {
|
|
|
|
|
// a node with this key does not exist and there is no node at all
|
|
|
|
|
// this therefore creates the root node if an only if there was no root Node yet
|
|
|
|
|
if (getHandle(root) != null)
|
|
|
|
@ -356,7 +351,6 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
// do updates
|
|
|
|
|
e.commit(CP_LOW);
|
|
|
|
|
setHandle(root, e.handle());
|
|
|
|
|
searchResult = null;
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
// a node with this key does not exist
|
|
|
|
@ -367,7 +361,7 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
// that side, but not if the assigned position is appropriate.
|
|
|
|
|
|
|
|
|
|
// create new node and assign values
|
|
|
|
|
Node parentNode = searchResult.getParent();
|
|
|
|
|
Node parentNode = writeSearchObj.getParent();
|
|
|
|
|
Node theNode = newNode();
|
|
|
|
|
theNode.setValues(newrow);
|
|
|
|
|
theNode.setOHByte(0, (byte) 1); // fresh magic
|
|
|
|
@ -379,10 +373,10 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
|
|
|
|
|
// check consistency and link new node to parent node
|
|
|
|
|
byte parentBalance;
|
|
|
|
|
if (searchResult.isLeft()) {
|
|
|
|
|
if (writeSearchObj.isLeft()) {
|
|
|
|
|
if (parentNode.getOHHandle(leftchild) != null) throw new kelondroException(filename, "tried to create leftchild node twice");
|
|
|
|
|
parentNode.setOHHandle(leftchild, theNode.handle());
|
|
|
|
|
} else if (searchResult.isRight()) {
|
|
|
|
|
} else if (writeSearchObj.isRight()) {
|
|
|
|
|
if (parentNode.getOHHandle(rightchild) != null) throw new kelondroException(filename, "tried to create rightchild node twice");
|
|
|
|
|
parentNode.setOHHandle(rightchild, theNode.handle());
|
|
|
|
|
} else {
|
|
|
|
@ -468,6 +462,7 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
return null; // that means: no previous stored value present
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assignChild(Node parentNode, Node childNode, int childType) throws IOException {
|
|
|
|
|
parentNode.setOHHandle(childType, childNode.handle());
|
|
|
|
@ -593,18 +588,19 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Removes the mapping for this key from this map if present (optional operation).
|
|
|
|
|
public synchronized byte[][] remove(byte[] key) throws IOException {
|
|
|
|
|
Search search = new Search(key);
|
|
|
|
|
if (search.found()) {
|
|
|
|
|
Node result = search.getMatcher();
|
|
|
|
|
public byte[][] remove(byte[] key) throws IOException {
|
|
|
|
|
synchronized(writeSearchObj) {
|
|
|
|
|
writeSearchObj.process(key);
|
|
|
|
|
if (writeSearchObj.found()) {
|
|
|
|
|
Node result = writeSearchObj.getMatcher();
|
|
|
|
|
byte[][] values = result.getValues();
|
|
|
|
|
remove(result, search.getParent());
|
|
|
|
|
search = null;
|
|
|
|
|
remove(result, writeSearchObj.getParent());
|
|
|
|
|
return values;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeAll() throws IOException {
|
|
|
|
|
while (size() > 0) remove(lastNode(), null);
|
|
|
|
@ -762,17 +758,16 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized Iterator nodeIterator(boolean up, boolean rotating, byte[] firstKey) {
|
|
|
|
|
public Iterator nodeIterator(boolean up, boolean rotating, byte[] firstKey) {
|
|
|
|
|
// iterates the elements in a sorted way. returns Node - type Objects
|
|
|
|
|
try {
|
|
|
|
|
Search s = new Search(firstKey);
|
|
|
|
|
if (s.found()) {
|
|
|
|
|
Node matcher = s.getMatcher();
|
|
|
|
|
s = null;
|
|
|
|
|
Search search = new Search();
|
|
|
|
|
search.process(firstKey);
|
|
|
|
|
if (search.found()) {
|
|
|
|
|
Node matcher = search.getMatcher();
|
|
|
|
|
return new nodeIterator(up, rotating, matcher);
|
|
|
|
|
} else {
|
|
|
|
|
Node nn = s.getParent();
|
|
|
|
|
s = null;
|
|
|
|
|
Node nn = search.getParent();
|
|
|
|
|
if (nn == null) {
|
|
|
|
|
return (new HashSet()).iterator(); // an empty iterator
|
|
|
|
|
} else {
|
|
|
|
@ -944,22 +939,21 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized rowIterator rows(boolean up, boolean rotating) throws IOException {
|
|
|
|
|
public 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 synchronized Iterator rows(boolean up, boolean rotating, byte[] firstKey) throws IOException {
|
|
|
|
|
Search s = new Search(firstKey);
|
|
|
|
|
if (s.found()) {
|
|
|
|
|
Node matcher = s.getMatcher();
|
|
|
|
|
s = null;
|
|
|
|
|
public Iterator rows(boolean up, boolean rotating, byte[] firstKey) throws IOException {
|
|
|
|
|
Search search = new Search();
|
|
|
|
|
search.process(firstKey);
|
|
|
|
|
if (search.found()) {
|
|
|
|
|
Node matcher = search.getMatcher();
|
|
|
|
|
return new rowIterator(new nodeIterator(up, rotating, matcher));
|
|
|
|
|
} else {
|
|
|
|
|
Node nn = s.getParent();
|
|
|
|
|
s = null;
|
|
|
|
|
Node nn = search.getParent();
|
|
|
|
|
if (nn == null) {
|
|
|
|
|
return (Iterator) (new HashSet()).iterator();
|
|
|
|
|
} else {
|
|
|
|
@ -1002,15 +996,14 @@ public class kelondroTree extends kelondroRecords implements Comparator, kelondr
|
|
|
|
|
return new keyIterator(new nodeIterator(up, rotating));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized Iterator keys(boolean up, boolean rotating, byte[] firstKey) throws IOException {
|
|
|
|
|
Search s = new Search(firstKey);
|
|
|
|
|
if (s.found()) {
|
|
|
|
|
Node matcher = s.getMatcher();
|
|
|
|
|
s = null;
|
|
|
|
|
public Iterator keys(boolean up, boolean rotating, byte[] firstKey) throws IOException {
|
|
|
|
|
Search search = new Search();
|
|
|
|
|
search.process(firstKey);
|
|
|
|
|
if (search.found()) {
|
|
|
|
|
Node matcher = search.getMatcher();
|
|
|
|
|
return new keyIterator(new nodeIterator(up, rotating, matcher));
|
|
|
|
|
} else {
|
|
|
|
|
Node nn = s.getParent();
|
|
|
|
|
s = null;
|
|
|
|
|
Node nn = search.getParent();
|
|
|
|
|
if (nn == null) {
|
|
|
|
|
return (Iterator) (new HashSet()).iterator();
|
|
|
|
|
} else {
|
|
|
|
|