예제 #1
0
  /** Commits the transaction to the log file. */
  void commit() throws IOException {
    Serialization.writeObject(oos, txns[curTxn]);
    sync();

    // set clean flag to indicate blocks have been written to log
    setClean(txns[curTxn]);

    // open a new ObjectOutputStream in order to store
    // newer states of BlockIo
    oos = new DataOutputStream(new BufferedOutputStream(fos));
  }
예제 #2
0
  /** Startup recovery on all files */
  private void recover() throws IOException {
    String logName = makeLogName();
    File logFile = new File(logName);
    if (!logFile.exists()) return;
    if (logFile.length() == 0) {
      logFile.delete();
      return;
    }

    FileInputStream fis = new FileInputStream(logFile);
    DataInputStream ois = new DataInputStream(new BufferedInputStream(fis));

    try {
      if (ois.readShort() != Magic.LOGFILE_HEADER) throw new Error("Bad magic on log file");
    } catch (IOException e) {
      // corrupted/empty logfile
      logFile.delete();
      return;
    }

    while (true) {
      ArrayList<BlockIo> blocks = null;
      try {
        blocks = (ArrayList<BlockIo>) Serialization.readObject(ois);
      } catch (ClassNotFoundException e) {
        throw new Error("Unexcepted exception: " + e);
      } catch (IOException e) {
        // corrupted logfile, ignore rest of transactions
        break;
      }
      synchronizeBlocks(blocks, false);

      // ObjectInputStream must match exactly each
      // ObjectOutputStream created during writes
      //            try {
      ois = new DataInputStream(fis);
      //            } catch (IOException e) {
      //                // corrupted logfile, ignore rest of transactions
      //                break;
      //            }
    }
    owner.sync();
    fis.close();
    logFile.delete();
  }