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

FastObjectDB.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  
00030 package org.openmobileis.database.fastobjectdb;
00031 
00032 import java.io.*;
00033 
00034 import org.odbms.Query;
00035 import org.openmobileis.database.DatabaseException;
00036 import org.openmobileis.database.fastobjectdb.db.CollectionManager;
00037 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00038 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00039 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00040 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaQuery;
00041 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00042 import org.openmobileis.database.fastobjectdb.db.transaction.TransactionManager;
00043 
00044 import org.openmobileis.common.util.collection.Array;
00045 import org.openmobileis.common.util.exception.BadDataFormatException;
00046 
00070 public class FastObjectDB {
00071         protected final static int ACTION_CREATE = 0;
00072         protected final static int ACTION_OPEN = 1;
00073         
00074         private String name;
00075         private String rootDir;
00076         protected CollectionManager colManager;
00077         
00078         
00079         private FODBMainFile databaseFile;
00080         
00081         private TransactionManager transactionManager;
00082 
00094         protected FastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action) throws BadDataFormatException, FODBException {
00095                 name = dbName;
00096                 rootDir = dbRootDir;
00097                 databaseFile = fmf;
00098                 colManager =new CollectionManager();
00099                 transactionManager = new TransactionManager();
00100                 if (action == ACTION_OPEN) {
00101                         String[] colList = databaseFile.getCollectionsList();
00102                         if (colList != null) {
00103                                 for (int i = 0; i < colList.length; i++) {
00104 //                                      System.out.println("" + colList[i]);
00105                                         try {
00106                                                 openCollection(colList[i]);
00107                                         } catch (IOException ex) {
00108                                         } catch (ClassNotFoundException ex) {
00109                                         }
00110                                 }
00111                         }
00112                 }
00113         }
00114         
00126         static public FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00127                 String completeFilePath = "" + dbRootDir + "/" + dbName + "/" + dbName + ".db";
00128                 File file = new File(completeFilePath);
00129                 FastObjectDB db = null;
00130                 if (file.exists())      {
00131                         FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00132                         db =  new FastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00133                 } else  {
00134                         File filedir = new File(dbRootDir+ "/" + dbName);
00135                         if (!filedir.exists())  {
00136                                 filedir.mkdirs();
00137                         }                       
00138                         FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00139                         db =  new FastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00140                 }
00141                 return db;
00142         }
00143         
00144         public String getRootDir() {
00145                 return rootDir;
00146         }
00147         
00148         public String getName() {
00149                 return name;
00150         }
00151         
00152         public FODBCollection getCollection(String name)        {
00153                 return  colManager.getCollectionByName(name);
00154         }
00155 
00156         public boolean isCollectionExist(String colName) {
00157                 String completeFilePath = "" + this.getRootDir() + "/" + this.getName() + "/" + colName + ".col";
00158                 File collect = new File(completeFilePath);
00159                 return collect.exists();
00160 
00161         }
00174         public boolean createCollection(String collectionName, Class objectType, boolean synchro) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00175                 if (colManager.getCollectionByName(collectionName)!=null) {
00176                         return false;
00177                 }
00178                 FODBCollection newCol;
00179                 newCol = FODBCollection.createCollection(this, collectionName, objectType);
00180                 colManager.addCollection(newCol);
00181                 
00182                 databaseFile.addCollection(collectionName);
00183                 
00184                 return true;
00185         }
00186         
00199         public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00200                 return this.createCollection(collectionName, objectType, true);
00201         }
00202         
00214         protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00215                 if (colManager.getCollectionByName(collectionName)!=null) {
00216                         return false;
00217                 }
00218                 FODBCollection newCol;
00219                 newCol = FODBCollection.openCollection(this, collectionName);
00220                 colManager.addCollection(newCol);
00221                 return true;
00222         }
00223         
00224         public boolean isCollectionEmpty(String colName) throws DatabaseException       {
00225                 boolean ret = false;
00226                 FODBCollection collect = colManager.getCollectionByName(colName);
00227                 if (collect != null)    {
00228                         try     {
00229                                 this.getTransactionManager().begin();
00230                                 try     {
00231                                         this.getTransactionManager().enterTransaction(collect.getName());
00232                                         FODBUniqueIndex index = collect.getCollectionIdIndex();
00233                                         ret = index.isEmpty();
00234                                 } finally       {
00235                                         this.getTransactionManager().commit();
00236                                 }
00237                         } catch (Throwable ex)  {
00238                                 throw new DatabaseException(ex);
00239                         }                       
00240                 }
00241                 return ret;
00242         }
00243         
00253         public boolean addIndex(String colName, FODBIndexDescriptor descriptor) throws BadDataFormatException, FODBException {
00254                 FODBCollection col;
00255                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00256                         return false;
00257                 }
00258                 this.getTransactionManager().enterTransaction(col.getName());
00259                 return col.addIndex(descriptor);
00260         }
00261         
00270         public boolean add(String colName, Object obj) throws FODBException {
00271                 FODBCollection col;
00272                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00273                         return false;
00274                 }
00275                 
00276                 this.getTransactionManager().enterTransaction(col.getName());
00277                 return col.addElement(obj);
00278         }
00279         
00288         public boolean replace(String colName, Object obj) throws FODBException {
00289                 FODBCollection col;
00290                 if ( (col = colManager.getCollectionByName(colName)) == null) {
00291                         return false;
00292                 }
00293                 
00294                 this.getTransactionManager().enterTransaction(col.getName());
00295                 return col.replaceElement(obj);
00296         }
00297 
00377         public Query query() {
00378                 return new FODBSodaQuery(colManager, this);
00379         }
00380         
00389         public boolean delete(String colName, Object obj) throws FODBException {
00390                 if (obj == null) throw new  FODBException("Error FastObjectDB delete object null");
00391                 FODBCollection col;
00392                 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00393                         return false;
00394                 }
00395                 
00396                 this.getTransactionManager().enterTransaction(col.getName());
00397                 return col.deleteSingleElement(obj);
00398         }
00399         
00408         public boolean deleteWithId(String colName, Object id) throws FODBException {
00409                 if (id == null) throw new  FODBException("Error FastObjectDB delete object null");
00410                 FODBCollection col;
00411                 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00412                         return false;
00413                 }
00414                 
00415                 this.getTransactionManager().enterTransaction(col.getName());
00416                 return col.deleteObjectWithId(id);
00417         }
00418         
00419         public String toString() {
00420                 Object[] collectionsKey = null;
00421                 String result = "FastObjectDB [" + name + ", " + rootDir + "]\n";
00422                 
00423                 collectionsKey = colManager.getCollectionList().keySet().toArray();
00424                 for (int i = 0; i < collectionsKey.length; i++) {
00425                         result = result.concat("  " + (String)collectionsKey[i] + "\n" + colManager.getCollectionByName((String)collectionsKey[i]) + "\n");
00426                 }
00427                 System.out.println("");
00428                 
00429                 return result;
00430         }
00431         
00432         public FODBCollection[] getDatabaseCollectionArray()    {
00433                 Array arr =  colManager.getCollectionArray();
00434                 FODBCollection[] retList = new FODBCollection[arr.size()];
00435                 arr.toArray(retList);
00436                 return retList;
00437         }
00438 
00439         public TransactionManager getTransactionManager() {
00440                 return transactionManager;
00441         }
00442 
00443 }

Generated on Thu Oct 6 10:06:32 2005 for OpenMobileIS by  doxygen 1.4.3