/** * Delete the specified directory if it is empty. * Also recursively delete the parent directories if they are empty. */ private final void deleteDir(File dir) { // Check the disk state. It may be false according to the transaction log but // it doesn't matter because directories are lazily created. String[] children = dir.list(); // children may be null if dir doesn't exist any more. if (children != null && children.length == 0) { dir.delete(); if (dir.getAbsolutePath().length() > this.dir.getAbsolutePath().length()) { deleteDir(dir.getParentFile()); } } }
private final byte[] getFromLog(String dirName, String name) throws IOException { // First searchs in the logs a new value for the object. Object key = OperationKey.newKey(dirName, name); byte[] buf = getFromLog(perThreadContext.get().getLog(), key); if (buf != null) return buf; if (((buf = getFromLog(clog, key)) != null) || ((buf = getFromLog(plog, key)) != null)) { return buf; } return null; }
private synchronized void _release() throws IOException { // Change the transaction state. setPhase(FREE); // wake-up an eventually user's thread in begin notify(); }
private final void wakeup() throws IOException { if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, Wakeup: " + commitCount + ", " + operationCount + ", " + cumulativeSize); commitCount = operationCount = cumulativeSize = 0; Hashtable templog = plog; plog = clog; clog = templog; logFile.close(); logFilePN.renameTo(plogFilePN); newLogFile(); _release(); commit(plog); // commit clears the log and frees Operations object. plogFilePN.delete(); if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, Wakeup: end"); } }
public final void initRepository() throws IOException { Operation.initPool(CLEANUP_THRESHOLD_OPERATION); // Search for log files: plog then clog, reads it, then apply all // committed operation, finally deletes it. lockFile = new File(dir, LOCK); if (! lockFile.createNewFile()) { logmon.log(BasicLevel.FATAL, "ATransaction.init(): Either the server is already running, " + "either you have to remove lock file: " + lockFile.getAbsolutePath()); throw new IOException("Transaction already running."); } lockFile.deleteOnExit(); logFilePN = new File(dir, LOG); plogFilePN = new File(dir, PLOG); Hashtable tempLog = new Hashtable(); restart(tempLog, logFilePN); restart(tempLog, plogFilePN); commit(tempLog); plogFilePN.delete(); logFilePN.delete(); clog = new Hashtable(CLEANUP_THRESHOLD_OPERATION / 2); plog = new Hashtable(CLEANUP_THRESHOLD_OPERATION / 2); baos = new ByteArrayOutputStream(10 * Kb); dos = new DataOutputStream(baos); newLogFile(); lock = new Object(); garbage = false; gThread = new Thread(this, "TGarbage"); gThread.start(); }
syncLogFile(); logmon.log(BasicLevel.DEBUG, "ATransaction, committed"); setPhase(COMMIT); release();
/** * Close the transaction module. * It waits all transactions termination, the module will be initialized * anew before reusing it. */ public void close() { stop(); }
public final void stop() { if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, stops"); _stop(); try { // And waits for this thread to die. gThread.join(); } catch (InterruptedException exc3) { if (logmon.isLoggable(BasicLevel.WARN)) logmon.log(BasicLevel.WARN, "ATransaction, interrupted"); } if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, stopped"); }
private final void wakeup() throws IOException { if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, Wakeup: " + commitCount + ", " + operationCount + ", " + cumulativeSize); commitCount = operationCount = cumulativeSize = 0; Hashtable templog = plog; plog = clog; clog = templog; logFile.close(); logFilePN.renameTo(plogFilePN); newLogFile(); _release(); commit(plog); // commit clears the log and frees Operations object. plogFilePN.delete(); if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, Wakeup: end"); } }
public final void initRepository() throws IOException { Operation.initPool(CLEANUP_THRESHOLD_OPERATION); // Search for log files: plog then clog, reads it, then apply all // committed operation, finally deletes it. lockFile = new File(dir, LOCK); if (! lockFile.createNewFile()) { logmon.log(BasicLevel.FATAL, "ATransaction.init(): Either the server is already running, " + "either you have to remove lock file: " + lockFile.getAbsolutePath()); throw new IOException("Transaction already running."); } lockFile.deleteOnExit(); logFilePN = new File(dir, LOG); plogFilePN = new File(dir, PLOG); Hashtable tempLog = new Hashtable(); restart(tempLog, logFilePN); restart(tempLog, plogFilePN); commit(tempLog); plogFilePN.delete(); logFilePN.delete(); clog = new Hashtable(CLEANUP_THRESHOLD_OPERATION / 2); plog = new Hashtable(CLEANUP_THRESHOLD_OPERATION / 2); baos = new ByteArrayOutputStream(10 * Kb); dos = new DataOutputStream(baos); newLogFile(); lock = new Object(); garbage = false; gThread = new Thread(this, "TGarbage"); gThread.start(); }
syncLogFile(); logmon.log(BasicLevel.DEBUG, "ATransaction, committed"); setPhase(COMMIT); release();
/** * Close the transaction module. * It waits all transactions termination, the module will be initialized * anew before reusing it. */ public void close() { stop(); }
public final void stop() { if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, stops"); _stop(); try { // And waits for this thread to die. gThread.join(); } catch (InterruptedException exc3) { if (logmon.isLoggable(BasicLevel.WARN)) logmon.log(BasicLevel.WARN, "ATransaction, interrupted"); } if (logmon.isLoggable(BasicLevel.INFO)) logmon.log(BasicLevel.INFO, "ATransaction, stopped"); }
private synchronized void _release() throws IOException { // Change the transaction state. setPhase(FREE); // wake-up an eventually user's thread in begin notify(); }
private final byte[] getFromLog(String dirName, String name) throws IOException { // First searchs in the logs a new value for the object. Object key = OperationKey.newKey(dirName, name); byte[] buf = getFromLog(((Context) perThreadContext.get()).getLog(), key); if (buf != null) return buf; if (((buf = getFromLog(clog, key)) != null) || ((buf = getFromLog(plog, key)) != null)) { return buf; } return null; }
/** * Delete the specified directory if it is empty. * Also recursively delete the parent directories if they are empty. */ private final void deleteDir(File dir) { // Check the disk state. It may be false according to the transaction log but // it doesn't matter because directories are lazily created. String[] children = dir.list(); // children may be null if dir doesn't exist any more. if (children != null && children.length == 0) { dir.delete(); if (dir.getAbsolutePath().length() > this.dir.getAbsolutePath().length()) { deleteDir(dir.getParentFile()); } } }
public final synchronized void _stop() { synchronized (lock) { while (phase != FREE) { // Wait for the transaction subsystem to be free try { wait(); } catch (InterruptedException exc) { } } // Change the transaction state. setPhase(FINALIZE); isRunning = false; garbage = true; // Wake-up the garbage thread. lock.notify(); } }
public final byte[] loadByteArray(String dirName, String name) throws IOException { // First searchs in the logs a new value for the object. try { byte[] buf = getFromLog(dirName, name); if (buf != null) return buf; // Gets it from disk. File file; if (dirName == null) { file = new File(dir, name); } else { File parentDir = new File(dir, dirName); file = new File(parentDir, name); } FileInputStream fis = new FileInputStream(file); buf = new byte[(int) file.length()]; for (int nb=0; nb<buf.length; ) { int ret = fis.read(buf, nb, buf.length-nb); if (ret == -1) throw new EOFException(); nb += ret; } fis.close(); return buf; } catch (FileNotFoundException exc) { return null; } }
file = new File(parentDir, op.name); deleted = file.delete(); deleteDir(parentDir);
public final synchronized void _stop() { synchronized (lock) { while (phase != FREE) { // Wait for the transaction subsystem to be free try { wait(); } catch (InterruptedException exc) { } } // Change the transaction state. setPhase(FINALIZE); isRunning = false; garbage = true; // Wake-up the garbage thread. lock.notify(); } }