00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.openmobileis.database.fastobjectdb.db;
00029
00030 import java.io.IOException;
00031
00032
00033 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00034 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00035 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00036 import org.openmobileis.database.fastobjectdb.db.index.SearchResult;
00037 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00038
00047 public final class ForwardUniqueIndexIterator {
00048 private FODBCollection collection;
00049 private Node currentNode;
00050 private int position = 0;
00051 private boolean end = false;
00052 private FODBIndex index;
00053 private boolean inited = false;
00054
00058 ForwardUniqueIndexIterator(FODBIndex index, FODBCollection collection) throws IOException, ClassNotFoundException, FODBException {
00059 super();
00060 if (!(index instanceof FODBUniqueIndex)) {
00061 throw new FODBException("Not unique index to iterator awailable");
00062 }
00063 this.index = index;
00064 this.collection = collection;
00065 }
00066
00067 private void initIndex() throws FODBException {
00068 collection.getDatabase().getTransactionManager().begin();
00069 end = false;
00070 try {
00071 collection.getDatabase().getTransactionManager().enterTransaction(this.collection.getName());
00072 this.currentNode = index.readRoot();
00073 if (this.currentNode.nbKey == 0) {
00074 end = true;
00075 }
00076
00077
00078 long childBranch = currentNode.branchs[this.position];
00079 while (childBranch != Node.NO_NODE) {
00080 this.currentNode = (Node)collection.getNodeAtPos(childBranch);
00081 this.position = 0;
00082 childBranch = currentNode.branchs[this.position];
00083 }
00084 } catch (Throwable ex) {
00085 throw new FODBException(ex);
00086 } finally {
00087 collection.getDatabase().getTransactionManager().commit();
00088 }
00089 inited = true;
00090 }
00091
00092 public void initCurrentNode(Object indexKey) throws FODBException {
00093 inited = false;
00094 collection.getDatabase().getTransactionManager().begin();
00095 try {
00096 collection.getDatabase().getTransactionManager().enterTransaction(this.collection.getName());
00097 SearchResult result = ((FODBUniqueIndex)index).getNodeForKey(indexKey);
00098 if (result.found) {
00099 this.currentNode = result.node;
00100 this.position = result.pos;
00101 end = false;
00102 inited = true;
00103 }
00104 this.next();
00105 } finally {
00106 collection.getDatabase().getTransactionManager().commit();
00107 }
00108
00109 }
00110
00111 public boolean hasNext() throws FODBException {
00112 return !end;
00113 }
00114
00115 public Object next() throws FODBException {
00116 if (!inited) this.initIndex();
00117 if (end) return null;
00118 collection.getDatabase().getTransactionManager().begin();
00119 try {
00120 collection.getDatabase().getTransactionManager().enterTransaction(this.collection.getName());
00121
00122
00123 long objPos= currentNode.ptr[this.position];
00124 Object ret = collection.getElementAtPos(objPos);
00125
00126
00127 if (currentNode.branchs[(this.position+1)] != Node.NO_NODE) {
00128 long childBranch = currentNode.branchs[this.position+1];
00129 while (childBranch != Node.NO_NODE) {
00130 this.currentNode = (Node)collection.getNodeAtPos(childBranch);
00131 this.position = 0;
00132 childBranch = currentNode.branchs[this.position];
00133 }
00134 } else {
00135 this.position ++;
00136 while (this.position >= currentNode.nbKey) {
00137 long parent = currentNode.parentPtr;
00138 if (parent == Node.NO_NODE) {
00139 end = true;
00140 break;
00141 }
00142 long nodepos = currentNode.filePtr;
00143 this.currentNode = (Node)collection.getNodeAtPos(parent);
00144
00145 int i=0;
00146 for (; i<this.currentNode.branchs.length; i++) {
00147 if (nodepos == this.currentNode.branchs[i]) break;
00148 }
00149 this.position = i;
00150 }
00151 }
00152
00153 return ret;
00154 } finally {
00155 collection.getDatabase().getTransactionManager().commit();
00156 }
00157
00158 }
00159
00160 }