Search in sources :

Example 26 with MappedByteBuffer

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();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 27 with MappedByteBuffer

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();
    }
}
Also used : DBException(org.mapdb.DBException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 28 with MappedByteBuffer

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();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 29 with MappedByteBuffer

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);
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ArchiveDescriptorDecoder(io.aeron.archiver.messages.ArchiveDescriptorDecoder) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 30 with MappedByteBuffer

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;
}
Also used : PrintStream(java.io.PrintStream) Date(java.util.Date) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) SimpleDateFormat(java.text.SimpleDateFormat) UUID(java.util.UUID) IoUtil(org.agrona.IoUtil) File(java.io.File) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) Consumer(java.util.function.Consumer) LangUtil(org.agrona.LangUtil) System.getProperty(java.lang.System.getProperty) ErrorLogReader(org.agrona.concurrent.errors.ErrorLogReader) AtomicBuffer(org.agrona.concurrent.AtomicBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) AtomicBuffer(org.agrona.concurrent.AtomicBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)147 FileChannel (java.nio.channels.FileChannel)71 IOException (java.io.IOException)40 File (java.io.File)34 RandomAccessFile (java.io.RandomAccessFile)33 FileInputStream (java.io.FileInputStream)27 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)7 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 FileNotFoundException (java.io.FileNotFoundException)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2