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

FODBMultipleIntIndex.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  *  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.FODBIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FODBIntIndexDescriptor;
00037 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00038 import org.openmobileis.database.fastobjectdb.db.index.node.IntNode;
00039 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00040 import org.openmobileis.database.fastobjectdb.db.store.FODBCollectionIndexFile;
00041 
00042 
00045 public class FODBMultipleIntIndex extends FODBIntIndex implements FODBMultipleIndex {
00046 
00047         public FODBMultipleIntIndex(FODBIndexHeader newHeader, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00048                 super(newHeader, cFile, accObj);
00049         }
00050         
00051         public FODBMultipleIntIndex(FODBIntIndexDescriptor descriptor, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00052                 super(descriptor, cFile, accObj);
00053                 header.incTabSize = descriptor.getIncTabSize();
00054         }
00055 
00056         protected void specificHeaderInit(FODBIndexDescriptor descriptor) throws FODBException {
00057                 super.specificHeaderInit(descriptor);
00058                 FODBIntIndexDescriptor desc = (FODBIntIndexDescriptor) descriptor;
00059                 header.incTabSize = desc.getIncTabSize();
00060         }
00061         
00062         /* (non-Javadoc)
00063          * @see org.openmobileis.embedded.fastobjectdb.db.FODBIndex#getType()
00064          */
00065         public int getType() {
00066                 return FODBIndex.MULTIPLE;
00067         }
00068 
00072         protected void writeKeyPtr(Node pg, int pos, long newptr) throws IOException, ClassNotFoundException {
00073           long arrayPtr = pg.ptr[pos];
00074           long[] ptrList = (long[])colFile.readLongPtr(arrayPtr);
00075           int size = ptrList.length;
00076 
00077           int i = 0;
00078           for (i=0; i<size; i++)  {
00079                 if (ptrList[i] == Node.NO_NODE) {
00080                         break;
00081                 }
00082           }
00083           if (i == size) { // increment +indexPosTabSize
00084                 long[] newptrlist = new long[size+header.incTabSize];
00085                 System.arraycopy(ptrList, 0, newptrlist, 0, ptrList.length);
00086                 newptrlist[size] = newptr;
00087                 for (i=size+1; i<newptrlist.length; i++)  {
00088                         newptrlist[i] = Node.NO_NODE;
00089                 }
00090                 colFile.deleteLongPtr(arrayPtr);
00091                 long modified = colFile.writeLongPtr(newptrlist);
00092                 pg.ptr[pos] = modified;
00093                 colFile.rewriteNode(pg, pg.filePtr);
00094                 return;
00095           } else  {
00096                 ptrList[i] = newptr;
00097                 colFile.rewriteLongPtr(ptrList, arrayPtr);
00098                 return;
00099           }
00100 
00101         }
00102 
00103         protected long createPtrArray(long ptr) throws IOException, ClassNotFoundException {
00104           long[] newptrlist = new long[header.incTabSize];
00105           newptrlist[0] = ptr;
00106           for (int i=1; i<newptrlist.length; i++)  {
00107                 newptrlist[i] = Node.NO_NODE;
00108           }
00109           return colFile.writeLongPtr(newptrlist);
00110         }
00111         
00112         protected void addSearchResult(IntNode pg, int pos, LongArray array) throws IOException, ClassNotFoundException {
00113                 long[] ptrList = this.readPtrAtPos(pg, pos);
00114                 array.add(ptrList);
00115         }
00116         
00117         private long[] readPtrAtPos(Node pg, int pos) throws IOException, ClassNotFoundException {
00118           long arrayPtr = pg.ptr[pos];
00119           
00120           long[] arrayList = (long[])colFile.readLongPtr(arrayPtr);
00121           // remove null;
00122           int i = 0;
00123           for (i=0; i<arrayList.length; i++)  {
00124                 if (arrayList[i] == Node.NO_NODE) {
00125                   break;
00126                 }
00127           }
00128           if (i==arrayList.length)  {
00129                 return arrayList;
00130           }
00131           long[] newArrayList = new long[i];
00132           System.arraycopy(arrayList, 0, newArrayList, 0, newArrayList.length);
00133           return newArrayList;
00134         }
00135         
00140         protected boolean removeKeyPtr(Node pg, int pos, long pointer) throws IOException, ClassNotFoundException {
00141           long arrayPtr = pg.ptr[pos];
00142           long[] ptrList = (long[])colFile.readLongPtr(arrayPtr);
00143 
00144           // find pointer position
00145           int i=0;
00146           int size = ptrList.length;
00147           for (i=0; i<size; i++)  {
00148                 if (ptrList[i] == pointer)  {
00149                   ptrList[i] = Node.NO_NODE;
00150                   break;
00151                 }
00152           }
00153           if (i == size)  { // Pointer not found
00154                         throw new IOException("Pointer not found !!!");
00155           }
00156           int nbnull = 1;
00157           for (int j=i+1; j<size ; j++) {
00158                 if (ptrList[j] == Node.NO_NODE) {
00159                   nbnull++;
00160                 } else  {
00161                   ptrList[j-1] = ptrList[j];
00162                   ptrList[j] = Node.NO_NODE;
00163                 }
00164           }
00165           if (nbnull == header.incTabSize)  {  //decrease
00166                 if (size > header.incTabSize) {
00167                   long[] newptrlist = new long[size - header.incTabSize];
00168                   System.arraycopy(ptrList, 0, newptrlist, 0, newptrlist.length);
00169                   colFile.deleteLongPtr(arrayPtr);
00170                   pg.ptr[pos] = colFile.writeLongPtr(newptrlist);
00171                   // write modified page
00172                   colFile.rewriteNode(pg, pg.filePtr);
00173                   return false;
00174                 } else  {     // is empty
00175                   colFile.deleteLongPtr(arrayPtr);
00176                   return true;
00177                 }
00178           }
00179           // nbnull < header.incTabSize
00180           colFile.rewriteLongPtr(ptrList, arrayPtr);
00181           return false;
00182         }       
00183 
00184         public FODBIndexDescriptor getIndexDescriptor() {
00185                 return new FODBIntIndexDescriptor(this.header.getName(), FODBIndexDescriptor.MULTIPLE, this.header.getMemberName(), this.header.order, this.header.incTabSize);
00186         }
00187 
00188 
00189 }

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