@NotNull @Override public File file() { return mappedFile.file(); }
@NotNull @Override public WireStore writePosition(long position) { assert singleThreadedAccess(); assert writePosition.getVolatileValue() + mappedFile.chunkSize() > position; assert Wires.isReadyData(mappedBytes.readVolatileInt(position)); writePosition.setMaxValue(position); return this; }
@NotNull private MappedFile mappedFile(File file) throws FileNotFoundException { long chunkSize = OS.pageAlign(blockSize); long overlapSize = OS.pageAlign(blockSize / 4); return MappedFile.of(file, chunkSize, overlapSize, readOnly); }
@Override public long realCapacity() { try { return mappedFile.actualSize(); } catch (IORuntimeException e) { Jvm.warn().on(getClass(), "Unable to obtain the real size for " + mappedFile.file(), e); return 0; } }
@Override public boolean writeEOF(@NotNull Wire wire, long timeoutMS) { String fileName = mappedFile.file().getAbsolutePath(); // just in case we are about to release this if (wire.bytes().tryReserve()) { try { wire.writeEndOfWire(timeoutMS, TimeUnit.MILLISECONDS, writePosition()); if (wire.bytes().writePosition() > 4 && Wires.isEndOfFile(wire.bytes().readVolatileInt(wire.bytes().writePosition() - 4))) { // only if we just written EOF QueueFileShrinkManager.scheduleShrinking(mappedFile.file(), wire.bytes().writePosition()); return true; } } finally { wire.bytes().release(); } } else { try (MappedBytes bytes = MappedBytes.mappedBytes(mappedFile.file(), mappedFile.chunkSize())) { Wire wire0 = WireType.valueOf(wire).apply(bytes); wire0.writeEndOfWire(timeoutMS, TimeUnit.MILLISECONDS, writePosition()); if (wire.bytes().writePosition() > 4 && Wires.isEndOfFile(wire.bytes().readVolatileInt(wire.bytes().writePosition() - 4))) { QueueFileShrinkManager.scheduleShrinking(mappedFile.file(), wire.bytes().writePosition()); return true; } } catch (Exception e) { Jvm.warn().on(getClass(), "unable to write the EOF file=" + fileName, e); } } return false; }
protected MappedBytes(@NotNull MappedFile mappedFile, String name) throws IllegalStateException { super(NoBytesStore.noBytesStore(), NoBytesStore.noBytesStore().writePosition(), NoBytesStore.noBytesStore().writeLimit(), name); this.mappedFile = reserve(mappedFile); this.backingFileIsReadOnly = !mappedFile.file().canWrite(); assert !mappedFile.isClosed(); clear(); }
if (bytes instanceof MappedBytes) { MappedBytes mb = (MappedBytes) bytes; sizeToSkip = Maths.toUInt31(mb.mappedFile().overlapSize() / 2);
public static void warmup() { try { Jvm.disableDebugHandler(); @NotNull File file = File.createTempFile("delete", "me"); file.deleteOnExit(); long mapAlignment = OS.mapAlignment(); int chunks = 64; int compileThreshold = Jvm.compileThreshold(); for (int j = 0; j <= compileThreshold; j += chunks) { try { try (@NotNull RandomAccessFile raf = new RandomAccessFile(file, "rw")) { @NotNull MappedFile mappedFile = new MappedFile(file, raf, mapAlignment, 0, mapAlignment * chunks, false); warmup0(mapAlignment, chunks, mappedFile); mappedFile.release(); } Thread.yield(); Files.delete(file.toPath()); } catch (IOException e) { Jvm.debug().on(MappedFile.class, "Error during warmup", e); } } } catch (IOException e) { Jvm.warn().on(MappedFile.class, "Error during warmup", e); } Jvm.resetExceptionHandlers(); }
private static void warmup0(long mapAlignment, int chunks, @NotNull MappedFile mappedFile) throws IOException { for (int i = 0; i < chunks; i++) { mappedFile.acquireBytesForRead(i * mapAlignment).release(); mappedFile.acquireBytesForWrite(i * mapAlignment).release(); } }
@NotNull public MappedFile withSizes(long chunkSize, long overlapSize) { chunkSize = OS.mapAlign(chunkSize); overlapSize = OS.mapAlign(overlapSize); if (chunkSize == this.chunkSize && overlapSize == this.overlapSize) return this; try { return new MappedFile(file, raf, chunkSize, overlapSize, capacity, readOnly); } finally { release(); } }
MappedUniqueMicroTimeProvider() { try { file = MappedFile.mappedFile(OS.TMP + "/.time-stamp.dat", OS.pageSize(), 0); bytes = file.acquireBytesForWrite(0); bytes.append8bit("&TSF\nTime stamp file uses for sharing a unique id\n"); BytesUtil.unregister(bytes); } catch (IOException ioe) { throw new IORuntimeException(ioe); } }
@NotNull public MappedBytesStore acquireByteStore(long position) throws IOException, IllegalArgumentException, IllegalStateException { return acquireByteStore(position, readOnly ? ReadOnlyMappedBytesStore::new : MappedBytesStore::new); }
@NotNull public static MappedFile mappedFile(@NotNull File file, long capacity, long chunkSize, long overlapSize, boolean readOnly) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, readOnly ? "r" : "rw"); // Windows throws an exception when setting the length when you re-open if (raf.length() < capacity) raf.setLength(capacity); return new MappedFile(file, raf, chunkSize, overlapSize, capacity, readOnly); }
public long actualSize() throws IORuntimeException { boolean interrupted = Thread.interrupted(); try { return fileChannel.size(); } catch (ArrayIndexOutOfBoundsException aiooe) { // try again. return actualSize(); } catch (ClosedByInterruptException cbie) { closed.set(true); interrupted = true; throw new IllegalStateException(cbie); } catch (IOException e) { boolean open = fileChannel.isOpen(); if (open) { throw new IORuntimeException(e); } else { closed.set(true); throw new IllegalStateException(e); } } finally { if (interrupted) Thread.currentThread().interrupt(); } }
@NotNull public static MappedBytes mappedBytes(@NotNull File file, long chunkSize, long overlapSize, boolean readOnly) throws FileNotFoundException, IllegalStateException { @NotNull MappedFile rw = MappedFile.of(file, chunkSize, overlapSize, readOnly); try { return mappedBytes(rw); } finally { rw.release(); } }
public MappedBytes write(long offsetInRDO, byte[] bytes, int offset, int length) { long wp = offsetInRDO; if ((length + offset) > bytes.length) throw new ArrayIndexOutOfBoundsException("bytes.length=" + bytes.length + ", " + "length=" + length + ", offset=" + offset); if (length > writeRemaining()) throw new DecoratedBufferOverflowException( String.format("write failed. Length: %d > writeRemaining: %d", length, writeRemaining())); int remaining = length; acquireNextByteStore(wp, false); while (remaining > 0) { long safeCopySize = copySize(wp); if (safeCopySize + mappedFile.overlapSize() >= remaining) { bytesStore.write(wp, bytes, offset, remaining); return this; } bytesStore.write(wp, bytes, offset, (int) safeCopySize); offset += safeCopySize; wp += safeCopySize; remaining -= safeCopySize; // move to the next chunk acquireNextByteStore0(wp, false); } return this; }
public void acquireBytesForRead(long position, @NotNull VanillaBytes bytes) throws IOException, IllegalStateException, IllegalArgumentException { @Nullable MappedBytesStore mbs = acquireByteStore(position); bytes.bytesStore(mbs, position, mbs.capacity() - position); }
@NotNull public static MappedFile of(@NotNull File file, long chunkSize, long overlapSize, boolean readOnly) throws FileNotFoundException { // if (readOnly && OS.isWindows()) { // Jvm.warn().on(MappedFile.class, "Read only mode not supported on Windows, defaulting to read/write"); // readOnly = false; // } @NotNull RandomAccessFile raf = new RandomAccessFile(file, readOnly ? "r" : "rw"); // try { long capacity = /*readOnly ? raf.length() : */DEFAULT_CAPACITY; return new MappedFile(file, raf, chunkSize, overlapSize, capacity, readOnly); /* } catch (IOException e) { Closeable.closeQuietly(raf); @NotNull FileNotFoundException fnfe = new FileNotFoundException("Unable to open " + file); fnfe.initCause(e); throw fnfe; } */ }
@NotNull @Override public File file() { return mappedFile.file(); }