@Override public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, @NotNull final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException { Jvm.warn().on(getClass(), "Clearing an incomplete header so a header can be written"); wire.bytes().writeInt(0); wire.pauser().reset(); try { return wire.writeHeaderOfUnknownLength(timeoutMS, TimeUnit.MILLISECONDS, lastPosition, sequence); } catch (@NotNull TimeoutException | EOFException e) { throw new UnrecoverableTimeoutException(e); } } }
static void writeMany(Bytes bytes, int size) { for (int i = 0; i < size; i += 32) { bytes.writeInt(i);// 4 bytes bytes.writeFloat(i);// 4 bytes bytes.writeLong(i);// 8 bytes bytes.writeDouble(i);// 8 bytes bytes.writeUtf8("Hello!!"); // 8 bytes } }
@Test public void testCheckIndexWithWritingDocument2() { doTestCheckIndex( (appender, n) -> { try (final DocumentContext dc = appender.writingDocument()) { dc.wire().bytes().writeUtf8("Hello") .writeStopBit(12345) .writeStopBit(1.2) // float also supported. .writeInt(1); } }); }
files = dir.listFiles(pathname -> pathname.getAbsolutePath().endsWith(".cq4")); wire.bytes().writeInt(wp, Wires.END_OF_DATA); appender.writeText("hello world 2");
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(); } }
@Test public void testWriteToCorruptedFile() { File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile"); try (ChronicleQueue queue = SingleChronicleQueueBuilder .binary(dir) .testBlockSize() .rollCycle(RollCycles.TEST_DAILY) .build()) { ExcerptAppender appender = queue.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write().text("hello world"); } Bytes bytes; long pos; try (DocumentContext dc = appender.writingDocument()) { bytes = dc.wire().bytes(); pos = bytes.writePosition() - 4; } // write as not complete. bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write().text("hello world 2"); } try (DocumentContext dc = appender.writingDocument()) { dc.wire().write().text("hello world 3"); } } }
@Override public void write(Bytes out, @NotNull Integer toWrite) { out.writeInt(toWrite); }
@Override public void write(@NotNull Bytes out, long size, @NotNull Integer toWrite) { out.writeInt(toWrite); }
@Override public void write(Bytes out, @NotNull List<T> toWrite) { out.writeInt(toWrite.size()); // indexed loop to avoid garbage creation //noinspection ForLoopReplaceableByForEach for (int i = 0; i < toWrite.size(); i++) { elementWriter.write(out, toWrite.get(i)); } }
@Override public void write(Bytes out, @NotNull Set<T> toWrite) { out.writeInt(toWrite.size()); toWrite.forEach(e -> elementWriter.write(out, e)); }
@Override public void write(Bytes out, @NotNull Map<K, V> toWrite) { out.writeInt(toWrite.size()); toWrite.forEach((k, v) -> { keyWriter.write(out, k); valueWriter.write(out, v); }); }
public ChronicleBytesCASPingPongMain() throws IOException { bytes = MappedBytes.mappedBytes("deleteme", OS.pageSize()); bytes.writeLong(COUNTER_OFFSET, 0L); bytes.writeInt(TOGGLE_OFFSET, 0); }
@Override public void write(Bytes out, @NotNull HistogramKey toWrite) { int accumulatorKeySize = 5; out.writeByte(toWrite.granularityOrdinal); out.writeInt(toWrite.binId); accumulatorKeySize += 2 + toWrite.metric.length(); writeString(out, toWrite.metric); accumulatorKeySize += 2 + (toWrite.source == null ? 0 : toWrite.source.length()); writeString(out, toWrite.source); short numTags = toWrite.tags == null ? 0 : (short) toWrite.tags.length; accumulatorKeySize += 2; out.writeShort(numTags); for (short i = 0; i < numTags; ++i) { accumulatorKeySize += 2 + (toWrite.tags[i] == null ? 0 : toWrite.tags[i].length()); writeString(out, toWrite.tags[i]); } accumulatorKeySizes.update(accumulatorKeySize); } }
@Override public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, @NotNull final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException { Jvm.warn().on(getClass(), "Clearing an incomplete header so a header can be written"); wire.bytes().writeInt(0); wire.pauser().reset(); try { return wire.writeHeaderOfUnknownLength(timeoutMS, TimeUnit.MILLISECONDS, lastPosition, sequence); } catch (@NotNull TimeoutException | EOFException e) { throw new UnrecoverableTimeoutException(e); } } }
@Override public void write(Bytes out, @NotNull HistogramKey toWrite) { int accumulatorKeySize = 5; out.writeByte(toWrite.granularityOrdinal); out.writeInt(toWrite.binId); accumulatorKeySize += 2 + toWrite.metric.length(); writeString(out, toWrite.metric); accumulatorKeySize += 2 + (toWrite.source == null ? 0 : toWrite.source.length()); writeString(out, toWrite.source); short numTags = toWrite.tags == null ? 0 : (short) toWrite.tags.length; accumulatorKeySize += 2; out.writeShort(numTags); for (short i = 0; i < numTags; ++i) { accumulatorKeySize += 2 + (toWrite.tags[i] == null ? 0 : toWrite.tags[i].length()); writeString(out, toWrite.tags[i]); } accumulatorKeySizes.update(accumulatorKeySize); } }