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

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