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 package org.openmobileis.database.fastobjectdb.db.store;
00030
00031 import java.io.*;
00032
00033 import org.openmobileis.common.util.collection.Array;
00034
00045 public class FODBMainFile {
00046 private final static int ACTION_CREATE = 0;
00047 private final static int ACTION_OPEN = 1;
00048
00049 private Array collectionTable = null;
00050 private String mainFilePath;
00051
00052 private FODBMainFile(String filePath, int action) throws IOException, ClassNotFoundException {
00053 if (action == ACTION_CREATE) {
00054 File f1 = new File(filePath);
00055 f1.delete();
00056 }
00057
00058 mainFilePath = filePath;
00059
00060
00061 if (action == ACTION_CREATE) {
00062
00063
00064 collectionTable = new Array();
00065 rewriteCollectionTable(collectionTable);
00066 } else if (action == ACTION_OPEN) {
00067 collectionTable = readCollectionTable();
00068 }
00069 }
00070
00071 public static FODBMainFile createDbFile(String filePath) throws IOException, ClassNotFoundException {
00072 return new FODBMainFile(filePath, ACTION_CREATE);
00073 }
00074
00075 public static FODBMainFile openDbFile(String filePath) throws IOException, ClassNotFoundException {
00076 File dbFile = new File(filePath);
00077 if (!dbFile.exists()) {
00078 return null;
00079 }
00080 return new FODBMainFile(filePath, ACTION_OPEN);
00081 }
00082
00083 public String addCollection(String collectionName) throws IOException {
00084 boolean modified;
00085 modified = collectionTable.add(collectionName);
00086 rewriteCollectionTable(collectionTable);
00087 return collectionName;
00088 }
00089
00090 private void rewriteCollectionTable(Array obj) throws IOException {
00091 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(mainFilePath)));
00092 try {
00093 out.writeObject(obj);
00094 } finally {
00095 out.flush();
00096 out.close();
00097 }
00098 }
00099
00100 private Array readCollectionTable() throws IOException, ClassNotFoundException {
00101 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(mainFilePath)));
00102 try {
00103 return (Array)in.readObject();
00104 } finally {
00105 in.close();
00106 }
00107 }
00108
00109 public String[] getCollectionsList() {
00110 String[] collectionsNameDuplic = new String[collectionTable.size()];
00111 collectionsNameDuplic = (String[])collectionTable.toArray(collectionsNameDuplic);
00112 return collectionsNameDuplic;
00113 }
00114 }