Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ForwardUniqueIndexIterator.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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)        { //empty index
00074                                 end = true;
00075                         }
00076         
00077                         //position to the first positon
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(); //position to next record.
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                         //read current obj
00123                         long objPos= currentNode.ptr[this.position];
00124                         Object ret = collection.getElementAtPos(objPos);
00125                         
00126                         //get next pos
00127                         if (currentNode.branchs[(this.position+1)] != Node.NO_NODE)     { //goes down
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 ++;  //goes next
00136                                 while (this.position >= currentNode.nbKey)      { //goes up
00137                                         long parent = currentNode.parentPtr;
00138                                         if (parent == Node.NO_NODE)     { //root node end index traversal.
00139                                                 end = true;
00140                                                 break; 
00141                                         }
00142                                         long nodepos = currentNode.filePtr;
00143                                         this.currentNode = (Node)collection.getNodeAtPos(parent);
00144                                         //child node position
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 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4