1 24 package org.objectweb.jalisto.se.storage.raf.log.synraf; 25 26 import org.objectweb.jalisto.se.api.internal.JalistoObject; 27 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess; 28 import org.objectweb.jalisto.se.api.JalistoProperties; 29 import org.objectweb.jalisto.se.exception.JalistoException; 30 import org.objectweb.jalisto.se.impl.InFileAddress; 31 import org.objectweb.jalisto.se.JalistoFactory; 32 import org.objectweb.jalisto.se.impl.trace.Trace; 33 import org.objectweb.jalisto.se.storage.raf.*; 34 import org.objectweb.jalisto.se.storage.raf.log.RecordsFileLog; 35 import org.objectweb.jalisto.se.storage.raf.log.synraf.action.LogAction; 36 37 import java.io.*; 38 import java.util.Collection ; 39 40 public class PhysicalFileAccessLogSynrafImpl implements PluggablePhysicalFileAccess { 41 42 private void init(int initialSize, JalistoProperties properties) 43 throws IOException, RecordsFileException { 44 int nbrDbFiles = properties.getInstanceDbFileNumber(); 45 if (nbrDbFiles > 1) { 46 files = new RecordsFileLog[nbrDbFiles + 2]; 47 files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), properties.isCleanLog()); 48 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 49 files[1] = new RecordsFileLog(properties.getOidDbFilePath(), properties.isCleanLog()); 50 files[1].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 51 52 for (int i = 0; i < nbrDbFiles; i++) { 53 files[i + 2] = new RecordsFileLog(properties.getInstanceDbFilePathAt(i), properties.isCleanLog()); 54 files[i + 2].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 55 } 56 } else { 57 files = new RecordsFileLog[1]; 58 files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), properties.isCleanLog()); 59 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 60 } 61 } 62 63 private void init(String accessFlags, JalistoProperties properties) 64 throws IOException, RecordsFileException { 65 int nbrDbFiles = properties.getInstanceDbFileNumber(); 66 if (nbrDbFiles > 1) { 67 files = new RecordsFileLog[nbrDbFiles + 2]; 68 files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), 69 accessFlags, 70 properties.isCleanLog()); 71 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 72 files[1] = new RecordsFileLog(properties.getOidDbFilePath(), 73 accessFlags, 74 properties.isCleanLog()); 75 files[1].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 76 77 for (int i = 0; i < nbrDbFiles; i++) { 78 files[i + 2] = new RecordsFileLog(properties.getInstanceDbFilePathAt(i), 79 accessFlags, 80 properties.isCleanLog()); 81 files[i + 2].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 82 } 83 } else { 84 files = new RecordsFileLog[1]; 85 files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), 86 accessFlags, 87 properties.isCleanLog()); 88 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 89 } 90 } 91 92 public void init(JalistoProperties properties) { 93 try { 94 String dbPath = properties.getDbFileFullName(); 95 File f = new File(dbPath); 96 this.isNewBase = (!f.exists()); 97 trace = JalistoFactory.getInternalFactory().getTracer(properties); 98 if (isNewBase && properties.isReadOnlyImplementation()) { 99 throw new JalistoException("cannot create a database instance in read only mode"); 100 } 101 if (isNewBase) { 102 init(properties.getInitialSize(), properties); 103 } else { 104 init("rw", properties); 105 } 106 for (int i = 0; i < files.length; i++) { 107 files[i].setTrace(trace); 108 } 109 logFile = new RandomAccessFile(properties.getLogFileName(), "rw"); 110 trace.println(Trace.PHYSICAL, "new instance with {0} underlying files", new Integer (files.length)); 111 } catch (Exception e) { 112 throw new JalistoException(e); 113 } 114 } 115 116 public boolean isNewBase() { 117 return isNewBase; 118 } 119 120 public void reorganize() { 121 trace.println(Trace.PHYSICAL, "synraf reorganize : "); 122 for (int i = 0; i < files.length; i++) { 123 try { 124 files[i].reorganize(); 125 } catch (Exception e) { 126 throw new JalistoException( 127 "error during reorganization of a physical store file : " + files[i].toString(), e); 128 } 129 } 130 } 131 132 public Collection getKeysStartingWith(String filter) { 133 trace.println(Trace.PHYSICAL, "synraf getKeysStartingWith : filter = {0}", filter); 134 if (files.length > 1) { 135 Collection result = files[0].getKeysStartingWith(filter); 136 for (short i = 1; i < files.length; i++) { 137 result.addAll(files[i].getKeysStartingWith(filter)); 138 } 139 return result; 140 } else { 141 return files[0].getKeysStartingWith(filter); 142 } 143 } 144 145 public void insertFileObject(InFileAddress ifa, JalistoObject fo) { 146 trace.println(Trace.PHYSICAL, "synraf insertFileObject({0}, {1})", ifa, fo); 147 int i = 0; 148 try { 149 RecordWriter rw = new RecordWriter(ifa.getAddress()); 150 rw.writeObject(fo); 151 if (files.length > 1) { 152 i = ifa.getFileIndex(); 153 } 154 155 LogAction logAction = new LogAction(LogAction.INSERT_ACTION, ifa, fo); 156 157 files[i].startBackUp(); 158 logAction.writeTo(logFile); 159 files[i].insertRecord(rw); 160 files[i].finishBackUp(); 161 rw.reset(); 162 } catch (Exception e) { 163 Object v; 164 try { 165 RecordReader rr = files[i].readRecord(ifa.getAddress()); 166 v = rr.readObject(); 167 rr.reset(); 168 } catch (Exception e2) { 169 throw new JalistoException("could not insert object " + fo, e); 170 } 171 throw new JalistoException("could not insert object " + fo + 172 ";\n\t\tvalue already in base : " + String.valueOf(v), e); 173 } 174 } 175 176 public JalistoObject readFileObjectAt(InFileAddress ifa) { 177 trace.println(Trace.PHYSICAL, "synraf readFileObjectAt : ifa = {0}", ifa); 178 JalistoObject result; 179 int i = 0; 180 try { 181 if (files.length > 1) { 182 i = ifa.getFileIndex(); 183 } 184 RecordReader rr = files[i].readRecord(ifa.getAddress()); 185 result = (JalistoObject) rr.readObject(); 186 result.setIfa(ifa); 187 rr.reset(); 188 } catch (Exception e) { 189 throw new JalistoException("could not read object at " + ifa, e); 190 } 191 return result; 192 } 193 194 public void updateFileObject(InFileAddress ifa, JalistoObject fo) { 195 trace.println(Trace.PHYSICAL, "synraf updateFileObject({0}, {1})", ifa, fo); 196 try { 197 RecordWriter rw = new RecordWriter(ifa.getAddress()); 198 rw.writeObject(fo); 199 int i = 0; 200 if (files.length > 1) { 201 i = ifa.getFileIndex(); 202 } 203 204 RecordReader rr = files[i].readRecord(ifa.getAddress()); 205 JalistoObject oldValue = (JalistoObject) rr.readObject(); 206 LogAction logAction = new LogAction(LogAction.UPDATE_ACTION, ifa, oldValue); 207 208 files[i].startBackUp(); 209 logAction.writeTo(logFile); 210 files[i].updateRecord(rw); 211 files[i].finishBackUp(); 212 rw.reset(); 213 rr.reset(); 214 } catch (Exception e) { 215 throw new JalistoException("could not update object " + fo, e); 216 } 217 } 218 219 public void deleteFileObject(InFileAddress ifa) { 220 trace.println(Trace.PHYSICAL, "synraf deleteFileObject : ifa = {0}", ifa); 221 try { 222 int i = 0; 223 if (files.length > 1) { 224 i = ifa.getFileIndex(); 225 } 226 227 RecordReader rr = files[i].readRecord(ifa.getAddress()); 228 JalistoObject fo = (JalistoObject) rr.readObject(); 229 rr.reset(); 230 LogAction logAction = new LogAction(LogAction.DELETE_ACTION, ifa, fo); 231 232 files[i].startBackUp(); 233 logAction.writeTo(logFile); 234 files[i].deleteRecord(ifa.getAddress()); 235 files[i].finishBackUp(); 236 } catch (Exception e) { 237 throw new JalistoException("could not delete object at " + ifa, e); 238 } 239 } 240 241 public void startCommit() { 242 trace.println(Trace.PHYSICAL, "synraf startCommit : "); 243 try { 244 logFile.seek(0); 245 logFile.setLength(0); 246 LogAction logAction = new LogAction(LogAction.BEGIN_ACTION); 247 logAction.writeTo(logFile); 248 } catch (IOException e) { 249 throw new JalistoException(e); 250 } 251 } 252 253 public void commit() { 254 trace.println(Trace.PHYSICAL, "synraf commit : "); 255 try { 256 LogAction logAction = new LogAction(LogAction.COMMIT_ACTION); 257 logAction.writeTo(logFile); 258 logFile.setLength(logFile.getFilePointer()); 259 } catch (IOException e) { 260 throw new JalistoException(e); 261 } 262 } 263 264 public void rollback() { 265 trace.println(Trace.PHYSICAL, "synraf rollback : "); 266 try { 267 LogAction logAction = new LogAction(LogAction.COMMIT_ACTION); 268 logAction.writeTo(logFile); 269 logFile.setLength(logFile.getFilePointer()); 270 } catch (IOException e) { 271 throw new JalistoException(e); 272 } 273 } 274 275 public void close() { 276 } 277 278 public void open() { 279 } 280 281 282 public String toString() { 283 RecordsFileInspector rfi = new RecordsFileInspector(); 284 StringWriter sw = new StringWriter(); 285 PrintWriter out = new PrintWriter(sw); 286 try { 287 rfi.inspectRecordsFile(out, files[0]); 288 } catch (Exception e) { 289 out.println("error during inspection of the database : " + e.getMessage()); 290 } 291 out.flush(); 292 return sw.toString(); 293 } 294 295 296 private RecordsFileLog[] files; 297 private RandomAccessFile logFile; 298 private boolean isNewBase; 299 private Trace trace; 300 } 301 302 | Popular Tags |