00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00105 try {
00106 openCollection(colList[i]);
00107 } catch (IOException ex) {
00108 throw new FODBException("Open collection error :"+colList[i], ex);
00109 } catch (ClassNotFoundException ex) {
00110 throw new FODBException("Open collection error :"+colList[i], ex);
00111 }
00112 }
00113 }
00114 }
00115 }
00116
00128 static public FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00129 String completeFilePath = "" + dbRootDir + "/" + dbName + "/" + dbName + ".db";
00130 File file = new File(completeFilePath);
00131 FastObjectDB db = null;
00132 if (file.exists()) {
00133 FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00134 db = new FastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00135 } else {
00136 File filedir = new File(dbRootDir+ "/" + dbName);
00137 if (!filedir.exists()) {
00138 filedir.mkdirs();
00139 }
00140 FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00141 db = new FastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00142 }
00143 return db;
00144 }
00145
00146 public String getRootDir() {
00147 return rootDir;
00148 }
00149
00150 public String getName() {
00151 return name;
00152 }
00153
00154 public FODBCollection getCollection(String name) {
00155 return colManager.getCollectionByName(name);
00156 }
00157
00158 public boolean isCollectionExist(String colName) {
00159 String completeFilePath = "" + this.getRootDir() + "/" + this.getName() + "/" + colName + ".col";
00160 File collect = new File(completeFilePath);
00161 return collect.exists();
00162
00163 }
00164
00177 public boolean createCollection(FODBCollectionDescriptor descriptor) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00178 if (colManager.getCollectionByName(descriptor.getCollectionName())!=null) {
00179 return false;
00180 }
00181 FODBCollection newCol;
00182 newCol = FODBCollection.createCollection(this, descriptor);
00183 colManager.addCollection(newCol);
00184
00185 databaseFile.addCollection(descriptor.getCollectionName());
00186
00187 return true;
00188 }
00189
00202 public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00203 return this.createCollection(new FODBCollectionDescriptor(collectionName, objectType));
00204 }
00205
00217 protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00218 if (colManager.getCollectionByName(collectionName)!=null) {
00219 return false;
00220 }
00221 FODBCollection newCol;
00222 newCol = FODBCollection.openCollection(this, collectionName);
00223 colManager.addCollection(newCol);
00224 return true;
00225 }
00226
00227 public boolean isCollectionEmpty(String colName) throws DatabaseException {
00228 boolean ret = false;
00229 FODBCollection collect = colManager.getCollectionByName(colName);
00230 if (collect != null) {
00231 try {
00232 this.getTransactionManager().begin();
00233 try {
00234 this.getTransactionManager().enterTransaction(collect.getName());
00235 FODBUniqueIndex index = collect.getCollectionIdIndex();
00236 ret = index.isEmpty();
00237 } finally {
00238 this.getTransactionManager().commit();
00239 }
00240 } catch (Throwable ex) {
00241 throw new DatabaseException(ex);
00242 }
00243 }
00244 return ret;
00245 }
00246
00256 public boolean addIndex(String colName, FODBIndexDescriptor descriptor) throws BadDataFormatException, FODBException {
00257 FODBCollection col;
00258 if ( (col = colManager.getCollectionByName(colName)) == null) {
00259 return false;
00260 }
00261 this.getTransactionManager().enterTransaction(col.getName());
00262 return col.addIndex(descriptor);
00263 }
00264
00273 public boolean add(String colName, Object obj) throws FODBException {
00274 FODBCollection col;
00275 if ( (col = colManager.getCollectionByName(colName)) == null) {
00276 return false;
00277 }
00278
00279 this.getTransactionManager().enterTransaction(col.getName());
00280 return col.addElement(obj);
00281 }
00282
00291 public boolean replace(String colName, Object obj) throws FODBException {
00292 FODBCollection col;
00293 if ( (col = colManager.getCollectionByName(colName)) == null) {
00294 return false;
00295 }
00296
00297 this.getTransactionManager().enterTransaction(col.getName());
00298 return col.replaceElement(obj);
00299 }
00300
00380 public Query query() {
00381 return new FODBSodaQuery(colManager, this);
00382 }
00383
00392 public boolean delete(String colName, Object obj) throws FODBException {
00393 if (obj == null) throw new FODBException("Error FastObjectDB delete object null");
00394 FODBCollection col;
00395 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00396 return false;
00397 }
00398
00399 this.getTransactionManager().enterTransaction(col.getName());
00400 return col.deleteSingleElement(obj);
00401 }
00402
00411 public boolean deleteWithId(String colName, Object id) throws FODBException {
00412 if (id == null) throw new FODBException("Error FastObjectDB delete object null");
00413 FODBCollection col;
00414 if ( (col = (FODBCollection) colManager.getCollectionByName(colName)) == null) {
00415 return false;
00416 }
00417
00418 this.getTransactionManager().enterTransaction(col.getName());
00419 return col.deleteObjectWithId(id);
00420 }
00421
00422 public String toString() {
00423 Object[] collectionsKey = null;
00424 String result = "FastObjectDB [" + name + ", " + rootDir + "]\n";
00425
00426 collectionsKey = colManager.getCollectionList().keySet().toArray();
00427 for (int i = 0; i < collectionsKey.length; i++) {
00428 result = result.concat(" " + (String)collectionsKey[i] + "\n" + colManager.getCollectionByName((String)collectionsKey[i]) + "\n");
00429 }
00430 System.out.println("");
00431
00432 return result;
00433 }
00434
00435 public FODBCollection[] getDatabaseCollectionArray() {
00436 Array arr = colManager.getCollectionArray();
00437 FODBCollection[] retList = new FODBCollection[arr.size()];
00438 arr.toArray(retList);
00439 return retList;
00440 }
00441
00442 public TransactionManager getTransactionManager() {
00443 return transactionManager;
00444 }
00445
00446 }