1 24 25 package com.mckoi.database; 26 27 import com.mckoi.store.*; 28 import com.mckoi.debug.Lvl; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.IOException ; 32 33 41 42 class V1FileStoreSystem implements StoreSystem { 43 44 47 private static final String FLOCK_EXT = ".lock"; 48 49 53 private TransactionSystem system; 54 55 58 private File path; 59 60 63 private boolean read_only; 64 65 68 private FileOutputStream lock_file; 69 70 73 public V1FileStoreSystem(TransactionSystem system, File path, 74 boolean read_only) { 75 this.system = system; 76 this.path = path; 77 this.read_only = read_only; 78 if (!read_only && !path.exists()) { 80 path.mkdirs(); 81 } 82 } 83 84 87 private JournalledFileStore createFileStore(String file_name) 88 throws IOException { 89 LoggingBufferManager buffer_manager = system.getBufferManager(); 90 return new JournalledFileStore(file_name, buffer_manager, read_only); 91 } 92 93 95 public boolean storeExists(String name) { 96 try { 97 JournalledFileStore store = createFileStore(name); 98 return store.exists(); 99 } 100 catch (IOException e) { 101 system.Debug().writeException(e); 102 throw new RuntimeException ("IO Error: " + e.getMessage()); 103 } 104 } 105 106 public Store createStore(String name) { 107 LoggingBufferManager buffer_manager = system.getBufferManager(); 108 if (read_only) { 109 throw new RuntimeException ( 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 ("Can not create - store with name " + name + 122 " already exists."); 123 } 124 } 125 catch (IOException e) { 126 system.Debug().writeException(e); 127 throw new RuntimeException ("IO Error: " + e.getMessage()); 128 } 129 catch (InterruptedException e) { 130 throw new Error ("Interrupted: " + e.getMessage()); 131 } 132 finally { 133 buffer_manager.unlockForWrite(); 134 } 135 136 } 137 138 public Store openStore(String 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 ("Can not open - store with name " + name + 150 " does not exist."); 151 } 152 } 153 catch (IOException e) { 154 system.Debug().writeException(e); 155 throw new RuntimeException ("IO Error: " + e.getMessage()); 156 } 157 catch (InterruptedException e) { 158 throw new Error ("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 e) { 175 system.Debug().writeException(e); 176 throw new RuntimeException ("IO Error: " + e.getMessage()); 177 } 178 catch (InterruptedException e) { 179 throw new Error ("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 e) { 195 system.Debug().writeException(e); 196 throw new RuntimeException ("IO Error: " + e.getMessage()); 197 } 198 catch (InterruptedException e) { 199 throw new Error ("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 e) { 213 system.Debug().writeException(e); 214 throw new RuntimeException ("IO Error: " + e.getMessage()); 215 } 216 catch (InterruptedException e) { 217 system.Debug().writeException(e); 218 throw new RuntimeException ("Interrupted Error: " + e.getMessage()); 219 } 220 } 221 222 public void lock(String name) throws IOException { 223 File flock_fn = new File (path, name + FLOCK_EXT); 224 if (flock_fn.exists()) { 225 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 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 ("Couldn't delete conglomerate file lock."); 240 } 241 } 242 flock_fn.createNewFile(); 245 flock_fn.deleteOnExit(); 247 lock_file = new FileOutputStream (flock_fn); 250 } 251 252 public void unlock(String name) throws IOException { 253 if (lock_file != null) { 255 lock_file.close(); 256 } 257 File flock_fn = new File (path, name + FLOCK_EXT); 259 flock_fn.delete(); 260 } 261 262 } 263 264 | Popular Tags |