de-serialized read and write access

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@989 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 20 years ago
parent 09a0e898e0
commit 4fa942511b

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

Loading…
Cancel
Save