FODBMultipleIntIndex.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.db.index;
00030 
00031 import java.io.IOException;
00032 import java.lang.reflect.AccessibleObject;
00033 
00034 import org.openmobileis.common.util.collection.LongArray;
00035 import org.openmobileis.database.fastobjectdb.FODBIntIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00037 import org.openmobileis.database.fastobjectdb.db.index.node.IntNode;
00038 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00039 import org.openmobileis.database.fastobjectdb.db.store.FODBCollectionIndexFile;
00040 
00041 
00044 public class FODBMultipleIntIndex extends FODBIntIndex implements FODBMultipleIndex {
00045 
00046         public FODBMultipleIntIndex(FODBIndexHeader newHeader, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00047                 super(newHeader, cFile, accObj);
00048         }
00049         
00050         public FODBMultipleIntIndex(FODBIntIndexDescriptor descriptor, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00051                 super(descriptor, cFile, accObj);
00052         }
00053         
00054         /* (non-Javadoc)
00055          * @see org.openmobileis.embedded.fastobjectdb.db.FODBIndex#getType()
00056          */
00057         public int getType() {
00058                 return FODBIndex.MULTIPLE;
00059         }
00060 
00064         protected void writeKeyPtr(Node pg, int pos, long newptr) throws IOException, ClassNotFoundException {
00065           long arrayPtr = pg.ptr[pos];
00066           long[] ptrList = (long[])colFile.readLongPtr(arrayPtr);
00067           int size = ptrList.length;
00068 
00069           int i = 0;
00070           for (i=0; i<size; i++)  {
00071                 if (ptrList[i] == Node.NO_NODE) {
00072                         break;
00073                 }
00074           }
00075           if (i == size) { // increment +indexPosTabSize
00076                 long[] newptrlist = new long[size+((FODBIntIndexDescriptor)header.descriptor).getIncTabSize()];
00077                 System.arraycopy(ptrList, 0, newptrlist, 0, ptrList.length);
00078                 newptrlist[size] = newptr;
00079                 for (i=size+1; i<newptrlist.length; i++)  {
00080                         newptrlist[i] = Node.NO_NODE;
00081                 }
00082                 colFile.deleteLongPtr(arrayPtr);
00083                 long modified = colFile.writeLongPtr(newptrlist);
00084                 pg.ptr[pos] = modified;
00085                 colFile.rewriteNode(pg, pg.filePtr);
00086                 return;
00087           } else  {
00088                 ptrList[i] = newptr;
00089                 colFile.rewriteLongPtr(ptrList, arrayPtr);
00090                 return;
00091           }
00092 
00093         }
00094 
00095         protected long createPtrArray(long ptr) throws IOException, ClassNotFoundException {
00096           long[] newptrlist = new long[((FODBIntIndexDescriptor)header.descriptor).getIncTabSize()];
00097           newptrlist[0] = ptr;
00098           for (int i=1; i<newptrlist.length; i++)  {
00099                 newptrlist[i] = Node.NO_NODE;
00100           }
00101           return colFile.writeLongPtr(newptrlist);
00102         }
00103         
00104         protected void addSearchResult(IntNode pg, int pos, LongArray array) throws IOException, ClassNotFoundException {
00105                 long[] ptrList = this.readPtrAtPos(pg, pos);
00106                 array.add(ptrList);
00107         }
00108         
00109         private long[] readPtrAtPos(Node pg, int pos) throws IOException, ClassNotFoundException {
00110           long arrayPtr = pg.ptr[pos];
00111           
00112           long[] arrayList = (long[])colFile.readLongPtr(arrayPtr);
00113           // remove null;
00114           int i = 0;
00115           for (i=0; i<arrayList.length; i++)  {
00116                 if (arrayList[i] == Node.NO_NODE) {
00117                   break;
00118                 }
00119           }
00120           if (i==arrayList.length)  {
00121                 return arrayList;
00122           }
00123           long[] newArrayList = new long[i];
00124           System.arraycopy(arrayList, 0, newArrayList, 0, newArrayList.length);
00125           return newArrayList;
00126         }
00127         
00132         protected boolean removeKeyPtr(Node pg, int pos, long pointer) throws IOException, ClassNotFoundException {
00133           long arrayPtr = pg.ptr[pos];
00134           long[] ptrList = (long[])colFile.readLongPtr(arrayPtr);
00135 
00136           // find pointer position
00137           int i=0;
00138           int size = ptrList.length;
00139           for (i=0; i<size; i++)  {
00140                 if (ptrList[i] == pointer)  {
00141                   ptrList[i] = Node.NO_NODE;
00142                   break;
00143                 }
00144           }
00145           if (i == size)  { // Pointer not found
00146                         throw new IOException("Pointer not found !!!");
00147           }
00148           int nbnull = 1;
00149           for (int j=i+1; j<size ; j++) {
00150                 if (ptrList[j] == Node.NO_NODE) {
00151                   nbnull++;
00152                 } else  {
00153                   ptrList[j-1] = ptrList[j];
00154                   ptrList[j] = Node.NO_NODE;
00155                 }
00156           }
00157       int inctabsize = ((FODBIntIndexDescriptor)header.descriptor).getIncTabSize();
00158           if (nbnull == inctabsize)  {  //decrease
00159                 if (size > inctabsize) {
00160                   long[] newptrlist = new long[size - inctabsize];
00161                   System.arraycopy(ptrList, 0, newptrlist, 0, newptrlist.length);
00162                   colFile.deleteLongPtr(arrayPtr);
00163                   pg.ptr[pos] = colFile.writeLongPtr(newptrlist);
00164                   // write modified page
00165                   colFile.rewriteNode(pg, pg.filePtr);
00166                   return false;
00167                 } else  {     // is empty
00168                   colFile.deleteLongPtr(arrayPtr);
00169                   return true;
00170                 }
00171           }
00172           // nbnull < header.incTabSize
00173           colFile.rewriteLongPtr(ptrList, arrayPtr);
00174           return false;
00175         }       
00176 
00177 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1