@Override
public void openWriter() throws DatastoreException {
try {
File parentDir = recordLocation.getParentFile();
checkState(parentDir.exists() || parentDir.mkdirs());
this.randomAccess = new RandomAccessFile(recordLocation, "rw");
this.channel = randomAccess.getChannel();
LOG.debug("Acquiring exclusive file lock on: {}", recordLocation);
this.fileLock = this.channel.tryLock();
if(this.fileLock != null) {
LOG.debug("Got an exclusive lock on: {}", recordLocation);
if(channel.size() > 0) {
loadHeader();
} else {
recordCount = new AtomicLong(0);
recordPosition = HEADER_SIZE;
writeHeader();
}
} else {
throw new RecordStoreInUseException("Record datastore: " + recordLocation + " is already in use, cannot be opened");
}
} catch(IOException e) {
throw new DatastoreException("Unable to open record store", e);
} catch(OverlappingFileLockException e) {
throw new RecordStoreInUseException("Record datastore: " + recordLocation + " is already in use, cannot be opened");
}
}