private static boolean processWireQueue() { Wire wireToRelease; boolean released = false; while ((wireToRelease = WIRES_TO_RELEASE.poll()) != null) { try { released = true; wireToRelease.bytes().release(); } catch (IllegalStateException e) { // ignore this - resource may have already been released by explicit close() operation } catch (Throwable t) { LOGGER.warn("Failed to release wire bytes", t); } } return released; }
private void doRollback(boolean interrupted) { if (interrupted) LOG.warn("Thread is interrupted. Can't guarantee complete message, so not committing"); // zero out all contents... for (long i = position; i <= wire.bytes().writePosition(); i++) wire.bytes().writeByte(i, (byte) 0); position = lastPosition; wire.bytes().writePosition(position); ((AbstractWire) wire).forceNotInsideHeader(); }
@NotNull private Wire readAnywhere(@NotNull Wire wire) { Bytes<?> bytes = wire.bytes(); bytes.readLimit(bytes.capacity()); return wire; }
private static MessageHistory readHistoryFromBytes(final Wire wire, MessageHistory history) { final Bytes<?> bytes = wire.bytes(); if (MESSAGE_HISTORY_METHOD_ID != wire.readEventNumber()) return null; ((BytesMarshallable) history).readMarshallable(bytes); return history; }
@NotNull private ScanResult moveToIndexFromTheStart(@NotNull ExcerptContext ec, long index) { try { Wire wire = ec.wire(); wire.bytes().readPositionUnlimited(0); if (wire.readDataHeader()) return linearScan(wire, index, 0, wire.bytes().readPosition()); } catch (EOFException fallback) { return ScanResult.END_OF_FILE; } return ScanResult.NOT_FOUND; }
@Override public void writeEOF(@NotNull Wire wire, long timeoutMS) { // just in case we are about to release this if (wire.bytes().tryReserve()) { wire.writeEndOfWire(timeoutMS, TimeUnit.MILLISECONDS, writePosition()); wire.bytes().release(); } else { Jvm.debug().on(getClass(), "Tried to writeEOF to as it was being closed"); } }
@NotNull private LongArrayValues arrayForAddress(@NotNull Wire wire, long secondaryAddress) { LongArrayValuesHolder holder = getIndexArray(); if (holder.address == secondaryAddress) return holder.values; holder.address = secondaryAddress; wire.bytes().readPositionRemaining(secondaryAddress, 4); // to read the header. wire.readMetaDataHeader(); return array(wire, holder.values, false); }
private void close() { // the wire ref count will be released here by setting it to null context.wire(null); Wire w0 = wireForIndex; if (w0 != null) w0.bytes().release(); wireForIndex = null; if (store != null) { queue.release(store); } store = null; }
@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); } } }
@NotNull static SingleChronicleQueueStore createStore(@NotNull RollingChronicleQueue queue, @NotNull Wire wire) { final SingleChronicleQueueStore wireStore = new SingleChronicleQueueStore( queue.rollCycle(), queue.wireType(), (MappedBytes) wire.bytes(), queue.indexCount(), queue.indexSpacing()); wire.writeEventName(MetaDataKeys.header).typedMarshallable(wireStore); return wireStore; }
private MappedFile toMappedFile(@NotNull DocumentContext documentContext) { MappedFile mappedFile; MappedBytes bytes = (MappedBytes) documentContext.wire().bytes(); mappedFile = bytes.mappedFile(); return mappedFile; }
private boolean canReuseLastIndexMove( final long index, final TailerState state, final TailerDirection direction, final ChronicleQueue queue, final Wire wire) { return ((wire == null) || wire.bytes().readPosition() == readPositionAtLastMove) && index == this.lastMovedToIndex && index != 0 && state == FOUND_CYCLE && direction == directionAtLastMoveTo && queue.rollCycle().toCycle(index) == queue.rollCycle().toCycle(lastMovedToIndex); }
public int fetchOneMessage(@NotNull ExcerptTailer tailer, @NotNull byte[] using) { try (DocumentContext dc = tailer.readingDocument()) { return !dc.isPresent() ? -1 : dc.wire().bytes().read(using); } }
@Override public void run(long startTimeNS) { datum.ts = startTimeNS; try (DocumentContext dc = appender.writingDocument()) { datum.writeMarshallable(dc.wire().bytes()); } }
private void resetPosition() throws UnrecoverableTimeoutException { try { if (store == null || wire == null) return; position(store.writePosition()); assert position == 0 || Wires.isReadyData(wire.bytes().readVolatileInt(position)); final long headerNumber = store.lastSequenceNumber(this); wire.headerNumber(queue.rollCycle().toIndex(cycle, headerNumber + 1) - 1); assert wire.headerNumber() != -1 || checkIndex(wire.headerNumber(), position); } catch (@NotNull BufferOverflowException | StreamCorruptedException e) { throw new AssertionError(e); } assert checkWritePositionHeaderNumber(); }
private BytesWithIndex bytes(final ExcerptTailer tailer) { try (DocumentContext dc = tailer.readingDocument()) { if (!dc.isPresent()) return null; Bytes<?> bytes = dc.wire().bytes(); long index = dc.index(); return new BytesWithIndex(bytes, index); } }
private void resetWires() { WireType wireType = queue.wireType(); final AbstractWire wire = (AbstractWire) readAnywhere(wireType.apply(store.bytes())); assert headerNumberCheck(wire); this.context.wire(wire); wire.parent(this); Wire wireForIndexOld = wireForIndex; wireForIndex = readAnywhere(wireType.apply(store().bytes())); closableResources.wireForIndexReference = wireForIndex.bytes(); closableResources.wireReference = wire.bytes(); assert headerNumberCheck((AbstractWire) wireForIndex); assert wire != wireForIndexOld; if (wireForIndexOld != null) { releaseWireResources(wireForIndexOld); } }
@Before public void setup() { chroniclePath = new File(OS.TARGET, "read_only"); try (ChronicleQueue readWrite = ChronicleQueue.singleBuilder(chroniclePath) .readOnly(false) .testBlockSize() .build()) { final ExcerptAppender appender = readWrite.acquireAppender(); appender.writeText(STR1); try (DocumentContext dc = appender.writingDocument()) { dc.wire().bytes().writeUtf8(STR2); } } }
@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); } }); }