Search in sources :

Example 16 with Bytes

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

the class StoreAppender method prepareAndReturnWriteContext.

private StoreAppender.StoreAppenderContext prepareAndReturnWriteContext(boolean metaData) {
    if (count > 1) {
        assert metaData == writeContext.metaData;
        return writeContext;
    }
    if (queue.doubleBuffer && writeLock.locked() && !metaData) {
        writeContext.isClosed = false;
        writeContext.rollbackOnClose = false;
        writeContext.buffered = true;
        if (bufferWire == null) {
            Bytes bufferBytes = Bytes.allocateElasticOnHeap();
            bufferWire = queue().wireType().apply(bufferBytes);
        }
        writeContext.wire = bufferWire;
        writeContext.metaData(false);
    } else {
        writeLock.lock();
        int cycle = queue.cycle();
        if (wire == null)
            setWireIfNull(cycle);
        if (this.cycle != cycle)
            rollCycleTo(cycle);
        long safeLength = queue.overlapSize();
        resetPosition();
        assert !QueueSystemProperties.CHECK_INDEX || checkWritePositionHeaderNumber();
        // sets the writeLimit based on the safeLength
        openContext(metaData, safeLength);
        // Move readPosition to the start of the context. i.e. readRemaining() == 0
        wire.bytes().readPosition(wire.bytes().writePosition());
    }
    return writeContext;
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes)

Example 17 with Bytes

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

the class CreateAtIndexTest method testWriteBytesWithIndex.

@Test
public void testWriteBytesWithIndex() {
    final Bytes HELLO_WORLD = Bytes.from("hello world");
    File tmp = getTmpDir();
    try (ChronicleQueue queue = single(tmp).testBlockSize().rollCycle(TEST_DAILY).build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        appender.writeBytes(0x421d00000001L, HELLO_WORLD);
    }
    try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        String before = queue.dump();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        String after = queue.dump();
        assertEquals(before, after);
    }
    // try too far
    try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        try {
            appender.writeBytes(0x421d00000003L, HELLO_WORLD);
            fail();
        } catch (IllegalStateException e) {
            assertTrue(e.getMessage().startsWith("Unable to move to index 421d00000003 beyond the end of the queue"));
        }
    }
    try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000002L, HELLO_WORLD);
        appender.writeBytes(0x421d00000003L, HELLO_WORLD);
    }
    try {
        IOTools.deleteDirWithFiles(tmp, 2);
    } catch (IORuntimeException ignored) {
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) File(java.io.File) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 18 with Bytes

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

the class InternalAppenderWriteBytesTest method test3.

@Test
public void test3() {
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    Bytes result = Bytes.elasticHeapByteBuffer();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
        ExcerptAppender appender = q.acquireAppender();
        appender.writeBytes(test);
        ExcerptTailer tailer = q.createTailer();
        expectException("Trying to overwrite index 0 which is before the end of the queue");
        ((InternalAppender) appender).writeBytes(0, test);
        try (DocumentContext documentContext = tailer.readingDocument()) {
            result.write(documentContext.wire().bytes());
        }
        Assert.assertTrue("hello world".contentEquals(result));
        assertEquals(1, tailer.index());
        result.clear();
        ((InternalAppender) appender).writeBytes(1, test);
        try (DocumentContext dc = tailer.readingDocument()) {
            dc.rollbackOnClose();
        }
        assertEquals(1, tailer.index());
        ((InternalAppender) appender).writeBytes(2, test);
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) NotNull(org.jetbrains.annotations.NotNull) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 19 with Bytes

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

the class InternalAppenderWriteBytesTest method dontOverwriteExisting.

@Test
public void dontOverwriteExisting() {
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    Bytes result = Bytes.elasticHeapByteBuffer();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
        ExcerptAppender appender = q.acquireAppender();
        appender.writeBytes(test);
        expectException("Trying to overwrite index 0 which is before the end of the queue");
        // try to overwrite - will not overwrite
        ((InternalAppender) appender).writeBytes(0, Bytes.from("HELLO WORLD"));
        ExcerptTailer tailer = q.createTailer();
        tailer.readBytes(result);
        assertEquals(test, result);
        assertEquals(1, tailer.index());
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 20 with Bytes

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

the class InternalAppenderWriteBytesTest method writeJustAfterLastIndex.

@Test
public void writeJustAfterLastIndex() {
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    @NotNull Bytes<byte[]> test2 = Bytes.from("hello world again");
    Bytes result = Bytes.elasticHeapByteBuffer();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
        ExcerptAppender appender = q.acquireAppender();
        // write at index 0
        appender.writeBytes(test);
        // append at index 1
        ((InternalAppender) appender).writeBytes(1, test2);
        ExcerptTailer tailer = q.createTailer();
        tailer.readBytes(result);
        assertEquals(test, result);
        result.clear();
        tailer.readBytes(result);
        assertEquals(test2, result);
        result.clear();
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) 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