@Test public void testSelectMappedBuffer() throws IOException { MappedFile mappedFile = new MappedFile("target/unit_test_store/MappedFileTest/000", 1024 * 64); boolean result = mappedFile.appendMessage(storeMessage.getBytes()); assertThat(result).isTrue(); SelectMappedBufferResult selectMappedBufferResult = mappedFile.selectMappedBuffer(0); byte[] data = new byte[storeMessage.length()]; selectMappedBufferResult.getByteBuffer().get(data); String readString = new String(data); assertThat(readString).isEqualTo(storeMessage); mappedFile.shutdown(1000); assertThat(mappedFile.isAvailable()).isFalse(); selectMappedBufferResult.release(); assertThat(mappedFile.isCleanupOver()).isTrue(); assertThat(mappedFile.destroy(1000)).isTrue(); }
public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { int readPosition = getReadPosition(); if ((pos + size) <= readPosition) { if (this.hold()) { ByteBuffer byteBuffer = this.mappedByteBuffer.slice(); byteBuffer.position(pos); ByteBuffer byteBufferNew = byteBuffer.slice(); byteBufferNew.limit(size); return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size + ", fileFromOffset: " + this.fileFromOffset); } return null; }
MappedFile extends ReferenceResource { public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { int readPosition = getReadPosition(); if ((pos + size) <= readPosition) { if (this.hold()) { ByteBuffer byteBuffer = this.mappedByteBuffer.slice(); byteBuffer.position(pos); ByteBuffer byteBufferNew = byteBuffer.slice(); byteBufferNew.limit(size); return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size + ", fileFromOffset: " + this.fileFromOffset); } return null; } }
MappedFile extends ReferenceResource { public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { int readPosition = getReadPosition(); if ((pos + size) <= readPosition) { if (this.hold()) { ByteBuffer byteBuffer = this.mappedByteBuffer.slice(); byteBuffer.position(pos); ByteBuffer byteBufferNew = byteBuffer.slice(); byteBufferNew.limit(size); return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size + ", fileFromOffset: " + this.fileFromOffset); } return null; } MappedFile();  MappedFile(final String fileName, final int fileSize);  MappedFile(final String fileName, final int fileSize,
        final TransientStorePool transientStorePool); }
MappedFile extends ReferenceResource { public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { int readPosition = getReadPosition(); if ((pos + size) <= readPosition) { if (this.hold()) { ByteBuffer byteBuffer = this.mappedByteBuffer.slice(); byteBuffer.position(pos); ByteBuffer byteBufferNew = byteBuffer.slice(); byteBufferNew.limit(size); return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size + ", fileFromOffset: " + this.fileFromOffset); } return null; } MappedFile();  MappedFile(final String fileName, final int fileSize);  MappedFile(final String fileName, final int fileSize,
        final TransientStorePool transientStorePool); static void ensureDirOK(final String dirName); static void clean(final ByteBuffer buffer); static int getTotalMappedFiles(); static long getTotalMappedVirtualMemory(); void init(final String fileName, final int fileSize,
        final TransientStorePool transientStorePool); long getLastModifiedTimestamp(); int getFileSize(); FileChannel getFileChannel(); AppendMessageResult appendMessage(final MessageExtBrokerInner msg, final AppendMessageCallback cb); AppendMessageResult appendMessages(final MessageExtBatch messageExtBatch, final AppendMessageCallback cb); AppendMessageResult appendMessagesInner(final MessageExt messageExt, final AppendMessageCallback cb); long getFileFromOffset(); boolean appendMessage(final byte[] data); boolean appendMessage(final byte[] data, final int offset, final int length); int flush(final int flushLeastPages); int commit(final int commitLeastPages); int getFlushedPosition(); void setFlushedPosition(int pos); boolean isFull(); SelectMappedBufferResult selectMappedBuffer(int pos, int size); SelectMappedBufferResult selectMappedBuffer(int pos); @Override boolean cleanup(final long currentRef); boolean destroy(final long intervalForcibly); int getWrotePosition(); void setWrotePosition(int pos); int getReadPosition(); void setCommittedPosition(int pos); void warmMappedFile(FlushDiskType type, int pages); String getFileName(); MappedByteBuffer getMappedByteBuffer(); ByteBuffer sliceByteBuffer(); long getStoreTimestamp(); boolean isFirstCreateInQueue(); void setFirstCreateInQueue(boolean firstCreateInQueue); void mlock(); void munlock(); @Override String toString(); }
MappedFile extends ReferenceResource { public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { int readPosition = getReadPosition(); if ((pos + size) <= readPosition) { if (this.hold()) { ByteBuffer byteBuffer = this.mappedByteBuffer.slice(); byteBuffer.position(pos); ByteBuffer byteBufferNew = byteBuffer.slice(); byteBufferNew.limit(size); return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size + ", fileFromOffset: " + this.fileFromOffset); } return null; } MappedFile();  MappedFile(final String fileName, final int fileSize);  MappedFile(final String fileName, final int fileSize,
        final TransientStorePool transientStorePool); static void ensureDirOK(final String dirName); static void clean(final ByteBuffer buffer); static int getTotalMappedFiles(); static long getTotalMappedVirtualMemory(); void init(final String fileName, final int fileSize,
        final TransientStorePool transientStorePool); long getLastModifiedTimestamp(); int getFileSize(); FileChannel getFileChannel(); AppendMessageResult appendMessage(final MessageExtBrokerInner msg, final AppendMessageCallback cb); AppendMessageResult appendMessages(final MessageExtBatch messageExtBatch, final AppendMessageCallback cb); AppendMessageResult appendMessagesInner(final MessageExt messageExt, final AppendMessageCallback cb); long getFileFromOffset(); boolean appendMessage(final byte[] data); boolean appendMessage(final byte[] data, final int offset, final int length); int flush(final int flushLeastPages); int commit(final int commitLeastPages); int getFlushedPosition(); void setFlushedPosition(int pos); boolean isFull(); SelectMappedBufferResult selectMappedBuffer(int pos, int size); SelectMappedBufferResult selectMappedBuffer(int pos); @Override boolean cleanup(final long currentRef); boolean destroy(final long intervalForcibly); int getWrotePosition(); void setWrotePosition(int pos); int getReadPosition(); void setCommittedPosition(int pos); void warmMappedFile(FlushDiskType type, int pages); String getFileName(); MappedByteBuffer getMappedByteBuffer(); ByteBuffer sliceByteBuffer(); long getStoreTimestamp(); boolean isFirstCreateInQueue(); void setFirstCreateInQueue(boolean firstCreateInQueue); void mlock(); void munlock(); @Override String toString(); static final int OS_PAGE_SIZE; }