@Override
 public void appendPrepareRecord(
     long txID, EncodingSupport transactionData, boolean sync, IOCompletion callback)
     throws Exception {
   JournalInternalRecord prepareRecord =
       new JournalCompleteRecordTX(TX_RECORD_TYPE.PREPARE, txID, transactionData);
   AtomicInteger value = transactions.get(Long.valueOf(txID));
   if (value != null) {
     prepareRecord.setNumberOfRecords(value.get());
   }
   writeRecord(prepareRecord, sync, callback);
 }
  @Override
  public void appendCommitRecord(
      long txID, boolean sync, IOCompletion callback, boolean lineUpContext) throws Exception {
    JournalInternalRecord commitRecord =
        new JournalCompleteRecordTX(TX_RECORD_TYPE.COMMIT, txID, null);
    AtomicInteger value = transactions.remove(Long.valueOf(txID));
    if (value != null) {
      commitRecord.setNumberOfRecords(value.get());
    }

    writeRecord(commitRecord, true, callback);
  }
  /** Write the record to the current file. */
  private void writeRecord(
      JournalInternalRecord encoder, final boolean sync, final IOCompletion callback)
      throws Exception {

    lockAppend.lock();
    try {
      if (callback != null) {
        callback.storeLineUp();
      }
      currentFile = journal.switchFileIfNecessary(encoder.getEncodeSize());
      encoder.setFileID(currentFile.getRecordID());

      if (callback != null) {
        currentFile.getFile().write(encoder, sync, callback);
      } else {
        currentFile.getFile().write(encoder, sync);
      }
    } finally {
      lockAppend.unlock();
    }
  }
  public static SequentialFile writeControlFile(
      final SequentialFileFactory fileFactory,
      final List<JournalFile> files,
      final List<JournalFile> newFiles,
      final List<Pair<String, String>> renames)
      throws Exception {

    SequentialFile controlFile =
        fileFactory.createSequentialFile(AbstractJournalUpdateTask.FILE_COMPACT_CONTROL, 1);

    try {
      controlFile.open(1, false);

      JournalImpl.initFileHeader(fileFactory, controlFile, 0, 0);

      HornetQBuffer filesToRename = HornetQBuffers.dynamicBuffer(1);

      // DataFiles first

      if (files == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(files.size());

        for (JournalFile file : files) {
          filesToRename.writeUTF(file.getFile().getFileName());
        }
      }

      // New Files second

      if (newFiles == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(newFiles.size());

        for (JournalFile file : newFiles) {
          filesToRename.writeUTF(file.getFile().getFileName());
        }
      }

      // Renames from clean up third
      if (renames == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(renames.size());
        for (Pair<String, String> rename : renames) {
          filesToRename.writeUTF(rename.getA());
          filesToRename.writeUTF(rename.getB());
        }
      }

      JournalInternalRecord controlRecord =
          new JournalAddRecord(
              true, 1, (byte) 0, new ByteArrayEncoding(filesToRename.toByteBuffer().array()));

      HornetQBuffer renameBuffer = HornetQBuffers.dynamicBuffer(filesToRename.writerIndex());

      controlRecord.setFileID(0);

      controlRecord.encode(renameBuffer);

      ByteBuffer writeBuffer = fileFactory.newBuffer(renameBuffer.writerIndex());

      writeBuffer.put(renameBuffer.toByteBuffer().array(), 0, renameBuffer.writerIndex());

      writeBuffer.rewind();

      controlFile.writeDirect(writeBuffer, true);

      return controlFile;
    } finally {
      controlFile.close();
    }
  }
 protected void writeEncoder(final JournalInternalRecord record, final int txcounter)
     throws Exception {
   record.setNumberOfRecords(txcounter);
   writeEncoder(record);
 }
 protected void writeEncoder(final JournalInternalRecord record) throws Exception {
   record.setFileID(currentFile.getRecordID());
   record.encode(getWritingChannel());
 }