net.openhft.chronicle.wire.WireType Java Examples

The following examples show how to use net.openhft.chronicle.wire.WireType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
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(test);
    try {
        final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
        final Bytes<?> bytes = wire.bytes();
        bytes.readLimitToCapacity();
        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);
    }
}
 
Example #2
Source File: ClusterTest.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeepCopy() {
    MyClusterContext cc = (MyClusterContext) new MyClusterContext().value(22).wireType(WireType.TEXT);
    String s = Marshallable.$toString(cc);
    MyClusterContext o = Marshallable.fromString(s);
    Assert.assertEquals(cc.value, o.value);
    MyClusterContext cc2 = cc.deepCopy();
    Assert.assertEquals(cc.value, cc2.value);

    try (Cluster<HostDetails, ?, MyClusterContext<?>> c = new MyCluster("mine")) {
        c.clusterContext(cc);
        try (Cluster<HostDetails, ?, MyClusterContext<?>> c2 = c.deepCopy()) {
            MyClusterContext mcc = c2.clusterContext();
            Assert.assertNotNull(mcc);
            Assert.assertEquals(22, mcc.value);
        }
    }
}
 
Example #3
Source File: Queue28Test.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
//                {WireType.TEXT},
                {WireType.BINARY}
        });
    }
 
Example #4
Source File: ChronicleReaderMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
protected void configureReader(final ChronicleReader chronicleReader, final CommandLine commandLine) {
    final Consumer<String> messageSink = commandLine.hasOption('l') ?
            s -> System.out.println(s.replaceAll("\n", "")) :
            System.out::println;
    chronicleReader.
            withMessageSink(messageSink).
            withBasePath(Paths.get(commandLine.getOptionValue('d')));

    if (commandLine.hasOption('i')) {
        stream(commandLine.getOptionValues('i')).forEach(chronicleReader::withInclusionRegex);
    }
    if (commandLine.hasOption('e')) {
        stream(commandLine.getOptionValues('e')).forEach(chronicleReader::withExclusionRegex);
    }
    if (commandLine.hasOption('f')) {
        chronicleReader.tail();
    }
    if (commandLine.hasOption('m')) {
        chronicleReader.historyRecords(Long.parseLong(commandLine.getOptionValue('m')));
    }
    if (commandLine.hasOption('n')) {
        chronicleReader.withStartIndex(Long.decode(commandLine.getOptionValue('n')));
    }
    if (commandLine.hasOption('r')) {
        chronicleReader.asMethodReader(commandLine.getOptionValue('r'));
    }
    if (commandLine.hasOption('w')) {
        chronicleReader.withWireType(WireType.valueOf(commandLine.getOptionValue('w')));
    }
    if (commandLine.hasOption('s')) {
        chronicleReader.suppressDisplayIndex();
    }
}
 
Example #5
Source File: BatchAppenderNativeTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testNative() {
    if (!OS.isMacOSX())
        return;

    Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
    try {

        BatchAppenderNative batchAppenderNative = new BatchAppenderNative();

        // this will append a message in wire of hello world
        long result = batchAppenderNative.writeMessages(bytes.addressForWrite(0), bytes.realCapacity(), 1);

        int len = (int) result;
        int count = (int) (result >> 32);
        bytes.readLimit(len);

        Assert.assertEquals(16, len);
        Assert.assertEquals(1, count);

        Wire w = WireType.BINARY.apply(bytes);

        for (int i = 0; i < count; i++) {
            try (DocumentContext dc = w.readingDocument()) {
                Assert.assertEquals("hello world", dc.wire().getValueIn().text());
            }
        }
    } finally {
        bytes.releaseLast();
    }
}
 
Example #6
Source File: SingleTableBuilder.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@NotNull
public static <T extends Metadata> SingleTableBuilder<T> builder(@NotNull File file, @NotNull WireType wireType, @NotNull T metadata) {
    if (file.isDirectory()) {
        throw new IllegalArgumentException("Tables should be configured with the table file, not a directory. Actual file used: " + file.getParentFile());
    }
    if (!file.getName().endsWith(SingleTableStore.SUFFIX)) {
        throw new IllegalArgumentException("Invalid file type: " + file.getName());
    }

    return new SingleTableBuilder<>(file, metadata).wireType(wireType);
}
 
Example #7
Source File: PretoucherTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
static SingleChronicleQueue createQueue(final File path, final TimeProvider timeProvider) {
    return SingleChronicleQueueBuilder.
            binary(path).
            timeProvider(timeProvider).
            rollCycle(RollCycles.TEST_SECONDLY).
            testBlockSize().
            wireType(WireType.BINARY).
            build();
}
 
Example #8
Source File: MoveToWrongIndexThenToEndTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private SingleChronicleQueue createChronicle(Path queuePath) {
    SingleChronicleQueueBuilder builder = SingleChronicleQueueBuilder.builder();
    builder.path(queuePath);
    builder.wireType(WireType.FIELDLESS_BINARY);
    builder.rollCycle(rollCycle);
    return builder.build();
}
 
