Java Code Examples for net.openhft.chronicle.bytes.Bytes#elasticByteBuffer()

The following examples show how to use net.openhft.chronicle.bytes.Bytes#elasticByteBuffer() . 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: SingleCQFormat2Test.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
public void appendMessage(@NotNull ChronicleQueue queue, long expectedIndex, String msg) {
    @NotNull ExcerptAppender appender = queue.acquireAppender();
    switch (appendMode) {
        case 1:
            appender.writeDocument(w -> w.write(() -> "msg").text(msg));
            break;

        case 2:
            Bytes bytes = Bytes.elasticByteBuffer();
            new BinaryWire(bytes).write(() -> "msg").text(msg);
            appender.writeBytes(bytes);
            bytes.releaseLast();

            break;

        default:
            try (DocumentContext dc = appender.writingDocument()) {
                Wire wire = dc.wire();
                wire.write(() -> "msg").text(msg);
            }
            break;
    }

    long index = appender.lastIndexAppended();
    assertHexEquals(expectedIndex, index);
}
 
Example 2
Source File: AppenderFileHandleLeakTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private static void readMessage(final ChronicleQueue queue,
                                final boolean manuallyReleaseResources,
                                final Consumer<ExcerptTailer> refHolder) {
    final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
    try (final ExcerptTailer tailer = queue.createTailer()) {
        while (bytes.isEmpty()) {
            tailer.toStart().readBytes(bytes);
        }
        refHolder.accept(tailer);
        assertTrue(Math.signum(bytes.readInt()) >= 0);

        if (manuallyReleaseResources) {
            try {
                ((StoreTailer) tailer).releaseResources();
            } catch (RuntimeException e) {
                // ignore
            }
        }
    } finally {
        bytes.releaseLast();
    }
}
 
Example 3
Source File: TcpEventHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
public TcpEventHandler(@NotNull final T nc, final TcpHandlerBias bias) {
    this.sc = ISocketChannel.wrapUnsafe(nc.socketChannel().socketChannel());
    this.scToString = sc.toString();
    this.nc = nc;
    this.bias = bias.get();
    try {
        sc.configureBlocking(false);
        Socket sock = sc.socket();
        // TODO: should have a strategy for this like ConnectionNotifier
        if (!DISABLE_TCP_NODELAY)
            sock.setTcpNoDelay(true);

        if (TCP_BUFFER >= 64 << 10) {
            sock.setReceiveBufferSize(TCP_BUFFER);
            sock.setSendBufferSize(TCP_BUFFER);

            checkBufSize(sock.getReceiveBufferSize(), "recv");
            checkBufSize(sock.getSendBufferSize(), "send");
        }
    } catch (IOException e) {
        if (isClosed() || !sc.isOpen())
            throw new IORuntimeException(e);
        Jvm.warn().on(getClass(), e);
    }

    //We have to provide back pressure to restrict the buffer growing beyond,2GB because it reverts to
    // being Native bytes, we should also provide back pressure if we are not able to keep up
    inBBB = Bytes.elasticByteBuffer(TCP_BUFFER + OS.pageSize(), max(TCP_BUFFER + OS.pageSize(), DEFAULT_MAX_MESSAGE_SIZE));
    outBBB = Bytes.elasticByteBuffer(TCP_BUFFER, max(TCP_BUFFER, DEFAULT_MAX_MESSAGE_SIZE));

    // must be set after we take a slice();
    outBBB.underlyingObject().limit(0);
    readLog = new NetworkLog(this.sc, "read");
    writeLog = new NetworkLog(this.sc, "write");
    nbWarningEnabled = Jvm.warn().isEnabled(getClass());
    statusMonitorEventHandler = new StatusMonitorEventHandler(getClass());
    if (FIRST_HANDLER.compareAndSet(false, true))
        warmUp();
}
 
