private SCQIndexing(int indexCount, int indexSpacing, LongValue index2Index, LongValue nextEntryToBeIndexed, Supplier<LongArrayValues> longArraySupplier) { this.indexCount = indexCount; this.indexCountBits = Maths.intLog2(indexCount); this.indexSpacing = indexSpacing; this.indexSpacingBits = Maths.intLog2(indexSpacing); this.index2Index = index2Index; this.nextEntryToBeIndexed = nextEntryToBeIndexed; this.longArraySupplier = longArraySupplier; this.index2indexArray = new ThreadLocal<>(); this.indexArray = new ThreadLocal<>(); this.index2IndexTemplate = w -> w.writeEventName(() -> "index2index").int64array(indexCount); this.indexTemplate = w -> w.writeEventName(() -> "index").int64array(indexCount); }
/** * Creates a new Excerpt containing and index which will be 1L << 17L bytes long, This method is * used for creating both the primary and secondary indexes. Chronicle Queue uses a root primary * index ( each entry in the primary index points to a unique a secondary index. The secondary * index only records the addressForRead of every 64th except, the except are linearly scanned from * there on. ) * * @param wire the current wire * @return the addressForRead of the Excerpt containing the usable index, just after the header */ long newIndex(@NotNull WireOut wire, boolean index2index) throws StreamCorruptedException { long writePosition = this.writePosition.getVolatileValue(); Bytes<?> bytes = wire.bytes(); bytes.writePosition(writePosition); long position = wire.enterHeader(indexCount * 8 + 128); WriteMarshallable writer = index2index ? index2IndexTemplate : indexTemplate; writer.writeMarshallable(wire); wire.updateHeader(position, true, 0); return position; }
@Override public void writeMarshallable(@NotNull WireOut wire) { ValueOut wireOut = wire.write(MetaDataField.writePosition); intForBinding(wireOut, writePosition).write(MetaDataField.indexing).typedMarshallable(this.indexing); wire.padToCacheAlign(); }
@Override public void writeMarshallable(@NotNull WireOut wire) { wire .write(MetaDataField.wireType).object(wireType) .write(MetaDataField.recovery).typedMarshallable(recovery); if (metadata != Metadata.NoMeta.INSTANCE) wire.write(MetaDataField.metadata).typedMarshallable(this.metadata); // align to a word whether needed or not as a micro-optimisation. wire.writeAlignTo(Integer.BYTES, 0); }
w.writeDocument(true, d -> d.write(CoreFields.cid).int64(cid())); w.writeDocument(false, d -> { d.writeEventName(CoreFields.lastUpdateTime).int64(lastUpdateTime); d.write(() -> "id").int8(id); }); }); w.writeDocument(true, d -> d.write(CoreFields.cid).int64(cid())); w.writeDocument(false, d -> { d.writeEventName(replicationEvent).marshallable(e); d.writeComment("isAcceptor=" + nc().isAcceptor()); }); }));
outWire.writeDocument(true, w -> w.writeEventName(CoreFields.tid).int64 (CollectionWireHandler.this.tid)); outWire.writeDocument(false, out -> { outWire.write(CoreFields.reply).bool(underlyingCollection.remove(fromWire.apply(valueIn))); return; @NotNull final ValueOut valueOut = out.writeEventName(CoreFields.reply); valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e))); return; outWire.write(CoreFields.reply).int32(1); return; outWire.write(CoreFields.reply).bool(underlyingCollection.isEmpty()); return; outWire.write(CoreFields.reply).int32(underlyingCollection.size()); return; outWire.write(CoreFields.reply).bool( underlyingCollection.contains(fromWire.apply(valueIn))); return; outWire.write(CoreFields.reply).bool( underlyingCollection.add(fromWire.apply(valueIn))); return;
private static WriteMarshallable heartbeatHandler(final long heartbeatTimeoutMs, final long heartbeatIntervalMs, final long cid) { return w -> w.writeDocument(true, d -> d.writeEventName(CoreFields.csp).text("/") .writeEventName(CoreFields.cid).int64(cid) .writeEventName(CoreFields.handler).typedMarshallable(new HeartbeatHandler(heartbeatTimeoutMs, heartbeatIntervalMs))); }
@Override public void onSubscribe(@NotNull final WireOut wireOut) { subscribersToTid.put(subscriber, tid()); wireOut.writeEventName(registerTopicSubscriber).marshallable(m -> { m.write(() -> "keyType").typeLiteral(kClass); m.write(() -> "valueType").typeLiteral(vClass); if (rc.bootstrap() != null) m.writeEventName(() -> "bootstrap").bool(rc.bootstrap()); }); }
publisher.put(key, publish -> { publish.writeDocument(true, wire -> wire.writeEventName(tid).int64 (inputTid)); publish.writeNotCompleteDocument(false, wire -> wire.writeEventName(reply) .marshallable(m -> m.write(Params.message).object(message))); });
private void dumpRules(@NotNull Wire wire) { wire.bytes().append8bit("---\n"); wire.write("name").text(fullName()) .write("leaf").marshallable(w -> { for (@NotNull Map.Entry<Class, LeafView> entry : leafViewMap.entrySet()) { w.writeEvent(Class.class, entry.getKey()).leaf(false) .text(entry.getValue().name); } }) .write("wrapping").marshallable(w -> { for (@NotNull Map.Entry<Class, SortedMap<String, WrappingViewRecord>> entry : wrappingViewFactoryMap.entrySet()) { w.writeEvent(Class.class, entry.getKey()).marshallable(ww -> { for (@NotNull Map.Entry<String, WrappingViewRecord> recordEntry : entry.getValue().entrySet()) { ww.writeEventName(recordEntry.getKey()).object(Class.class, recordEntry.getValue().underlyingType); } }); } }); }
outWire.writeDocument(true, wire -> outWire.writeEventName(CoreFields.tid).int64(tid)); writeData(inWire, out -> outWire.write(identifierReply).int8(hostId.hostId())); outBootstrap.identifier(hostId.hostId()); outBootstrap.lastUpdatedTime(replication.lastModificationTime(id)); outWire.writeEventName(bootstrap).typedMarshallable(outBootstrap);
/** * write and exceptions and rolls back if no data was written */ void writeData(boolean isNotComplete, @NotNull Bytes inBytes, @NotNull WriteMarshallable c) { @NotNull final WriteMarshallable marshallable = out -> { final long readPosition = inBytes.readPosition(); final long position = outWire.bytes().writePosition(); try { c.writeMarshallable(outWire); } catch (Throwable t) { inBytes.readPosition(readPosition); if (LOG.isInfoEnabled()) LOG.info("While reading " + inBytes.toDebugString(), " processing wire " + c, t); outWire.bytes().writePosition(position); outWire.writeEventName(() -> "exception").throwable(t); } // write 'reply : {} ' if no data was sent if (position == outWire.bytes().writePosition()) { outWire.writeEventName(reply).marshallable(EMPTY); } }; if (isNotComplete) outWire.writeNotCompleteDocument(false, marshallable); else outWire.writeDocument(false, marshallable); logYaml(); }
/** * write and exceptions and rolls back if no data was written */ void writeData(@NotNull WireIn wireIn, @NotNull WriteMarshallable c) { @NotNull Bytes inBytes = wireIn.bytes(); outWire.writeDocument(false, out -> { final long readPosition = inBytes.readPosition(); final long position = outWire.bytes().writePosition(); try { c.writeMarshallable(outWire); } catch (Throwable t) { final String readingYaml = wireIn.readingPeekYaml(); inBytes.readPosition(readPosition); if (LOG.isInfoEnabled()) LOG.info("While readingBytes=" + inBytes.toDebugString() + "\nreadingYaml=" + readingYaml, "\nprocessing wire " + c, t); outWire.bytes().writePosition(position); outWire.writeEventName(() -> "readingYaml").text(readingYaml); outWire.writeEventName(() -> "exception").throwable(t); } // write 'reply : {} ' if no data was sent if (position == outWire.bytes().writePosition()) { outWire.writeEventName(reply).marshallable(EMPTY); } }); logYaml(); }
@Test public void testLastIndexAppended() { try (ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(w -> w.writeEventName("hello").text("world0")); final long nextIndexToWrite = appender.lastIndexAppended() + 1; appender.writeDocument(w -> w.getValueOut().bytes(new byte[0])); // System.out.println(chronicle.dump()); Assert.assertEquals(nextIndexToWrite, appender.lastIndexAppended()); } }
void logYaml() { if (YamlLogging.showServerWrites()) try { assert outWire.startUse(); LOG.info("\nServer Sends:\n" + Wires.fromSizePrefixedBlobs((Wire) outWire)); } catch (Exception e) { Jvm.warn().on(getClass(), "\nServer Sends ( corrupted ) :\n" + outWire.bytes().toDebugString()); } finally { assert outWire.endUse(); } }
@Override public void onSubscribe(@NotNull WireOut wireOut) { if (LOG.isDebugEnabled()) Jvm.debug().on(getClass(), "onSubscribe - localIdentifier=" + localIdentifier + "," + "remoteIdentifier=" + remoteIdentifier); wireOut.writeEventName(identifier) .marshallable(WriteMarshallable.EMPTY) .writeComment(toString() + ", tcpChannelHub={" + hub.toString() + "}"); }
@Override public void writeMarshallable(@NotNull WireOut wire) { ValueOut valueOut = wire.getValueOut(); for (int i = 0; i < NUMBER_OF_LONGS; i++) valueOut.int64(i); Jvm.pause(writePauseMs); } }
checkWires(in.bytes(), out.bytes(), wireType()); };
outWire.writeDocument(true, d -> { @NotNull final String fullName = requestContext.fullName(); outWire.write(CoreFields.csp).text(fullName + "?view=Replication") .write(CoreFields.cid).int64(cid); }); outWire.writeDocument(false, d -> outWire.write(bootstrap) .int64(replication.lastModificationTime(remoteIdentifier)) .writeComment("localIdentifier=" + hostId.hostId() + ",remoteIdentifier=" + remoteIdentifier)); outWire.writeDocument(true, wire -> outWire.writeEventName(CoreFields.tid).int64(tid)); writeData(inWire, out -> outWire.write(identifierReply).int8(hostId.hostId())); outBootstrap.identifier(hostId.hostId()); outBootstrap.lastUpdatedTime(replication.lastModificationTime(id)); outWire.writeEventName(bootstrap).typedMarshallable(outBootstrap);