FastObjectDB.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 
00030 package org.openmobileis.database.fastobjectdb;
00031 
00032 import java.io.*;
00033 
00034 import org.odbms.Query;
00035 import org.openmobileis.database.fastobjectdb.db.CollectionManager;
00036 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00037 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00038 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00039 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00040 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00041 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaQuery;
00042 import org.openmobileis.database.fastobjectdb.db.store.FODBCollectionFile;
00043 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00044 import org.openmobileis.database.fastobjectdb.db.transaction.TransactionManager;
00045 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00046 
00047 import org.openmobileis.common.util.collection.Array;
00048 import org.openmobileis.common.util.exception.BadDataFormatException;
00049 import org.openmobileis.common.util.exception.DatabaseException;
00050 
00074 public class FastObjectDB {
00075         protected final static int ACTION_CREATE = 0;
00076         protected final static int ACTION_OPEN = 1;
00077 
00078         private String name;
00079         private String rootDir;
00080         protected CollectionManager colManager;
00081 
00082 
00083         private FODBMainFile databaseFile;
00084 
00085         private TransactionManager transactionManager;
00086 
00098         protected FastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action) throws BadDataFormatException, FODBException {
00099                 name = dbName;
00100                 rootDir = dbRootDir;
00101                 databaseFile = fmf;
00102                 colManager =new CollectionManager();
00103                 transactionManager = new TransactionManager();
00104                 if (action == ACTION_OPEN) {
00105                         String[] colList = databaseFile.getCollectionsList();
00106                         if (colList != null) {
00107                                 for (int i = 0; i < colList.length; i++) {
00108 //                                      System.out.println("" + colList[i]);
00109                                         try {
00110                                                 openCollection(colList[i]);
00111           } catch (IOException ex) {
00112             throw new FODBException("Open collection error :"+colList[i], ex);
00113           } catch (ClassNotFoundException ex) {
00114             throw new FODBException("Open collection error :"+colList[i], ex);
00115           }
00116                                 }
00117                         }
00118                 }
00119         }
00120 
00132         static public FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00133                 String completeFilePath = "" + dbRootDir + "/" + dbName + "/" + dbName + ".db";
00134                 File file = new File(completeFilePath);
00135                 FastObjectDB db = null;
00136                 if (file.exists())      {
00137                         FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00138                         db =  new FastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00139                 } else  {
00140                         File filedir = new File(dbRootDir+ "/" + dbName);
00141                         if (!filedir.exists())  {
00142                                 filedir.mkdirs();
00143                         }
00144                         FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00145                         db =  new FastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00146                 }
00147                 return db;
00148         }
00149 
00150         public String getRootDir() {
00151                 return rootDir;
00152         }
00153 
00154         public String getName() {
00155                 return name;
00156         }
00157 
00158         public FODBCollection getCollection(String name)        {
00159                 return  colManager.getCollectionByName(name);
00160         }
00161 
00162         public boolean isCollectionExist(String colName) {
00163                 String completeFilePath = "" + this.getRootDir() + "/" + this.getName() + "/" + colName + ".col";
00164                 File collect = new File(completeFilePath);
00165                 return collect.exists();
00166 
00167         }
00168 
00181         public boolean createCollection(FODBCollectionDescriptor descriptor) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00182                 if (colManager.getCollectionByName(descriptor.getCollectionName())!=null) {
00183                         return false;
00184                 }
00185                 FODBCollection newCol;
00186                 newCol = FODBCollection.createCollection(this, descriptor);
00187                 colManager.addCollection(newCol);
00188 
00189                 databaseFile.addCollection(descriptor.getCollectionName());
00190 
00191                 return true;
00192         }
00193         
00194         public void removeCollecionFile(String collectionName) throws FODBException     {
00195                 FODBCollection col = colManager.getCollectionByName(collectionName);
00196                 if (col != null)        {
00197             this.getTransactionManager().begin();
00198             try {
00199                 this.getTransactionManager().enterTransaction(collectionName);
00200                 FODBCollectionFile collectionFile = col.getCollectionFile();
00201                 if (collectionFile != null)     {
00202                         String path = collectionFile.getCollectionFilePath();
00203                 FODBCypher cypher = col.getCollectionCypher();
00204                 //remove file
00205                 FODBCollectionDescriptor desc =  collectionFile.getDescriptor();
00206                 FODBIndex[] indexList = col.getCollectionIndexList();
00207                 File dbfile = new File(path);
00208                 dbfile.delete();
00209                 colManager.removeCollection(collectionName);
00210                         FODBCollection newCol = FODBCollection.createCollection(this, desc);
00211                         colManager.addCollection(newCol);
00212                         newCol.setCollectionCypher(cypher);
00213                         for (int i=0; i<indexList.length; i++){
00214                                 this.addIndex(collectionName, indexList[i].getIndexDescriptor());
00215                         }
00216                                 if (desc.isSynchronize())       {
00217                                         // remove all sync number.
00218                                         NumSyncManagerDB.getManager().deleteSyncNumberForService(desc.getCollectionName());
00219                                 }
00220                 }
00221             } catch (Throwable ex) {
00222               throw new FODBException(ex);
00223             } finally {
00224               this.getTransactionManager().commit();
00225             }
00226                 }
00227         }
00228 
00241         public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00242                 return this.createCollection(new FODBCollectionDescriptor(collectionName, objectType));
00243         }
00244 
00256         protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00257                 if (colManager.getCollectionByName(collectionName)!=null) {
00258                         return false;
00259                 }
00260                 FODBCollection newCol;
00261                 newCol = FODBCollection.openCollection(this, collectionName);
00262                 colManager.addCollection(newCol);
00263                 return true;
00264         }
00265 
00266         public boolean isCollectionEmpty(String colName) throws DatabaseException       {
00267                 boolean ret = false;
00268                 FODBCollection collect = colManager.getCollectionByName(colName);
00269                 if (collect != null)    {
00270                         try     {
00271                                 this.getTransactionManager().begin();
00272                                 try     {
00273                                         this.getTransactionManager().enterTransaction(collect.getName());
00274                                         FODBUniqueIndex index = collect.getCollectionIdIndex();
00275                                         ret = index.isEmpty();
00276                                 } finally       {
00277                                         this.getTransactionManager().commit();
00278                                 }
00279                         } catch (Throwable ex)  {
00280                                 throw new DatabaseException(ex);
00281                         }
00282                 }
00283                 return ret;
00284         }
00285 
00295         public boolean addIndex(String colName, FODBIndexDescriptor descriptor) throws BadDataFormatException, FODBException {
00296                 FODBCollection col;
00297                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00298                         return false;
00299                 }
00300                 this.getTransactionManager().enterTransaction(col.getName());
00301                 return col.addIndex(descriptor);
00302         }
00303 
00312         public boolean add(String colName, Object obj) throws FODBException {
00313                 FODBCollection col;
00314                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00315                         return false;
00316                 }
00317 
00318                 this.getTransactionManager().enterTransaction(col.getName());
00319                 return col.addElement(obj);
00320         }
00321 
00330         public boolean replace(String colName, Object obj) throws FODBException {
00331                 FODBCollection col;
00332                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00333                         return false;
00334                 }
00335 
00336                 this.getTransactionManager().enterTransaction(col.getName());
00337                 return col.replaceElement(obj);
00338         }
00339 
00419         public Query query() {
00420                 return new FODBSodaQuery(colManager, this);
00421         }
00422 
00431         public boolean delete(String colName, Object obj) throws FODBException {
00432                 if (obj == null) throw new  FODBException("Error FastObjectDB delete object null");
00433                 FODBCollection col;
00434                 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00435                         return false;
00436                 }
00437 
00438                 this.getTransactionManager().enterTransaction(col.getName());
00439                 return col.deleteSingleElement(obj);
00440         }
00441 
00450         public boolean deleteWithId(String colName, Object id) throws FODBException {
00451                 if (id == null) throw new  FODBException("Error FastObjectDB delete object null");
00452                 FODBCollection col;
00453                 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00454                         return false;
00455                 }
00456 
00457                 this.getTransactionManager().enterTransaction(col.getName());
00458                 return col.deleteObjectWithId(id);
00459         }
00460 
00461         public String toString() {
00462                 Object[] collectionsKey = null;
00463                 String result = "FastObjectDB [" + name + ", " + rootDir + "]\n";
00464 
00465                 collectionsKey = colManager.getCollectionList().keySet().toArray();
00466                 for (int i = 0; i < collectionsKey.length; i++) {
00467                         result = result.concat("  " + (String)collectionsKey[i] + "\n" + colManager.getCollectionByName((String)collectionsKey[i]) + "\n");
00468                 }
00469                 System.out.println("");
00470 
00471                 return result;
00472         }
00473 
00474         public FODBCollection[] getDatabaseCollectionArray()    {
00475                 Array arr =  colManager.getCollectionArray();
00476                 FODBCollection[] retList = new FODBCollection[arr.size()];
00477                 arr.toArray(retList);
00478                 return retList;
00479         }
00480 
00481         public TransactionManager getTransactionManager() {
00482                 return transactionManager;
00483         }
00484 
00485 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4