Example 4
Source File: JsonParserTest.java    From SAXophone with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void testPull(String json) {
    StringWriter stringWriter = new StringWriter();
    JsonParser p = JsonParser.builder().applyAdapter(new WriterAdapter(stringWriter)).build();
    Bytes jsonBytes = stringToBytes(json);
    Bytes parseBytes = Bytes.elasticByteBuffer();
    for (long i = 0; i < jsonBytes.capacity(); i++) {
        parseBytes.writeByte(jsonBytes.readByte(i));
        p.parse(parseBytes);
    }
    p.finish();
    com.google.gson.JsonParser referenceParser = new com.google.gson.JsonParser();
    JsonElement o1 = referenceParser.parse(json);
    JsonElement o2 = referenceParser.parse(stringWriter.toString());
    assertEquals(o1, o2);
}
 
Example 5
Source File: SingleTailer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public SingleTailer(@NotNull final SingleChronicleQueue chronicle) {
    this.bytes = new VanillaBytes(Bytes.elasticByteBuffer());
    this.chronicle = chronicle;
    this.wire = chronicle.createWire(bytes);
    this.value = WireUtil.newLongArrayValuesPool(chronicle.wireType());
    this.values = null;

    toStart();
}
 
Example 6
Source File: Sizer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public static int size(final BytesMarshallable message) {
    final Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
    try {
        message.writeMarshallable(buffer);
        return (int) buffer.writePosition();
    } finally {
        buffer.releaseLast();
    }
}
 
Example 7
Source File: DumpQueueMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
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, !UNALIGNED);
        while (bytes.readRemaining() >= 4) {
            sb.setLength(0);
            boolean last = dumper.dumpOne(sb, buffer);
            if (sb.indexOf("\nindex2index:") != -1 || sb.indexOf("\nindex:") != -1) {
                // truncate trailing zeros
                if (sb.indexOf(", 0\n]\n") == sb.length() - 6) {
                    int i = indexOfLastZero(sb);
                    if (i < sb.length())
                        sb.setLength(i - 5);
                    sb.append(" # truncated trailing zeros\n]");
                }
            }

            out.println(sb);

            if (last)
                break;
            if (bytes.readPosition() > upperLimit) {
                out.println("# limit reached.");
                return;
            }
        }
    } catch (IOException ioe) {
        err.println("Failed to read " + file + " " + ioe);
    } finally {
        buffer.releaseLast();
    }
}
 
Example 8
Source File: ThreadedQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testTailerReadingEmptyQueue() {
    final File path = DirectoryUtils.tempDir("testTailerReadingEmptyQueue");

    try (final ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
            .testBlockSize()
            .rollCycle(TEST_DAILY)
            .build()) {

        final ExcerptTailer tailer = rqueue.createTailer();

        try (final ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
                .testBlockSize()
                .rollCycle(TEST_DAILY)
                .build()) {

            Bytes bytes = Bytes.elasticByteBuffer();
            assertFalse(tailer.readBytes(bytes));

            final ExcerptAppender appender = wqueue.acquireAppender();
            appender.writeBytes(Bytes.wrapForRead("Hello World".getBytes(ISO_8859_1)));

            bytes.clear();
            assertTrue(tailer.readBytes(bytes));
            assertEquals("Hello World", bytes.toString());

            bytes.releaseLast();
        }
    }
}
 
Example 9
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 10
Source File: MoveToWrongIndexThenToEndTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public MoveToWrongIndexThenToEndTest() throws IOException {
    basePath = IOTools.createTempDirectory("MoveToWrongIndexThenToEndTest");
    basePath.toFile().deleteOnExit();

    queue = createChronicle(basePath);
    appender = queue.acquireAppender();
    outbound = Bytes.elasticByteBuffer();
}
 
Example 11
Source File: VanillaWireOutPublisher.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
public VanillaWireOutPublisher(@NotNull WireType wireType) {
    bytes = Bytes.elasticByteBuffer(TcpChannelHub.TCP_BUFFER);
    final WireType wireType0 = wireType == WireType.DELTA_BINARY ? WireType.BINARY : wireType;
    wire = wireType0.apply(bytes);
}
 
Example 12
Source File: RollCycleEncodeSequenceTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
public RollCycleEncodeSequenceTest(final RollCycles cycle) {
    longValue = new BinaryTwoLongReference();
    bytes = Bytes.elasticByteBuffer();
    longValue.bytesStore(bytes, 0, 16);
    rollCycleEncodeSequence = new RollCycleEncodeSequence(longValue, cycle.defaultIndexCount(), cycle.defaultIndexSpacing());
}