MappedFileQueue { public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile mappedFile = this.getFirstMappedFile(); if (mappedFile != null) { int index = (int) ((offset / this.mappedFileSize) - (mappedFile.getFileFromOffset() / this.mappedFileSize)); if (index < 0 || index >= this.mappedFiles.size()) { LOG_ERROR.warn("Offset for {} not matched. Request offset: {}, index: {}, " + "mappedFileSize: {}, mappedFiles count: {}", mappedFile, offset, index, this.mappedFileSize, this.mappedFiles.size()); } try { return this.mappedFiles.get(index); } catch (Exception e) { if (returnFirstOnNotFound) { return mappedFile; } LOG_ERROR.warn("findMappedFileByOffset failure. ", e); } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; } MappedFileQueue(final String storePath, int mappedFileSize,
        AllocateMappedFileService allocateMappedFileService); void checkSelf(); MappedFile getMappedFileByTime(final long timestamp); void truncateDirtyFiles(long offset); boolean load(); long howMuchFallBehind(); MappedFile getLastMappedFile(final long startOffset, boolean needCreate); MappedFile getLastMappedFile(final long startOffset); MappedFile getLastMappedFile(); boolean resetOffset(long offset); long getMinOffset(); long getMaxOffset(); long getMaxWrotePosition(); long remainHowManyDataToCommit(); long remainHowManyDataToFlush(); void deleteLastMappedFile(); int deleteExpiredFileByTime(final long expiredTime,
        final int deleteFilesInterval,
        final long intervalForcibly,
        final boolean cleanImmediately); int deleteExpiredFileByOffset(long offset, int unitSize); boolean flush(final int flushLeastPages); boolean commit(final int commitLeastPages); MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound); void deleteMappedFileByOffset(final long offset); MappedFile getFirstMappedFile(); MappedFile findMappedFileByOffset(final long offset); long getMappedMemorySize(); boolean retryDeleteFirstFile(final long intervalForcibly); void shutdown(final long intervalForcibly); void destroy(); long getFlushedWhere(); void setFlushedWhere(long flushedWhere); long getStoreTimestamp(); List<MappedFile> getMappedFiles(); int getMappedFileSize(); long getCommittedWhere(); void setCommittedWhere(final long committedWhere);  }
@Test public void testFindMappedFileByOffset() { final String fixedMsg = "abcd"; MappedFileQueue mappedFileQueue = new MappedFileQueue("target/unit_test_store/b/", 1024, null); for (int i = 0; i < 1024; i++) { MappedFile mappedFile = mappedFileQueue.getLastMappedFile(0); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.appendMessage(fixedMsg.getBytes())).isTrue(); } assertThat(mappedFileQueue.getMappedMemorySize()).isEqualTo(fixedMsg.getBytes().length * 1024); MappedFile mappedFile = mappedFileQueue.findMappedFileByOffset(0); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(0); mappedFile = mappedFileQueue.findMappedFileByOffset(100); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(0); mappedFile = mappedFileQueue.findMappedFileByOffset(1024); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(1024); mappedFile = mappedFileQueue.findMappedFileByOffset(1024 + 100); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(1024); mappedFile = mappedFileQueue.findMappedFileByOffset(1024 * 2); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(1024 * 2); mappedFile = mappedFileQueue.findMappedFileByOffset(1024 * 2 + 100); assertThat(mappedFile).isNotNull(); assertThat(mappedFile.getFileFromOffset()).isEqualTo(1024 * 2); mappedFile = mappedFileQueue.findMappedFileByOffset(1024 * 4); assertThat(mappedFile).isNull(); mappedFile = mappedFileQueue.findMappedFileByOffset(1024 * 4 + 100); assertThat(mappedFile).isNull(); mappedFileQueue.shutdown(1000); mappedFileQueue.destroy(); }