use of java.nio.MappedByteBuffer in project mapdb by jankotek.
the class ByteBufferMemoryVol method close.
@Override
public void close() {
if (!closed.compareAndSet(false, true))
return;
growLock.lock();
try {
if (cleanerHackEnabled) {
for (ByteBuffer b : slices) {
if (b != null && (b instanceof MappedByteBuffer)) {
unmap((MappedByteBuffer) b);
}
}
}
Arrays.fill(slices, null);
slices = null;
} finally {
growLock.unlock();
}
}
use of java.nio.MappedByteBuffer in project mapdb by jankotek.
the class MappedFileVol method truncate.
@Override
public void truncate(long size) {
final int maxSize = 1 + (int) (size >>> sliceShift);
if (maxSize == slices.length)
return;
if (maxSize > slices.length) {
ensureAvailable(size);
return;
}
growLock.lock();
try {
if (maxSize >= slices.length)
return;
ByteBuffer[] old = slices;
slices = Arrays.copyOf(slices, maxSize);
//unmap remaining buffers
for (int i = maxSize; i < old.length; i++) {
if (cleanerHackEnabled) {
unmap((MappedByteBuffer) old[i]);
}
old[i] = null;
}
if (ByteBufferVol.windowsWorkaround) {
for (int i = 0; i < maxSize; i++) {
if (cleanerHackEnabled) {
unmap((MappedByteBuffer) old[i]);
}
old[i] = null;
}
}
try {
fileChannel.truncate(1L * sliceSize * maxSize);
} catch (IOException e) {
throw new DBException.VolumeIOError(e);
}
if (ByteBufferVol.windowsWorkaround) {
for (int pos = 0; pos < maxSize; pos++) {
ByteBuffer b = fileChannel.map(mapMode, 1L * sliceSize * pos, sliceSize);
if (CC.ASSERT && b.order() != ByteOrder.BIG_ENDIAN)
throw new AssertionError("Little-endian");
slices[pos] = b;
}
}
} catch (IOException e) {
throw new DBException.VolumeIOError(e);
} finally {
growLock.unlock();
}
}
use of java.nio.MappedByteBuffer in project mapdb by jankotek.
the class MappedFileVol method sync.
@Override
public void sync() {
if (readOnly)
return;
growLock.lock();
try {
ByteBuffer[] slices = this.slices;
if (slices == null)
return;
// and it increases chance to detect file corruption.
for (int i = slices.length - 1; i >= 0; i--) {
ByteBuffer b = slices[i];
if (b != null && (b instanceof MappedByteBuffer)) {
MappedByteBuffer bb = ((MappedByteBuffer) b);
bb.force();
}
}
} finally {
growLock.unlock();
}
}
use of java.nio.MappedByteBuffer in project Aeron by real-logic.
the class ArchiveFileUtil method archiveMetaFileFormatDecoder.
static ArchiveDescriptorDecoder archiveMetaFileFormatDecoder(final File metaFile) throws IOException {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(metaFile, "rw");
FileChannel metadataFileChannel = randomAccessFile.getChannel()) {
final MappedByteBuffer metaDataBuffer = metadataFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, ArchiveIndex.INDEX_RECORD_SIZE);
final ArchiveDescriptorDecoder decoder = new ArchiveDescriptorDecoder();
return decoder.wrap(new UnsafeBuffer(metaDataBuffer), ArchiveIndex.INDEX_FRAME_LENGTH, ArchiveDescriptorDecoder.BLOCK_LENGTH, ArchiveDescriptorDecoder.SCHEMA_VERSION);
}
}
use of java.nio.MappedByteBuffer in project Aeron by real-logic.
the class CommonContext method saveErrorLog.
/**
* Read the error log to a given {@link PrintStream}
*
* @param out to write the error log contents to.
* @return the number of observations from the error log
*/
public int saveErrorLog(final PrintStream out) {
final File dirFile = new File(aeronDirectoryName);
int distinctErrorCount = 0;
if (dirFile.exists() && dirFile.isDirectory()) {
final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
if (cncFile.exists()) {
MappedByteBuffer cncByteBuffer = null;
try {
cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
if (CncFileDescriptor.CNC_VERSION != cncVersion) {
throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
}
final AtomicBuffer buffer = CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
distinctErrorCount = ErrorLogReader.read(buffer, (observationCount, firstObservationTimestamp, lastObservationTimestamp, encodedException) -> out.format("***%n%d observations from %s to %s for:%n %s%n", observationCount, dateFormat.format(new Date(firstObservationTimestamp)), dateFormat.format(new Date(lastObservationTimestamp)), encodedException));
out.format("%n%d distinct errors observed.%n", distinctErrorCount);
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
} finally {
IoUtil.unmap(cncByteBuffer);
}
}
}
return distinctErrorCount;
}
Aggregations