@NotNull @Override public String dump() { MappedBytes bytes = MappedBytes.mappedBytes(mappedFile); try { bytes.readLimit(bytes.realCapacity()); return Wires.fromSizePrefixedBlobs(bytes); } finally { bytes.release(); } }
long address = bytes.addressForWrite(bytes.writePosition()); long bstart = bytes.start(); long bcap = bytes.realCapacity(); long canWrite = bcap - (bytes.writePosition() - bstart); long lengthCount = batchAppender.writeMessages(address, canWrite, writeCount); bytes.writeSkip((int) lengthCount); lastIndex += lengthCount >> 32; count += lengthCount >> 32;
public synchronized LongValue acquireValueFor(CharSequence key, long defaultValue) { // TODO Change to ThreadLocal values if performance is a problem. StringBuilder sb = Wires.acquireStringBuilder(); mappedBytes.reserve(); try { mappedBytes.readPosition(0); mappedBytes.readLimit(mappedBytes.realCapacity()); while (mappedWire.readDataHeader()) { int header = mappedBytes.readVolatileInt(); if (Wires.isNotComplete(header)) break; long readPosition = mappedBytes.readPosition(); int length = Wires.lengthOf(header); ValueIn valueIn = mappedWire.readEventName(sb); return valueIn.int64ForBinding(null); mappedBytes.readPosition(readPosition + length); int safeLength = Maths.toUInt31(mappedBytes.realCapacity() - mappedBytes.readPosition()); mappedBytes.writeLimit(mappedBytes.realCapacity()); mappedBytes.writePosition(mappedBytes.readPosition()); long pos = recovery.writeHeader(mappedWire, safeLength, timeoutMS, null, null); LongValue longValue = wireType.newLongReference().get(); mappedBytes.release();
private static void dumpFile(@NotNull File file, @NotNull PrintStream out, long upperLimit) { Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer(); try { MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20, OS.pageSize(), !OS.isWindows()); bytes.readLimit(bytes.realCapacity()); StringBuilder sb = new StringBuilder(); WireDumper dumper = WireDumper.of(bytes); while (bytes.readRemaining() >= 4) { sb.setLength(0); boolean last = dumper.dumpOne(sb, buffer); if (bytes.readPosition() > upperLimit) { out.println("# limit reached."); return;
dir.mkdir(); @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE * 2); @NotNull Wire wire = new BinaryWire(bytes); try (DocumentContext dc = wire.writingDocument(true)) { " lastIndex: 0\n" + " }\n" + "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0))); bytes.release();
public Bytes<Void> appendUtf8(CharSequence cs, int start, int length) throws BufferOverflowException, IllegalArgumentException { assert singleThreadedAccess(); long pos = writePosition(); writeCheckOffset(pos, 0); if (!(cs instanceof String) || pos + length * 3 + 5 >= safeLimit()) { super.appendUtf8(cs, start, length); return this; long address = addressForWrite(pos); Memory memory = OS.memory(); int i = 0; byte c = bytes[i + start]; if (c > 127) { writeSkip(i); break non_ascii; writeSkip(length); return this; appendUtf8(c); long address = addressForWrite(pos); Memory memory = OS.memory(); int i = 0; char c = chars[i + start];
long start1 = System.currentTimeMillis(); String filename = baseDir + "/" + count++; queues[i] = MappedBytes.mappedBytes(filename, pagesPer10Second * (4 << 10)); long time1 = System.currentTimeMillis() - start1; if (time1 > 20) for (int i = 0; i < pagesPer10Second; i++) { for (MappedBytes bytes : queues) { bytes.writeLong(i * (4 << 10), i); bytes.release();
/** * @return creates a new instance of mapped bytes, because, for example the tailer and appender * can be at different locations. */ @NotNull @Override public MappedBytes bytes() { return MappedBytes.mappedBytes(mappedFile); }
private void removeEOF(Path path) throws IOException { long blockSize = 64 << 10; long chunkSize = OS.pageAlign(blockSize); long overlapSize = OS.pageAlign(blockSize / 4); final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false); mappedBytes.reserve(); try { final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes); final Bytes<?> bytes = wire.bytes(); bytes.readLimit(bytes.capacity()); bytes.readSkip(4); // move past header try (final SingleChronicleQueueStore qs = loadStore(wire)) { assertNotNull(qs); long l = qs.writePosition(); long len = Wires.lengthOf(bytes.readVolatileInt(l)); long eofOffset = l + len + 4L; bytes.writePosition(eofOffset); bytes.writeInt(0); } } finally { mappedBytes.release(); } }
@NotNull @Override public Bytes<Void> append8bit(@NotNull CharSequence cs, int start, int end) throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { assert singleThreadedAccess(); // check the start. long pos = writePosition(); writeCheckOffset(pos, 0); if (!(cs instanceof String) || pos + (end - start) * 3 + 5 >= safeLimit()) { return super.append8bit(cs, start, end); } return append8bit0((String) cs, start, end - start); }
@NotNull private MappedBytes append8bit0(@NotNull String s, int start, int length) throws BufferOverflowException { assert singleThreadedAccess(); if (Jvm.isJava9Plus()) { byte[] bytes = extractBytes(s); long address = addressForWrite(writePosition()); Memory memory = bytesStore().memory; int i = 0; for (; i < length - 3; i += 4) { memory.writeByte(address++, c); writeSkip(length); } else { char[] chars = extractChars(s); long address = addressForWrite(writePosition()); Memory memory = bytesStore().memory; int i = 0; for (; i < length - 3; i += 4) { memory.writeByte(address++, (byte) c); writeSkip(length);
@NotNull @Override public Bytes<Void> readPositionRemaining(long position, long remaining) throws BufferUnderflowException { long limit = position + remaining; acquireNextByteStore(position, true); if (writeLimit < limit) writeLimit(limit); boolean debug = false; assert debug = true; if (debug) readLimit(limit); else uncheckedWritePosition(limit); return readPosition(position); }
public long rawCopy(@NotNull BytesStore bytes, long offset, long length) throws BufferOverflowException, BufferUnderflowException { assert length < safeCopySize(); this.acquireNextByteStore(writePosition(), false); long len = Math.min(writeRemaining(), Math.min(bytes.readRemaining(), length)); if (len > 0) { OS.memory().copyMemory(bytes.addressForRead(offset), addressForWrite(writePosition()), len); uncheckedWritePosition(writePosition() + len); } return len; }
dir.mkdir(); @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE); @NotNull Wire wire = new BinaryWire(bytes); try (DocumentContext dc = wire.writingDocument(true)) { bytes.release(); try (@NotNull ChronicleQueue queue = binary(dir) .rollCycle(RollCycles.TEST4_DAILY)
static File getFile(MappedBytes bytes) { if (bytes == null) return new File("none"); MappedFile mappedFile = bytes.mappedFile(); assert (mappedFile != null); File file = mappedFile.file(); assert (file != null); return file; }
private void onCleanup() { mappedBytes.release(); }
@Test public void testInvalidFile() throws FileNotFoundException { @NotNull File dir = new File(OS.TARGET + "/deleteme-" + System.nanoTime()); dir.mkdir(); try (@NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700102" + SingleChronicleQueue.SUFFIX), 64 << 10)) { bytes.write8bit("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"); try (@NotNull RollingChronicleQueue queue = binary(dir) .rollCycle(RollCycles.TEST4_DAILY) .testBlockSize() .build()) { assertEquals(1, queue.firstCycle()); assertEquals(1, queue.lastCycle()); try { @NotNull ExcerptTailer tailer = queue.createTailer(); tailer.toEnd(); fail(); } catch (Exception e) { assertEquals("java.io.StreamCorruptedException: Unexpected magic number 783f3c37", e.toString()); } } } System.gc(); try { IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } }
private synchronized void acquireNextByteStore0(final long offset, final boolean set) { @Nullable BytesStore oldBS = this.bytesStore; try { @Nullable BytesStore newBS = mappedFile.acquireByteStore(offset); this.bytesStore = newBS; oldBS.release(); } catch (@NotNull IOException | IllegalStateException | IllegalArgumentException e) { @NotNull BufferOverflowException boe = new BufferOverflowException(); boe.initCause(e); throw boe; } if (set) { if (writeLimit() < readPosition) writeLimit(readPosition); if (readLimit() < readPosition) readLimit(readPosition); readPosition = offset; } }
@NotNull @Override public Bytes<Void> readSkip(long bytesToSkip) throws BufferUnderflowException { if (readPosition + bytesToSkip > readLimit()) throw new BufferUnderflowException(); long check = bytesToSkip >= 0 ? this.readPosition : this.readPosition + bytesToSkip; if (bytesStore instanceof NoBytesStore || bytesToSkip != (int) bytesToSkip || !bytesStore.inside(readPosition, (int) bytesToSkip)) { acquireNextByteStore0(check, false); } this.readPosition += bytesToSkip; return this; }
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(); }