Search in sources :

Example 1 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueue method dump.

@Override
public void dump(@NotNull Writer writer, long fromIndex, long toIndex) {
    try {
        long firstIndex = firstIndex();
        writer.append("# firstIndex: ").append(Long.toHexString(firstIndex)).append("\n");
        ExcerptTailer tailer = createTailer();
        if (!tailer.moveToIndex(fromIndex)) {
            if (firstIndex > fromIndex) {
                tailer.toStart();
            } else {
                return;
            }
        }
        Bytes bytes = Wires.acquireBytes();
        TextWire text = new TextWire(bytes);
        while (true) {
            try (DocumentContext dc = tailer.readingDocument()) {
                if (!dc.isPresent()) {
                    writer.append("# no more messages at ").append(Long.toHexString(dc.index())).append("\n");
                    return;
                }
                if (dc.index() > toIndex)
                    return;
                writer.append("# index: ").append(Long.toHexString(dc.index())).append("\n");
                Wire wire = dc.wire();
                long start = wire.bytes().readPosition();
                try {
                    text.clear();
                    wire.copyTo(text);
                    writer.append(bytes.toString());
                } catch (Exception e) {
                    wire.bytes().readPosition(start);
                    writer.append(wire.bytes()).append("\n");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace(new PrintWriter(writer));
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
            LoggerFactory.getLogger(SingleChronicleQueue.class).debug("", e);
        }
    }
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException)

Example 2 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class SingleCQFormatTest method expected.

private static void expected(@NotNull ExcerptTailer tailer, String expected) {
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue("No document found", dc.isPresent());
        Bytes bytes2 = Bytes.elasticHeapByteBuffer(128);
        dc.wire().copyTo(new TextWire(bytes2));
        assertEquals(expected, bytes2.toString());
    }
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes)

Example 3 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class DirectChronicleQueueStringTest method writeSome.

private void writeSome(DirectChronicleQueue chronicle) {
    NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
    final Bytes toWrite = allocate.bytes();
    for (int i = 0; i < RUNS; i++) {
        toWrite.clear();
        toWrite.write(EXPECTED_BYTES);
        toWrite.flip();
        chronicle.appendDocument(toWrite);
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore)

Example 4 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Bytes by OpenHFT.

the class OptimisedBytesStoreHashTest method testReadIncompleteLong.

@Test
public void testReadIncompleteLong() {
    Bytes bs = Bytes.allocateDirect(8);
    for (int i = 1; i <= 8; i++) bs.writeUnsignedByte(i);
    @NotNull Bytes bs2 = Bytes.allocateDirect(9).unchecked(true);
    for (int i = 0; i <= 8; i++) {
        assertEquals("i: " + i, Long.toHexString(bs2.readLong(0)), Long.toHexString(OptimisedBytesStoreHash.readIncompleteLong(bs.addressForRead(0), i)));
        bs2.writeUnsignedByte(i + 1);
    }
    bs.release();
    bs2.release();
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytes(net.openhft.chronicle.bytes.NativeBytes) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.Test)

Example 5 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Bytes by OpenHFT.

the class BinaryLongArrayReferenceTest method getSetValues.

@Test
public void getSetValues() {
    int length = 128 * 8 + 2 * 8;
    Bytes bytes = Bytes.allocateDirect(length);
    BinaryLongArrayReference.write(bytes, 128);
    @NotNull BinaryLongArrayReference array = new BinaryLongArrayReference();
    array.bytesStore(bytes, 0, length);
    assertEquals(128, array.getCapacity());
    for (int i = 0; i < 128; i++) array.setValueAt(i, i + 1);
    for (int i = 0; i < 128; i++) assertEquals(i + 1, array.getValueAt(i));
    bytes.release();
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.Test)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)53 Test (org.junit.Test)23 NotNull (org.jetbrains.annotations.NotNull)16 File (java.io.File)13 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)8 Wire (net.openhft.chronicle.wire.Wire)7 ByteBuffer (java.nio.ByteBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)6 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)6 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)5 NativeBytes (net.openhft.chronicle.bytes.NativeBytes)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 ParseException (java.text.ParseException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AffinityLock (net.openhft.affinity.AffinityLock)3