Search in sources :

Example 1 with ClosedIllegalStateException

use of net.openhft.chronicle.core.io.ClosedIllegalStateException in project Chronicle-Queue by OpenHFT.

the class SingleTableStore method acquireValueFor.

/**
 * {@inheritDoc}
 */
@Override
public synchronized LongValue acquireValueFor(CharSequence key, final long defaultValue) {
    if (mappedBytes.isClosed())
        throw new ClosedIllegalStateException("Closed");
    final StringBuilder sb = Wires.acquireStringBuilder();
    mappedBytes.reserve(this);
    try {
        mappedBytes.readPosition(0);
        mappedBytes.readLimit(mappedBytes.realCapacity());
        while (mappedWire.readDataHeader()) {
            final int header = mappedBytes.readVolatileInt();
            if (Wires.isNotComplete(header))
                break;
            final long readPosition = mappedBytes.readPosition();
            final int length = Wires.lengthOf(header);
            final ValueIn valueIn = mappedWire.readEventName(sb);
            if (StringUtils.equalsCaseIgnore(key, sb)) {
                return valueIn.int64ForBinding(null);
            }
            mappedBytes.readPosition(readPosition + length);
        }
        mappedBytes.writeLimit(mappedBytes.realCapacity());
        long start = mappedBytes.readPosition();
        mappedBytes.writePosition(start);
        final long pos = mappedWire.enterHeader(128L);
        final LongValue longValue = wireType.newLongReference().get();
        mappedWire.writeEventName(key).int64forBinding(defaultValue, longValue);
        mappedWire.writeAlignTo(Integer.BYTES, 0);
        mappedWire.updateHeader(pos, false, 0);
        long end = mappedBytes.writePosition();
        long chuckSize = mappedFile.chunkSize();
        long overlapSize = mappedFile.overlapSize();
        long endOfChunk = (start + chuckSize - 1) / chuckSize * chuckSize;
        if (end >= endOfChunk + overlapSize)
            throw new IllegalStateException("Misaligned write");
        return longValue;
    } catch (StreamCorruptedException | EOFException e) {
        throw new IORuntimeException(e);
    } finally {
        mappedBytes.release(this);
    }
}
Also used : ClosedIllegalStateException(net.openhft.chronicle.core.io.ClosedIllegalStateException) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) ClosedIllegalStateException(net.openhft.chronicle.core.io.ClosedIllegalStateException) LongValue(net.openhft.chronicle.core.values.LongValue) EOFException(java.io.EOFException) StreamCorruptedException(java.io.StreamCorruptedException)

Example 2 with ClosedIllegalStateException

use of net.openhft.chronicle.core.io.ClosedIllegalStateException in project Chronicle-Queue by OpenHFT.

the class MicroToucherTest method touchPage.

public void touchPage(Consumer<SingleChronicleQueueBuilder> configure, int pagesExpected) {
    long start = System.nanoTime();
    String path = OS.getTarget() + "/touchPage-" + System.nanoTime();
    int pages = 0;
    final SingleChronicleQueueBuilder builder = ChronicleQueue.singleBuilder(path);
    configure.accept(builder);
    try (ChronicleQueue q = builder.build();
        final StoreAppender appender = (StoreAppender) q.acquireAppender()) {
        Thread msync = new Thread(() -> {
            try {
                while (true) {
                    appender.bgMicroTouch();
                    Jvm.pause(25);
                }
            } catch (ClosedIllegalStateException expected) {
            }
        });
        msync.setDaemon(true);
        msync.start();
        long lastPage = 0;
        for (int i = 0; i < (1 << 20); i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().bytes().writeSkip(256);
            }
            long page = (appender.lastPosition + 0xFFF) & ~0xFFF;
            boolean touch = page != lastPage && appender.wire().bytes().bytesStore().inside(page, 8);
            lastPage = page;
            if (touch != appender.microTouch())
                assertEquals("i: " + i, touch, appender.microTouch());
            if (touch)
                pages++;
        }
    }
    System.out.println("pages = " + pages);
    // assertEquals(pagesExpected, pages);
    System.out.println("Time = " + (System.nanoTime() - start) / 1000000 / 1e3);
    IOTools.deleteDirWithFiles(path);
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ClosedIllegalStateException(net.openhft.chronicle.core.io.ClosedIllegalStateException) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Aggregations

ClosedIllegalStateException (net.openhft.chronicle.core.io.ClosedIllegalStateException)2 EOFException (java.io.EOFException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 LongValue (net.openhft.chronicle.core.values.LongValue)1 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)1 DocumentContext (net.openhft.chronicle.wire.DocumentContext)1