1 24 package org.objectweb.jalisto.se.storage.raf.nolog.asynchro; 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.server.JalistoObjectImpl; 31 import org.objectweb.jalisto.se.impl.trace.Trace; 32 import org.objectweb.jalisto.se.impl.InFileAddress; 33 import org.objectweb.jalisto.se.JalistoFactory; 34 import org.objectweb.jalisto.se.storage.raf.*; 35 36 import java.io.*; 37 import java.util.*; 38 39 public class PhysicalFileAccessNologAsynchroImpl implements PluggablePhysicalFileAccess, Runnable { 40 41 private void init(int initialSize, JalistoProperties properties) 42 throws IOException, RecordsFileException { 43 int nbrDbFiles = properties.getInstanceDbFileNumber(); 44 if (nbrDbFiles > 1) { 45 files = new RecordsFile[nbrDbFiles + 2]; 46 files[0] = new RecordsFile(properties.getSystemDbFilePath()); 47 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 48 files[1] = new RecordsFile(properties.getOidDbFilePath()); 49 files[1].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 50 51 for (int i = 0; i < nbrDbFiles; i++) { 52 files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i)); 53 files[i + 2].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 54 } 55 } else { 56 files = new RecordsFile[1]; 57 files[0] = new RecordsFile(properties.getSystemDbFilePath()); 58 files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace()); 59 } 60 } 61 62 private void init(String accessFlags, JalistoProperties properties) 63 throws IOException, RecordsFileException { 64 int nbrDbFiles = properties.getInstanceDbFileNumber(); 65 if (nbrDbFiles > 1) { 66 files = new RecordsFile[nbrDbFiles + 2]; 67 files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags); 68 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 69 files[1] = new RecordsFile(properties.getOidDbFilePath(), accessFlags); 70 files[1].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 71 72 for (int i = 0; i < nbrDbFiles; i++) { 73 files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i), accessFlags); 74 files[i + 2].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 75 } 76 } else { 77 files = new RecordsFile[1]; 78 files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags); 79 files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace()); 80 } 81 } 82 83 public void init(JalistoProperties properties) { 84 try { 85 orderedActions = Collections.synchronizedList(new LinkedList()); 86 actions = new HashMap(); 87 String dbPath = properties.getDbFileFullName(); 88 File f = new File(dbPath); 89 this.isNewBase = (!f.exists()); 90 trace = JalistoFactory.getInternalFactory().getTracer(properties); 91 trace.println(Trace.DEBUG, "Uses PhysicalFileAccessNologAsynchroImpl"); 92 if (isNewBase && properties.isReadOnlyImplementation()) { 93 throw new JalistoException("cannot create a database instance in read only mode"); 94 } 95 if (isNewBase) { 96 init(properties.getInitialSize(), properties); 97 } else { 98 init("rw", properties); 99 } 100 for (int i = 0; i < files.length; i++) { 101 files[i].setTrace(trace); 102 } 103 trace.println(Trace.PHYSICAL, "new instance with {0} underlying files", new Integer (files.length)); 104 } catch (Exception e) { 105 throw new JalistoException(e); 106 } 107 } 108 109 public boolean isNewBase() { 110 return isNewBase; 111 } 112 113 public void reorganize() { 114 trace.println(Trace.PHYSICAL, "asynchro reorganize : "); 115 for (int i = 0; i < files.length; i++) { 116 try { 117 files[i].reorganize(); 118 } catch (Exception e) { 119 throw new JalistoException( 120 "error during reorganization of a physical store file : " + files[i].toString(), e); 121 } 122 } 123 } 124 125 public Collection getKeysStartingWith(String filter) { 126 trace.println(Trace.PHYSICAL, "asynchro getKeysStartingWith : filter = {0}", filter); 127 synchronized (orderedActions) { 128 Collection result = files[0].getKeysStartingWith(filter); 129 for (short i = 1; i < files.length; i++) { 130 result.addAll(files[i].getKeysStartingWith(filter)); 131 } 132 Iterator addresses = actions.keySet().iterator(); 133 while (addresses.hasNext()) { 134 String address = (String ) addresses.next(); 135 if (address.startsWith(filter)) { 136 ((ActionWrapper) actions.get(address)).doExtentAction(address, result); 137 } 138 } 139 return result; 140 } 141 } 142 143 public void insertFileObject(InFileAddress ifa, JalistoObject fo) { 144 trace.println(Trace.PHYSICAL, "asynchro insertFileObject({0}, {1})", ifa, fo); 145 synchronized (orderedActions) { 146 ActionWrapper insertWrapper = (ActionWrapper) actions.get(ifa.getAddress()); 147 if (insertWrapper == null) { 148 insertWrapper = new InsertWrapper(); 149 insertWrapper.setJalistoObject(fo.getClone()); 150 orderedActions.add(insertWrapper); 151 actions.put(ifa.getAddress(), insertWrapper); 152 } else { 153 orderedActions.remove(insertWrapper); 154 ActionWrapper upgrade = insertWrapper.upgradeToInsert(fo.getClone()); 155 orderedActions.add(upgrade); 156 actions.put(ifa.getAddress(), upgrade); 157 } 158 } 159 } 160 161 public JalistoObject readFileObjectAt(InFileAddress ifa) { 162 trace.println(Trace.PHYSICAL, "asynchro readFileObjectAt : ifa = {0}", ifa); 163 ActionWrapper actionWrapper = (ActionWrapper) actions.get(ifa.getAddress()); 164 if (actionWrapper != null) { 165 if (actionWrapper.canReadJalistoObject()) { 166 return actionWrapper.getJalistoObject().getClone(); 167 } else { 168 throw new JalistoException("object has been removed from base : " + ifa); 169 } 170 } 171 return internalReadFileObjectAt(ifa); 172 } 173 174 public void updateFileObject(InFileAddress ifa, JalistoObject fo) { 175 trace.println(Trace.PHYSICAL, "asynchro updateFileObject({0}, {1})", ifa, fo); 176 synchronized (orderedActions) { 177 ActionWrapper updateWrapper = (ActionWrapper) actions.get(ifa.getAddress()); 178 if (updateWrapper == null) { 179 updateWrapper = new UpdateWrapper(); 180 updateWrapper.setJalistoObject(fo.getClone()); 181 orderedActions.add(updateWrapper); 182 actions.put(ifa.getAddress(), updateWrapper); 183 } else { 184 orderedActions.remove(updateWrapper); 185 ActionWrapper upgrade = updateWrapper.upgradeToUpdate(fo.getClone()); 186 orderedActions.add(upgrade); 187 actions.put(ifa.getAddress(), upgrade); 188 } 189 } 190 } 191 192 public void deleteFileObject(InFileAddress ifa) { 193 trace.println(Trace.PHYSICAL, "asynchro deleteFileObject : ifa = {0}", ifa); 194 synchronized (orderedActions) { 195 ActionWrapper deleteWrapper = (ActionWrapper) actions.get(ifa.getAddress()); 196 if (deleteWrapper == null) { 197 deleteWrapper = new DeleteWrapper(); 198 JalistoObject fo = new JalistoObjectImpl(); 199 fo.setIfa(ifa); 200 deleteWrapper.setJalistoObject(fo); 201 orderedActions.add(deleteWrapper); 202 actions.put(ifa.getAddress(), deleteWrapper); 203 } else { 204 orderedActions.remove(deleteWrapper); 205 ActionWrapper upgrade = deleteWrapper.upgradeToDelete(ifa); 206 orderedActions.add(upgrade); 207 actions.put(ifa.getAddress(), upgrade); 208 } 209 } 210 } 211 212 213 public String toString() { 214 RecordsFileInspector rfi = new RecordsFileInspector(); 215 StringWriter sw = new StringWriter(); 216 PrintWriter out = new PrintWriter(sw); 217 try { 218 rfi.inspectRecordsFile(out, files[0]); 219 } catch (Exception e) { 220 out.println("error during inspection of the database : " + e.getMessage()); 221 } 222 out.flush(); 223 return sw.toString(); 224 } 225 226 public void startCommit() { 227 } 228 229 public void commit() { 230 } 231 232 public void rollback() { 233 } 234 235 public void open() { 236 trace.println(Trace.PHYSICAL, "asynchro open : "); 237 isOpen = true; 238 thread = new Thread (this); 239 thread.start(); 240 } 241 242 public void close() { 243 trace.println(Trace.PHYSICAL, "asynchro close : "); 244 while (!orderedActions.isEmpty()) { 245 Thread.yield(); 246 } 247 isOpen = false; 248 } 249 250 251 private RecordsFile[] files; 252 private boolean isNewBase; 253 private Trace trace; 254 255 256 259 260 public void run() { 261 trace.println(Trace.PHYSICAL, "asynchro run : "); 262 while (isOpen) { 263 synchronized (orderedActions) { 264 if (!orderedActions.isEmpty()) { 265 ActionWrapper actionWrapper = (ActionWrapper) orderedActions.remove(0); 266 actionWrapper.doAction(this); 267 InFileAddress ifa = actionWrapper.getJalistoObject().getIfa(); 268 ActionWrapper actionInMap = (ActionWrapper) actions.get(ifa.getAddress()); 269 if (actionInMap == actionWrapper) { 270 actions.remove(ifa.getAddress()); 271 } 272 } else { 273 if (!actions.isEmpty()) { 274 throw new JalistoException("orderedActions empty and actions not : " + actions); 275 } 276 } 277 } 278 Thread.yield(); 279 } 280 } 281 282 public void internalInsertFileObject(InFileAddress ifa, JalistoObject fo) { 283 trace.println(Trace.PHYSICAL, "insertFileObject({0}, {1})", ifa, fo); 284 int i = 0; 285 try { 286 RecordWriter rw = new RecordWriter(ifa.getAddress()); 287 rw.writeObject(fo); 288 if (files.length > 1) { 289 i = ifa.getFileIndex(); 290 } 291 files[i].insertRecord(rw); 292 rw.reset(); 293 } catch (Exception e) { 294 Object v; 295 try { 296 RecordReader rr = files[i].readRecord(ifa.getAddress()); 297 v = rr.readObject(); 298 rr.reset(); 299 } catch (Exception e2) { 300 throw new JalistoException("could not insert object " + fo, e); 301 } 302 throw new JalistoException("could not insert object " + fo + 303 ";\n\t\tvalue already in base : " + String.valueOf(v), e); 304 } 305 } 306 307 public JalistoObject internalReadFileObjectAt(InFileAddress ifa) { 308 trace.println(Trace.PHYSICAL, "asynchro internalReadFileObjectAt : ifa = {0}", ifa); 309 JalistoObject result; 310 int i = 0; 311 try { 312 if (files.length > 1) { 313 i = ifa.getFileIndex(); 314 } 315 RecordReader rr = files[i].readRecord(ifa.getAddress()); 316 result = (JalistoObject) rr.readObject(); 317 result.setIfa(ifa); 318 rr.reset(); 319 } catch (Exception e) { 320 throw new JalistoException("could not read object at " + ifa, e); 321 } 322 return result; 323 } 324 325 public void internalUpdateFileObject(InFileAddress ifa, JalistoObject fo) { 326 trace.println(Trace.PHYSICAL, "asynchro updateFileObject({0}, {1})", ifa, fo); 327 try { 328 RecordWriter rw = new RecordWriter(ifa.getAddress()); 329 rw.writeObject(fo); 330 int i = 0; 331 if (files.length > 1) { 332 i = ifa.getFileIndex(); 333 } 334 files[i].updateRecord(rw); 335 rw.reset(); 336 } catch (Exception e) { 337 throw new JalistoException("could not update object " + fo, e); 338 } 339 } 340 341 public void internalDeleteFileObject(InFileAddress ifa) { 342 trace.println(Trace.PHYSICAL, "asynchro internalDeleteFileObject : ifa = {0}", ifa); 343 try { 344 int i = 0; 345 if (files.length > 1) { 346 i = ifa.getFileIndex(); 347 } 348 files[i].deleteRecord(ifa.getAddress()); 349 } catch (Exception e) { 350 throw new JalistoException("could not delete object at " + ifa, e); 351 } 352 } 353 354 355 private List orderedActions; 356 private HashMap actions; 357 private boolean isOpen = false; 358 private Thread thread; 359 } 360 361 362 363 | Popular Tags |