FODBIndex.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 org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00033 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00034 import org.openmobileis.database.fastobjectdb.db.exception.FODBQueryException;
00035 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00036 import org.openmobileis.database.fastobjectdb.db.query.soda.SodaIndexComparator;
00037 import org.openmobileis.database.fastobjectdb.db.store.FODBCollectionIndexFile;
00038 
00039 import java.io.IOException;
00040 import java.lang.reflect.*;
00041 
00042 
00050 public abstract class FODBIndex {
00051   public static final int UNIQUE = 1;
00052   public static final int MULTIPLE = 2;
00053 
00054   protected String name;
00055   protected final AccessibleObject accessObj;
00056   protected FODBCollectionIndexFile colFile;
00057   protected FODBIndexHeader header;
00058         protected int minKey;
00059 
00065   public FODBIndex(FODBIndexHeader newHeader, FODBCollectionIndexFile cFile, AccessibleObject accObj) {
00066     header = newHeader;
00067         minKey = (int) Math.ceil((double) header.descriptor.getOrder() / 2D) - 1;
00068     name = header.getName();
00069     colFile = cFile;
00070     accessObj = accObj;
00071   }
00072 
00073   public Node readRoot() throws IOException, ClassNotFoundException {
00074     return (Node) colFile.readNode(header.rootPtr);
00075   }
00076 
00084   public FODBIndex(FODBIndexDescriptor descriptor, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00085     name = descriptor.getName();
00086     colFile = cFile;
00087     accessObj = accObj;
00088     try {
00089 
00090                         // descriptor verification (specific to each index type).
00091                         specificDescriptorVerifications(descriptor);
00092       // open collection file
00093         // Logically, we should rewrite the root page as we initialised
00094         // its filePtr field.
00095         // This is not needed as FODBObjectDatabaseFile already
00096         // does it. Each time it writes an object instance of Page, it
00097         // first writes it, fill its filePtr field with the right value, and then
00098         // rewrites it.
00099         // We actually even shouldn't fill the filePtr field ourselves as
00100         // it is already done.
00101 
00102         // new header
00103         if (accessObj instanceof Method) {
00104           descriptor.setMemberName((((Method) accessObj).getName()).concat("()"));
00105           header = new FODBIndexHeader(descriptor);
00106           // we concat '()' at the end of Methods name, in order to be
00107           // able to recognise it is a method, next time we open
00108           // the index.
00109         } else { // accessObj instanceof Field
00110           descriptor.setMemberName(((Field) accessObj).getName());
00111           header = new FODBIndexHeader(descriptor);
00112         }
00113 
00114         header.descriptor.setType(this.getType()); // specific to each index type.
00115         header.rootPtr = Node.NO_NODE;
00116 
00117         // write header to file
00118         // First to get it's position
00119         long pos = colFile.writeHeader(header);
00120         // Then to store it with it's position
00121         header.pos = pos;
00122         colFile.rewriteHeader(header, pos);
00123         // Then we store it's pointer to the IndexTable
00124         colFile.addIndexPointerToTable(pos);
00125 
00126         // new root page (specific to each index type).
00127         Node root = initRoot(descriptor);
00128         // write root to collection file
00129         //update header rootPtr.
00130         root.filePtr = colFile.writeNode(root, header);
00131 
00132         //close collection file
00133     } catch (Throwable ex) {
00134       throw new FODBException(ex);
00135     }
00136                 minKey = (int) Math.ceil((double) header.descriptor.getOrder() / 2D) - 1;
00137   }
00138 
00139   public String getName() {
00140     return name;
00141   }
00142 
00143   public long getPos() {
00144     return header.pos;
00145   }
00146 
00151   public boolean isEmpty() throws FODBException {
00152     try {
00153       Node root = colFile.readNode(header.rootPtr);
00154       if (root == null) {
00155         throw new FODBException("Index closed");
00156       }
00157 
00158       return (root.nbKey == 0);
00159     } catch (Throwable ex) {
00160       throw new FODBException(ex);
00161     }
00162   }
00163 
00173   public void add(Object obj, long pos) throws FODBException {
00174         try     {
00175         Class accessObjType=getAccessObjType();
00176         if(accessObjType.getName().startsWith("[")){
00177                 //insert keys for all elements in array
00178                 Array keys=getArrayKey(obj);
00179                 for(int i=0;i<keys.size();i++){
00180                         Object key=keys.get(i);
00181                         if(key!=null){
00182                                 insertKey(key,pos);
00183                         }
00184                 }
00185         }else{
00186                 //insert key for obj
00187                 Object key=getKey(obj);
00188                 insertKey(key,pos);
00189         }
00190         } catch (Throwable ex)  {
00191                 throw new FODBException(ex);
00192         }
00193   }
00194   
00195   public Class getAccessObjType(){
00196         if(accessObj instanceof Method)
00197                 return ((Method)accessObj).getReturnType();
00198         else
00199                 return ((Field)accessObj).getType();
00200   }
00201   
00202 
00211   public boolean delObjectWithKey(Object obj, long pos) throws FODBException {
00212         try     {
00213                 boolean result=true;
00214                 Class accessObjType=getAccessObjType();
00215         if(accessObjType.getName().startsWith("[")){
00216                 //insert keys for all elements in array
00217                 Array keys=getArrayKey(obj);
00218                 for(int i=0;i<keys.size();i++){
00219                         Object key=keys.get(i);
00220                         if(key!=null){
00221                                 result=result && deleteKey(key,pos);
00222                         }
00223                 }
00224         }else{
00225                 //insert key for obj
00226                 Object key=getKey(obj);
00227                 result=deleteKey(key,pos);
00228         }
00229         return result;
00230         } catch (Throwable ex)  {
00231                 throw new FODBException(ex);
00232         }
00233   }
00234 
00235   public FODBIndexHeader getIndexHeader() {
00236     return this.header;
00237   }
00238 
00239         public abstract long[] query(SodaIndexComparator comparator) throws FODBQueryException;
00240   protected abstract void specificDescriptorVerifications(FODBIndexDescriptor descriptor) throws FODBException;
00241   protected abstract Node initRoot(FODBIndexDescriptor descriptor) throws FODBException;
00242   public abstract int getType();
00243 
00244   protected abstract void insertKey(Object key, long pos) throws IOException, ClassNotFoundException, FODBException;
00245   protected abstract boolean deleteKey(Object obj, long pos) throws IOException, ClassNotFoundException, FODBException;
00246   
00247         public abstract Object getKey(Object obj) throws FODBException;
00248         public abstract org.openmobileis.common.util.collection.Array getArrayKey(Object obj) throws FODBException;
00249     
00250   public FODBIndexDescriptor getIndexDescriptor()   {
00251     return this.header.descriptor;
00252   }
00253 
00257   public String toString() {
00258     String result = "FODBIndex[" + name + ", ";
00259 
00260     if (accessObj instanceof Method) {
00261       result = result.concat((((Method) accessObj).getName()).concat("()"));
00262     } else { // accessObj instanceof Field
00263       result = result.concat(((Field) accessObj).getName());
00264     }
00265 
00266     result =
00267       result.concat(
00268         ", "
00269           + header.getName()
00270           + ", "
00271           + header.getMemberName()
00272           + ", "
00273           + header.descriptor.getType()
00274           + ", "
00275           + header.descriptor.getOrder()
00276           + ", "
00277           + header.pos
00278           + ", "
00279           + header.rootPtr);
00280 
00281     result = result.concat("]");
00282 
00283     return result;
00284   }
00285 
00286 }

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