1 24 package org.objectweb.jalisto.se.storage.raf.nolog.synchro; 25 26 import org.objectweb.jalisto.se.api.JalistoProperties; 27 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess; 28 import org.objectweb.jalisto.se.api.internal.JalistoObject; 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 35 import java.io.*; 36 import java.util.Collection ; 37 38 public class PhysicalFileAccessNologSynchroImpl implements PluggablePhysicalFileAccess { 39 40 private void init(int initialSize, JalistoProperties properties) 41 throws IOException, RecordsFileException { 42 int nbrDbFiles = properties.getInstanceDbFileNumber(); 43 if (nbrDbFiles > 1) { 44 files = new RecordsFile[nbrDbFiles + 2]; 45 files[0] = new RecordsFile(properties.getSystemDbFilePath()); 46 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 47 files[1] = new RecordsFile(properties.getOidDbFilePath()); 48 files[1].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 49 50 for (int i = 0; i < nbrDbFiles; i++) { 51 files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i)); 52 files[i + 2].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 53 } 54 } else { 55 files = new RecordsFile[1]; 56 files[0] = new RecordsFile(properties.getSystemDbFilePath()); 57 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 58 } 59 } 60 61 private void init(String accessFlags, JalistoProperties properties) 62 throws IOException, RecordsFileException { 63 int nbrDbFiles = properties.getInstanceDbFileNumber(); 64 if (nbrDbFiles > 1) { 65 files = new RecordsFile[nbrDbFiles + 2]; 66 files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags); 67 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 68 files[1] = new RecordsFile(properties.getOidDbFilePath(), accessFlags); 69 files[1].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 70 71 for (int i = 0; i < nbrDbFiles; i++) { 72 files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i), accessFlags); 73 files[i + 2].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 74 } 75 } else { 76 files = new RecordsFile[1]; 77 files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags); 78 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 79 } 80 } 81 82 public void init(JalistoProperties properties) { 83 try { 84 String dbPath = properties.getDbFileFullName(); 85 File f = new File(dbPath); 86 this.isNewBase = (!f.exists()); 87 trace = JalistoFactory.getInternalFactory().getTracer(properties); 88 if (isNewBase && properties.isReadOnlyImplementation()) { 89 throw new JalistoException("cannot create a database instance in read only mode"); 90 } 91 if (isNewBase) { 92 init(properties.getInitialSize(), properties); 93 } else { 94 init("rw", properties); 95 } 96 for (int i = 0; i < files.length; i++) { 97 files[i].setTrace(trace); 98 } 99 trace.println(Trace.PHYSICAL, "new instance with {0} underlying files", new Integer (files.length)); 100 } catch (Exception e) { 101 throw new JalistoException(e); 102 } 103 } 104 105 public boolean isNewBase() { 106 return isNewBase; 107 } 108 109 public void reorganize() { 110 trace.println(Trace.PHYSICAL, "synchro reorganize : "); 111 for (int i = 0; i < files.length; i++) { 112 try { 113 files[i].reorganize(); 114 } catch (Exception e) { 115 throw new JalistoException( 116 "error during reorganization of a physical store file : " + files[i].toString(), e); 117 } 118 } 119 } 120 121 public Collection getKeysStartingWith(String filter) { 122 trace.println(Trace.PHYSICAL, "synchro getKeysStartingWith : filter = {0}", filter); 123 if (files.length > 1) { 124 Collection result = files[0].getKeysStartingWith(filter); 125 for (short i = 1; i < files.length; i++) { 126 result.addAll(files[i].getKeysStartingWith(filter)); 127 } 128 return result; 129 } else { 130 return files[0].getKeysStartingWith(filter); 131 } 132 } 133 134 public void insertFileObject(InFileAddress ifa, JalistoObject fo) { 135 trace.println(Trace.PHYSICAL, "synchro insertFileObject({0}, {1})", ifa, fo); 136 int i = 0; 137 RecordWriter rw = null; 138 RecordReader rr = null; 139 try { 140 rw = new RecordWriter(ifa.getAddress()); 141 rw.writeObject(fo); 142 if (files.length > 1) { 143 i = ifa.getFileIndex(); 144 } 145 files[i].insertRecord(rw); 146 rw.reset(); 147 } catch (Exception e) { 148 Object v; 149 try { 150 rr = files[i].readRecord(ifa.getAddress()); 151 v = rr.readObject(); 152 rr.reset(); 153 } catch (Exception e2) { 154 throw new JalistoException("could not insert object " + fo, e); 155 } 156 throw new JalistoException("could not insert object " + fo + 157 ";\n\t\tvalue already in base : " + String.valueOf(v), e); 158 } 166 } 167 168 public JalistoObject readFileObjectAt(InFileAddress ifa) { 169 trace.println(Trace.PHYSICAL, "synchro readFileObjectAt : "); 170 JalistoObject result; 171 int i = 0; 172 RecordReader rr = null; 173 try { 174 if (files.length > 1) { 175 i = ifa.getFileIndex(); 176 } 177 rr = files[i].readRecord(ifa.getAddress()); 178 result = (JalistoObject) rr.readObject(); 179 result.setIfa(ifa); 180 } catch (Exception e) { 181 throw new JalistoException("could not read object at " + ifa, e); 182 } 187 return result; 188 } 189 190 public void updateFileObject(InFileAddress ifa, JalistoObject fo) { 191 trace.println(Trace.PHYSICAL, "synchro updateFileObject({0}, {1})", ifa, fo); 192 RecordWriter rw = null; 193 try { 194 rw = new RecordWriter(ifa.getAddress()); 195 rw.writeObject(fo); 196 int i = 0; 197 if (files.length > 1) { 198 i = ifa.getFileIndex(); 199 } 200 files[i].updateRecord(rw); 201 rw.reset(); 202 } catch (Exception e) { 203 throw new JalistoException("could not update object " + fo, e); 204 } 209 } 210 211 public void deleteFileObject(InFileAddress ifa) { 212 trace.println(Trace.PHYSICAL, "synchro deleteFileObject : "); 213 try { 214 int i = 0; 215 if (files.length > 1) { 216 i = ifa.getFileIndex(); 217 } 218 files[i].deleteRecord(ifa.getAddress()); 219 } catch (Exception e) { 220 throw new JalistoException("could not delete object at " + ifa, e); 221 } 222 } 223 224 225 public String toString() { 226 RecordsFileInspector rfi = new RecordsFileInspector(); 227 StringWriter sw = new StringWriter(); 228 PrintWriter out = new PrintWriter(sw); 229 try { 230 rfi.inspectRecordsFile(out, files[0]); 231 } catch (Exception e) { 232 out.println("error during inspection of the database : " + e.getMessage()); 233 } 234 out.flush(); 235 return sw.toString(); 236 } 237 238 public void startCommit() { 239 } 240 241 public void commit() { 242 } 243 244 public void rollback() { 245 } 246 247 public void close() { 248 } 249 250 public void open() { 251 } 252 253 254 private RecordsFile[] files; 255 private boolean isNewBase; 256 private Trace trace; 257 } 258 259 | Popular Tags |