KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > V1FileStoreSystem


1 /**
2  * com.mckoi.database.V1FileStoreSystem 04 Feb 2003
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.store.*;
28 import com.mckoi.debug.Lvl;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * An implementation of StoreSystem that manages persistant data through the
35  * native file system. Each store is represented by a ScatteringFileStore
36  * object against the current path. This implementation is compatible with
37  * versions of the database from 0.94 onwards.
38  *
39  * @author Tobias Downer
40  */

41
42 class V1FileStoreSystem implements StoreSystem {
43
44   /**
45    * The name of the file extention of the file lock on this conglomerate.
46    */

47   private static final String JavaDoc FLOCK_EXT = ".lock";
48   
49   /**
50    * The TransactionSystem that contains the various configuration options for
51    * the database.
52    */

53   private TransactionSystem system;
54
55   /**
56    * The path in the filesystem where the data files are located.
57    */

58   private File JavaDoc path;
59
60   /**
61    * True if the stores are read-only.
62    */

63   private boolean read_only;
64
65   /**
66    * The lock file.
67    */

68   private FileOutputStream JavaDoc lock_file;
69   
70   /**
71    * Constructor.
72    */

73   public V1FileStoreSystem(TransactionSystem system, File JavaDoc path,
74                            boolean read_only) {
75     this.system = system;
76     this.path = path;
77     this.read_only = read_only;
78     // If the database path doesn't exist, create it now,
79
if (!read_only && !path.exists()) {
80       path.mkdirs();
81     }
82   }
83
84   /**
85    * Creates the JournalledFileStore object for this table.
86    */

87   private JournalledFileStore createFileStore(String JavaDoc file_name)
88                                                            throws IOException JavaDoc {
89     LoggingBufferManager buffer_manager = system.getBufferManager();
90     return new JournalledFileStore(file_name, buffer_manager, read_only);
91   }
92
93   // ---------- Implemented from StoreSystem ----------
94

95   public boolean storeExists(String JavaDoc name) {
96     try {
97       JournalledFileStore store = createFileStore(name);
98       return store.exists();
99     }
100     catch (IOException JavaDoc e) {
101       system.Debug().writeException(e);
102       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
103     }
104   }
105
106   public Store createStore(String JavaDoc name) {
107     LoggingBufferManager buffer_manager = system.getBufferManager();
108     if (read_only) {
109       throw new RuntimeException JavaDoc(
110                         "Can not create store because system is read-only.");
111     }
112     try {
113       buffer_manager.lockForWrite();
114       
115       JournalledFileStore store = createFileStore(name);
116       if (!store.exists()) {
117         store.open();
118         return store;
119       }
120       else {
121         throw new RuntimeException JavaDoc("Can not create - store with name " + name +
122                                    " already exists.");
123       }
124     }
125     catch (IOException JavaDoc e) {
126       system.Debug().writeException(e);
127       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
128     }
129     catch (InterruptedException JavaDoc e) {
130       throw new Error JavaDoc("Interrupted: " + e.getMessage());
131     }
132     finally {
133       buffer_manager.unlockForWrite();
134     }
135     
136   }
137
138   public Store openStore(String JavaDoc name) {
139     LoggingBufferManager buffer_manager = system.getBufferManager();
140     try {
141       buffer_manager.lockForWrite();
142       
143       JournalledFileStore store = createFileStore(name);
144       if (store.exists()) {
145         store.open();
146         return store;
147       }
148       else {
149         throw new RuntimeException JavaDoc("Can not open - store with name " + name +
150                                    " does not exist.");
151       }
152     }
153     catch (IOException JavaDoc e) {
154       system.Debug().writeException(e);
155       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
156     }
157     catch (InterruptedException JavaDoc e) {
158       throw new Error JavaDoc("Interrupted: " + e.getMessage());
159     }
160     finally {
161       buffer_manager.unlockForWrite();
162     }
163     
164   }
165
166   public boolean closeStore(Store store) {
167     LoggingBufferManager buffer_manager = system.getBufferManager();
168     try {
169       buffer_manager.lockForWrite();
170
171       ((JournalledFileStore) store).close();
172       return true;
173     }
174     catch (IOException JavaDoc e) {
175       system.Debug().writeException(e);
176       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
177     }
178     catch (InterruptedException JavaDoc e) {
179       throw new Error JavaDoc("Interrupted: " + e.getMessage());
180     }
181     finally {
182       buffer_manager.unlockForWrite();
183     }
184     
185   }
186
187   public boolean deleteStore(Store store) {
188     LoggingBufferManager buffer_manager = system.getBufferManager();
189     try {
190       buffer_manager.lockForWrite();
191
192       return ((JournalledFileStore) store).delete();
193     }
194     catch (IOException JavaDoc e) {
195       system.Debug().writeException(e);
196       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
197     }
198     catch (InterruptedException JavaDoc e) {
199       throw new Error JavaDoc("Interrupted: " + e.getMessage());
200     }
201     finally {
202       buffer_manager.unlockForWrite();
203     }
204     
205   }
206
207   public void setCheckPoint() {
208     try {
209       LoggingBufferManager buffer_manager = system.getBufferManager();
210       buffer_manager.setCheckPoint(false);
211     }
212     catch (IOException JavaDoc e) {
213       system.Debug().writeException(e);
214       throw new RuntimeException JavaDoc("IO Error: " + e.getMessage());
215     }
216     catch (InterruptedException JavaDoc e) {
217       system.Debug().writeException(e);
218       throw new RuntimeException JavaDoc("Interrupted Error: " + e.getMessage());
219     }
220   }
221   
222   public void lock(String JavaDoc name) throws IOException JavaDoc {
223     File JavaDoc flock_fn = new File JavaDoc(path, name + FLOCK_EXT);
224     if (flock_fn.exists()) {
225       // Okay, the file lock exists. This means either an extremely bad
226
// crash or there is another database locked on the files. If we can
227
// delete the lock then we can go on.
228
system.Debug().write(Lvl.WARNING, this,
229                            "File lock file exists: " + flock_fn);
230       boolean deleted = false;
231       deleted = flock_fn.delete();
232       if (!deleted) {
233         // If we couldn't delete, then most likely database being used.
234
System.err.println("\n" +
235             "I couldn't delete the file lock for Database '" + name + "'.\n" +
236             "This most likely means the database is open and being used by\n" +
237             "another process.\n" +
238             "The lock file is: " + flock_fn + "\n\n");
239         throw new IOException JavaDoc("Couldn't delete conglomerate file lock.");
240       }
241     }
242 //#IFDEF(NO_1.1)
243
// Atomically create the file,
244
flock_fn.createNewFile();
245     // Set it to delete on normal exit of the JVM.
246
flock_fn.deleteOnExit();
247 //#ENDIF
248
// Open up a stream and lock it in the OS
249
lock_file = new FileOutputStream JavaDoc(flock_fn);
250   }
251
252   public void unlock(String JavaDoc name) throws IOException JavaDoc {
253     // Close and delete the lock file.
254
if (lock_file != null) {
255       lock_file.close();
256     }
257     // Try and delete it
258
File JavaDoc flock_fn = new File JavaDoc(path, name + FLOCK_EXT);
259     flock_fn.delete();
260   }
261   
262 }
263
264
Popular Tags