Example #9
Source File: SimpleServerAndClientTest.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void createServer(@NotNull String desc, @NotNull EventLoop eg) throws IOException {
    @NotNull AcceptorEventHandler eah = new AcceptorEventHandler(desc,
            LegacyHanderFactory.simpleTcpEventHandlerFactory(WireEchoRequestHandler::new, WireType.TEXT),
            VanillaNetworkContext::new);

    eg.addHandler(eah);
    ServerSocketChannel sc = TCPRegistry.acquireServerSocketChannel(desc);
    sc.configureBlocking(false);
}
 
Example #10
Source File: SslTestClusterContext.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@NotNull
TcpEventHandler<T> createHandler(final T networkContext) {
    @NotNull final T nc = networkContext;
    if (nc.isAcceptor())
        nc.wireOutPublisher(new VanillaWireOutPublisher(WireType.TEXT));
    @NotNull final TcpEventHandler<T> handler = new TcpEventHandler<>(networkContext);

    @NotNull final Function<Object, TcpHandler<T>> consumer = o -> {

        if (o instanceof TcpHandler) {
            return wrapForSsl((TcpHandler) o);
        }

        throw new UnsupportedOperationException("not supported class=" + o.getClass());
    };

    final NetworkStatsListener<T> nl = nc.networkStatsListener();
    notifyHostPort(nc.socketChannel(), nl);

    @Nullable final Function<T, TcpHandler<T>> f
            = x -> new HeaderTcpHandler<>(handler, consumer);

    @NotNull final WireTypeSniffingTcpHandler<T> sniffer = new
            WireTypeSniffingTcpHandler<>(handler, f);

    handler.tcpHandler(sniffer);
    return handler;
}
 
Example #11
Source File: SslTestClusterContext.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
public void defaults() {
    this.handlerFactory(new UberHandler.Factory<>())
            .heartbeatFactory(new HeartbeatHandler.Factory<>())
            .wireOutPublisherFactory(VanillaWireOutPublisher::new)
            .wireType(WireType.TEXT)
            .connectionEventHandler(StubConnectionManager::new)
            .serverThreadingStrategy(ServerThreadingStrategy.CONCURRENT)
            .networkContextFactory(c -> ncFactory())
            .networkStatsListenerFactory(ctx -> new LoggingNetworkStatsListener<>())
            .eventLoop(new EventGroup(false, Pauser.balanced(), false, "ssl-cluster-"));
}
 
Example #12
Source File: TestMethodWriterWithThreads.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("TODO FIX")
public void test() throws FileNotFoundException {

    File tmpDir = getTmpDir();
    try (final ChronicleQueue q = builder(tmpDir, WireType.BINARY).rollCycle(HOURLY).doubleBuffer(doubleBuffer).build()) {

        methodWriter = q.methodWriter(I.class);

        ExcerptTailer tailer = q.createTailer();
        MethodReader methodReader = tailer.methodReader(newReader());

        IntStream.range(0, 1000)
                .parallel()
                .forEach(i -> {
                    creates();
                    amends();
                    synchronized (methodReader) {
                        for (int j = 0; j < 2 && !fail.get(); )
                            if (methodReader.readOne())
                                j++;
                    }
                    if (fail.get())
                        fail();
                });

    } finally {
        if (fail.get()) {
            DumpQueueMain.dump(tmpDir.getAbsolutePath());
        }
    }
}
 
Example #13
Source File: TailerIndexingQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static ChronicleQueue createQueue(final File path, final TimeProvider timeProvider) {
    return SingleChronicleQueueBuilder.
            binary(path).
            timeProvider(timeProvider).
            rollCycle(RollCycles.TEST_SECONDLY).
            testBlockSize().
            wireType(WireType.BINARY).
            build();
}
 
Example #14
Source File: SessionDetailsProvider.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
default void readMarshallable(@NotNull WireIn wire) throws IORuntimeException {
    userId(wire.read(EventId.userId).text());
    domain(wire.read(EventId.domain).text());
    sessionMode(wire.read(EventId.sessionMode).object(SessionMode.class));
    securityToken(wire.read(EventId.securityToken).text());
    @Nullable final String uid = wire.read(EventId.clientId).text();
    if (uid != null)
        clientId(UUID.fromString(uid));
    wireType(wire.read(EventId.wireType).object(WireType.class));
    hostId(wire.read(EventId.hostId).int8());
}
 
Example #15
Source File: IndexTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
//                {WireType.TEXT}, // TODO Add CAS to LongArrayReference.
                {WireType.BINARY}
        });
    }
 
Example #16
Source File: SingleChronicleQueueCloseTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testTailAfterClose() {
    final ChronicleQueue queue =
            SingleChronicleQueueBuilder.builder(getTmpDir(), WireType.BINARY).
                    build();
    final ExcerptAppender appender = queue.acquireAppender();
    appender.writeDocument(w -> w.write(TestKey.test).int32(1));
    queue.close();
    try {
        appender.writeDocument(w -> w.write(TestKey.test).int32(2));
        Assert.fail();
    } catch (IllegalStateException e) {
        // ok
    }
}
 
