Search in sources :

Example 26 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class PeekDocumentTest method testReadWrite10Backwards.

@Test
public void testReadWrite10Backwards() {
    File tempDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext documentContext = appender.writingDocument()) {
            documentContext.wire().write("value").text("hello");
        }
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.peekDocument());
        try (DocumentContext documentContext = tailer.readingDocument()) {
            assertTrue(documentContext.isPresent());
            assertTrue(tailer.peekDocument());
            Wire wire = documentContext.wire();
            String result = wire.read("value").text();
            assertEquals("hello", result);
        // System.out.println(result);
        }
        assertFalse(tailer.peekDocument());
        tailer.direction(TailerDirection.BACKWARD);
        assertTrue(tailer.peekDocument());
        try (DocumentContext documentContext = tailer.readingDocument()) {
        }
        assertFalse(tailer.peekDocument());
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) Test(org.junit.Test)

Example 27 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class TestBinarySearch method toWire.

@NotNull
private Wire toWire(int key) {
    final MyData myData = new MyData();
    myData.key = key;
    myData.value = Integer.toString(key);
    Wire wire = WireType.BINARY.apply(Bytes.elasticByteBuffer());
    wire.usePadding(true);
    try (final DocumentContext dc = wire.writingDocument()) {
        dc.wire().getValueOut().typedMarshallable(myData);
    }
    return wire;
}
Also used : Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class MicroToucher method execute.

public boolean execute() {
    final Wire bufferWire = appender.wire();
    if (bufferWire == null)
        return false;
    final long lastPosition = appender.lastPosition;
    final long lastPage = lastPosition & ~0xFFF;
    final long nextPage = (lastPosition + 0xFFF) & ~0xFFF;
    Bytes bytes = bufferWire.bytes();
    if (nextPage != lastPageTouched) {
        lastPageTouched = nextPage;
        try {
            // best effort
            final BytesStore bs = bytes.bytesStore();
            if (bs.inside(nextPage, 8))
                touchPage(nextPage, bs);
        } catch (Throwable ignored) {
        }
        return true;
    }
    lastPageToSync = lastPage;
    return false;
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) Wire(net.openhft.chronicle.wire.Wire) BytesStore(net.openhft.chronicle.bytes.BytesStore)

Example 29 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class MicroToucher method bgExecute.

public void bgExecute() {
    final long lastPage = this.lastPageToSync;
    final long start = this.lastPageSynced;
    final long length = Math.min(8 << 20, lastPage - start);
    // System.out.println("len "+length);
    if (length < 8 << 20)
        return;
    final Wire bufferWire = appender.wire();
    if (bufferWire == null)
        return;
    BytesStore bytes = bufferWire.bytes().bytesStore();
    sync(bytes, start, length);
    this.lastPageSynced += length;
}
Also used : Wire(net.openhft.chronicle.wire.Wire) BytesStore(net.openhft.chronicle.bytes.BytesStore)

Example 30 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class SingleTableBuilder method build.

// *************************************************************************
// 
// *************************************************************************
@NotNull
public TableStore<T> build() {
    if (readOnly) {
        if (!file.exists())
            throw new IORuntimeException("Metadata file not found in readOnly mode");
        // Wait a short time for the file to be initialized
        TimingPauser pauser = Pauser.balanced();
        try {
            while (file.length() < OS.mapAlignment()) {
                pauser.pause(1, TimeUnit.SECONDS);
            }
        } catch (TimeoutException e) {
            throw new IORuntimeException("Metadata file found in readOnly mode, but not initialized yet");
        }
    }
    MappedBytes bytes = null;
    try {
        if (!readOnly && file.createNewFile() && !file.canWrite()) {
            throw new IllegalStateException("Cannot write to tablestore file " + file);
        }
        bytes = MappedBytes.mappedBytes(file, OS.SAFE_PAGE_SIZE, OS.SAFE_PAGE_SIZE, readOnly);
        // these MappedBytes are shared, but the assumption is they shouldn't grow. Supports 2K entries.
        bytes.disableThreadSafetyCheck(true);
        // eagerly initialize backing MappedFile page - otherwise wire.writeFirstHeader() will try to lock the file
        // to allocate the first byte store and that will cause lock overlap
        bytes.readVolatileInt(0);
        Wire wire = wireType.apply(bytes);
        if (readOnly)
            return SingleTableStore.doWithSharedLock(file, v -> {
                try {
                    return readTableStore(wire);
                } catch (IOException ex) {
                    throw Jvm.rethrow(ex);
                }
            }, () -> null);
        else {
            MappedBytes finalBytes = bytes;
            return SingleTableStore.doWithExclusiveLock(file, v -> {
                try {
                    if (wire.writeFirstHeader()) {
                        return writeTableStore(finalBytes, wire);
                    } else {
                        return readTableStore(wire);
                    }
                } catch (IOException ex) {
                    throw Jvm.rethrow(ex);
                }
            }, () -> null);
        }
    } catch (IOException e) {
        throw new IORuntimeException("file=" + file.getAbsolutePath(), e);
    } finally {
        if (bytes != null)
            bytes.clearUsedByThread();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) StreamCorruptedException(java.io.StreamCorruptedException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Builder(net.openhft.chronicle.core.util.Builder) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) Jvm(net.openhft.chronicle.core.Jvm) File(java.io.File) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ValueIn(net.openhft.chronicle.wire.ValueIn) StringUtils(net.openhft.chronicle.core.util.StringUtils) CLASS_ALIASES(net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) MetaDataKeys(net.openhft.chronicle.queue.impl.single.MetaDataKeys) Path(java.nio.file.Path) Pauser(net.openhft.chronicle.threads.Pauser) Wires(net.openhft.chronicle.wire.Wires) TableStore(net.openhft.chronicle.queue.impl.TableStore) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Wire (net.openhft.chronicle.wire.Wire)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 Test (org.junit.Test)11 File (java.io.File)9 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)8 NotNull (org.jetbrains.annotations.NotNull)8 Bytes (net.openhft.chronicle.bytes.Bytes)7 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)7 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)5 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 BytesStore (net.openhft.chronicle.bytes.BytesStore)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Jvm (net.openhft.chronicle.core.Jvm)2 OS (net.openhft.chronicle.core.OS)2