}
}
}
private Location enqueueBatch(WriteCommand writeRecord) throws ClosedJournalException, IOException {
WriteBatch currentBatch = null;
int spinnings = 0;
int limit = SPIN_RETRIES;
while (true) {
if (asyncException.get() != null) {
throw new IOException(asyncException.get());
}
try {
if (!opened) {
throw new ClosedJournalException("The journal is closed!");
}
if (batching.compareAndSet(false, true)) {
boolean hasNewBatch = false;
try {
if (nextWriteBatch == null) {
DataFile file = journal.getCurrentWriteDataFile();
boolean canBatch = false;
currentBatch = new WriteBatch(file, journal.getLastAppendLocation().getPointer() + 1);
canBatch = currentBatch.canBatch(writeRecord, journal.getMaxWriteBatchSize(), journal.getMaxFileLength());
if (!canBatch) {
file = journal.newDataFile();
currentBatch = new WriteBatch(file, 0);
}
WriteCommand controlRecord = currentBatch.prepareBatch();
writeRecord.getLocation().setDataFileId(file.getDataFileId());
writeRecord.getLocation().setPointer(currentBatch.incrementAndGetPointer());
writeRecord.getLocation().setLatch(currentBatch.getLatch());
currentBatch.appendBatch(writeRecord);
if (!writeRecord.isSync()) {
journal.getInflightWrites().put(controlRecord.getLocation(), controlRecord);
journal.getInflightWrites().put(writeRecord.getLocation(), writeRecord);
nextWriteBatch = currentBatch;
} else {