Example #17
Source File: MethodTcpHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
protected Wire initialiseOutWire(Bytes<?> out, @NotNull WireType wireType) {
    Wire wire = super.initialiseOutWire(out, wireType);
    output = wire.methodWriter(outClass);
    return wire;

}
 
Example #18
Source File: TestBinarySearch.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@NotNull
private Wire toWire(int key) {
    final MyData myData = new MyData();
    myData.key = key;
    myData.value = Integer.toString(key);
    Wire result = WireType.BINARY.apply(Bytes.elasticByteBuffer());

    try (final DocumentContext dc = result.writingDocument()) {
        dc.wire().getValueOut().typedMarshallable(myData);
    }

    return result;
}
 
Example #19
Source File: AppenderFileHandleLeakTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private ChronicleQueue createQueue(final TimeProvider timeProvider) {
    return SingleChronicleQueueBuilder.
            binary(queuePath).
            rollCycle(RollCycles.TEST_SECONDLY).
            wireType(WireType.BINARY_LIGHT).
            storeFileListener(storeFileListener).
            timeProvider(timeProvider).
            build();
}
 
Example #20
Source File: DiskSerializationProcessor.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T loadData(long snapshotId,
                      SerializedModuleType type,
                      int instanceId,
                      Function<BytesIn, T> initFunc) {

    final Path path = resolveSnapshotPath(snapshotId, type, instanceId);

    log.debug("Loading state from {}", path);
    try (final InputStream is = Files.newInputStream(path, StandardOpenOption.READ);
         final InputStream bis = new BufferedInputStream(is);
         final LZ4FrameInputStream lz4is = new LZ4FrameInputStream(bis)) {

        // TODO improve reading algorithm
        final InputStreamToWire inputStreamToWire = new InputStreamToWire(WireType.RAW, lz4is);
        final Wire wire = inputStreamToWire.readOne();

        log.debug("start de-serializing...");

        AtomicReference<T> ref = new AtomicReference<>();
        wire.readBytes(bytes -> ref.set(initFunc.apply(bytes)));

        return ref.get();

    } catch (final IOException ex) {
        log.error("Can not read snapshot file: ", ex);
        throw new IllegalStateException(ex);
    }
}
 
Example #21
Source File: StoreTailerTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private SingleChronicleQueueBuilder minutely(@NotNull File file, TimeProvider timeProvider) {
    return SingleChronicleQueueBuilder.builder(file, WireType.BINARY).rollCycle(RollCycles.MINUTELY).testBlockSize().timeProvider(timeProvider);
}
 
Example #22
Source File: RollingChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
protected SingleChronicleQueueBuilder builder(@NotNull File file, @NotNull WireType wireType) {
    return SingleChronicleQueueBuilder.builder(file, wireType).rollCycle(RollCycles.TEST4_DAILY).testBlockSize();
}
 
Example #23
Source File: TestMethodWriterWithThreads.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
protected SingleChronicleQueueBuilder builder(@NotNull File file, @NotNull WireType wireType) {
    return SingleChronicleQueueBuilder.builder(file, wireType).rollCycle(TEST4_DAILY).testBlockSize();
}
 
Example #24
Source File: Queue28Test.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
public Queue28Test(WireType wireType) {
    this.wireType = wireType;
}
 
Example #25
Source File: IndexTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
/**
 * @param wireType the type of the wire
 */
public IndexTest(@NotNull WireType wireType) {
    this.wireType = wireType;
}
 
Example #26
Source File: FileUtilTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
protected SingleChronicleQueueBuilder builder(@NotNull File file, @NotNull WireType wireType) {
    return SingleChronicleQueueBuilder.builder(file, wireType).rollCycle(RollCycles.TEST4_DAILY).testBlockSize();
}
 
Example #27
Source File: CleanupUnusedQueueFilesTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
protected SingleChronicleQueueBuilder builder(@NotNull File file, @NotNull WireType wireType) {
    return SingleChronicleQueueBuilder.builder(file, wireType).rollCycle(RollCycles.TEST4_DAILY).testBlockSize();
}
 
Example #28
Source File: VanillaSessionDetails.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public WireType wireType() {
    return wireType;
}
 
Example #29
Source File: DiskSerializationProcessor.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public WireToOutputStream2(WireType wireType, OutputStream os) {
    wire = wireType.apply(bytes);
    dos = new DataOutputStream(os);
}
 
Example #30
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public static Wire longsToWire(long[] dataArray) {

        final int sizeInBytes = dataArray.length * 8;
        final ByteBuffer byteBuffer = ByteBuffer.allocate(sizeInBytes);
        byteBuffer.asLongBuffer().put(dataArray);

        final byte[] bytesArray = new byte[sizeInBytes];
        byteBuffer.get(bytesArray);

        //log.debug(" section {} -> {}", section, bytes);

        final Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(sizeInBytes);
        bytes.ensureCapacity(sizeInBytes);

        bytes.write(bytesArray);

        return WireType.RAW.apply(bytes);
    }