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

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