@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(); }
public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile firstMappedFile = this.getFirstMappedFile(); MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } if (returnFirstOnNotFound) { return firstMappedFile; } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; }
MappedFileQueue { public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile firstMappedFile = this.getFirstMappedFile(); MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } if (returnFirstOnNotFound) { return firstMappedFile; } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; } }
MappedFileQueue { public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile firstMappedFile = this.getFirstMappedFile(); MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } if (returnFirstOnNotFound) { return firstMappedFile; } } } catch (Exception e) { log.error("findMappedFileByOffset Exception", e); } return null; } MappedFileQueue(final String storePath, int mappedFileSize,
        AllocateMappedFileService allocateMappedFileService); }
MappedFileQueue { public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile firstMappedFile = this.getFirstMappedFile(); MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } if (returnFirstOnNotFound) { return firstMappedFile; } } } 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); 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); }
MappedFileQueue { public MappedFile findMappedFileByOffset(final long offset, final boolean returnFirstOnNotFound) { try { MappedFile firstMappedFile = this.getFirstMappedFile(); MappedFile lastMappedFile = this.getLastMappedFile(); if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { LOG_ERROR.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", offset, firstMappedFile.getFileFromOffset(), lastMappedFile.getFileFromOffset() + this.mappedFileSize, this.mappedFileSize, this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; try { targetFile = this.mappedFiles.get(index); } catch (Exception ignored) { } if (targetFile != null && offset >= targetFile.getFileFromOffset() && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } } if (returnFirstOnNotFound) { return firstMappedFile; } } } 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); 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);  }