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 collectionTable.add(collectionName);
00085 rewriteCollectionTable(collectionTable);
00086 return collectionName;
00087 }
00088
00089 private void rewriteCollectionTable(Array obj) throws IOException {
00090 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(mainFilePath)));
00091 try {
00092 out.writeObject(obj);
00093 } finally {
00094 out.flush();
00095 out.close();
00096 }
00097 }
00098
00099 private Array readCollectionTable() throws IOException, ClassNotFoundException {
00100 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(mainFilePath)));
00101 try {
00102 return (Array)in.readObject();
00103 } finally {
00104 in.close();
00105 }
00106 }
00107
00108 public String[] getCollectionsList() {
00109 String[] collectionsNameDuplic = new String[collectionTable.size()];
00110 collectionsNameDuplic = (String[])collectionTable.toArray(collectionsNameDuplic);
00111 return collectionsNameDuplic;
00112 }
00113 }