Search in sources :

Example 1 with LongValue

use of net.openhft.chronicle.core.values.LongValue in project Chronicle-Queue by OpenHFT.

the class TableDirectoryListingTest method lockShouldTimeOut.

@Test
public void lockShouldTimeOut() throws Exception {
    listing.onFileCreated(tempFile, 8);
    final TableStore tableCopy = SingleTableBuilder.binary(tableFile).build();
    final LongValue lock = tableCopy.acquireValueFor(TableDirectoryListing.LOCK);
    lock.setOrderedValue(System.currentTimeMillis() - (TimeUnit.SECONDS.toMillis(9) + 500));
    listing.onFileCreated(tempFile, 9);
    assertThat(listing.getMaxCreatedCycle(), is(9));
}
Also used : LongValue(net.openhft.chronicle.core.values.LongValue) TableStore(net.openhft.chronicle.queue.impl.TableStore) Test(org.junit.Test)

Example 2 with LongValue

use of net.openhft.chronicle.core.values.LongValue in project Chronicle-Queue by OpenHFT.

the class TableStoreTest method acquireValueFor.

@Test
public void acquireValueFor() {
    String file = OS.TARGET + "/table-" + System.nanoTime() + ".cq4t";
    new File(file).deleteOnExit();
    try (TableStore table = SingleTableBuilder.binary(file).build()) {
        LongValue a = table.acquireValueFor("a");
        LongValue b = table.acquireValueFor("b");
        assertEquals(Long.MIN_VALUE, a.getVolatileValue());
        assertTrue(a.compareAndSwapValue(Long.MIN_VALUE, 1));
        assertEquals(Long.MIN_VALUE, b.getVolatileValue());
        assertTrue(b.compareAndSwapValue(Long.MIN_VALUE, 2));
        assertEquals("--- !!meta-data #binary\n" + "header: !STStore {\n" + "  wireType: !WireType BINARY_LIGHT,\n" + "  recovery: !TimedStoreRecovery {\n" + "    timeStamp: 0\n" + "  }\n" + "}\n" + "# position: 112, header: 0\n" + "--- !!data #binary\n" + "a: 1\n" + "# position: 128, header: 1\n" + "--- !!data #binary\n" + "b: 2\n" + "...\n" + "# 65388 bytes remaining\n", table.dump());
    }
    try (TableStore table = SingleTableBuilder.binary(file).build()) {
        LongValue c = table.acquireValueFor("c");
        LongValue b = table.acquireValueFor("b");
        assertEquals(Long.MIN_VALUE, c.getVolatileValue());
        assertTrue(c.compareAndSwapValue(Long.MIN_VALUE, 3));
        assertEquals(2, b.getVolatileValue());
        assertTrue(b.compareAndSwapValue(2, 22));
        assertEquals("--- !!meta-data #binary\n" + "header: !STStore {\n" + "  wireType: !WireType BINARY_LIGHT,\n" + "  recovery: !TimedStoreRecovery {\n" + "    timeStamp: 0\n" + "  }\n" + "}\n" + "# position: 112, header: 0\n" + "--- !!data #binary\n" + "a: 1\n" + "# position: 128, header: 1\n" + "--- !!data #binary\n" + "b: 22\n" + "# position: 144, header: 2\n" + "--- !!data #binary\n" + "c: 3\n" + "...\n" + "# 65372 bytes remaining\n", table.dump());
        System.out.println(table.dump());
    }
}
Also used : LongValue(net.openhft.chronicle.core.values.LongValue) File(java.io.File) Test(org.junit.Test)

Example 3 with LongValue

use of net.openhft.chronicle.core.values.LongValue in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueue method appendMetaDataReturnAddress.

/**
 * This method does not update the index, as indexes are not used for meta data
 *
 * @param buffer
 * @return the addressForRead of the appended data
 */
private long appendMetaDataReturnAddress(@NotNull Bytes buffer) {
    long length = checkRemainingForAppend(buffer);
    LongValue writeByte = header.writeByte();
    long lastByte = writeByte.getVolatileValue();
    for (; ; ) {
        if (bytes.compareAndSwapInt(lastByte, 0, NOT_COMPLETE | (int) length)) {
            long lastByte2 = lastByte + 4 + buffer.remaining();
            bytes.write(lastByte + 4, buffer);
            writeByte.setOrderedValue(lastByte2);
            bytes.writeOrderedInt(lastByte, (int) (META_DATA | length));
            return lastByte;
        }
        int length2 = length30(bytes.readVolatileInt());
        bytes.skip(length2);
        try {
            Jvm.checkInterrupted();
        } catch (InterruptedException e) {
            throw new InterruptedRuntimeException(e);
        }
    }
}
Also used : LongValue(net.openhft.chronicle.core.values.LongValue)

Example 4 with LongValue

use of net.openhft.chronicle.core.values.LongValue in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueStore method loadWritePosition.

private LongValue loadWritePosition(@NotNull WireIn wire) {
    final ValueIn read = wire.read(MetaDataField.writePosition);
    final int code;
    final long start = wire.bytes().readPosition();
    try {
        wire.consumePadding();
        code = wire.bytes().uncheckedReadUnsignedByte();
    } finally {
        wire.bytes().readPosition(start);
    }
    if (code == BinaryWireCode.I64_ARRAY) {
        TwoLongValue result = wire.newTwoLongReference();
        // when the write position is and array it also encodes the sequence number in the write position as the second long value
        read.int128(result);
        return result;
    }
    final LongValue result = wire.newLongReference();
    read.int64(result);
    return result;
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) TwoLongValue(net.openhft.chronicle.core.values.TwoLongValue) LongValue(net.openhft.chronicle.core.values.LongValue) TwoLongValue(net.openhft.chronicle.core.values.TwoLongValue)

Example 5 with LongValue

use of net.openhft.chronicle.core.values.LongValue in project Chronicle-Queue by OpenHFT.

the class SingleTableStore method acquireValueFor.

/**
 * {@inheritDoc}
 */
@Override
public synchronized LongValue acquireValueFor(CharSequence key) {
    // TODO Change to ThreadLocal values if performance is a problem.
    StringBuilder sb = Wires.acquireStringBuilder();
    mappedBytes.reserve();
    try {
        mappedBytes.readPosition(0);
        mappedBytes.readLimit(mappedBytes.realCapacity());
        while (mappedWire.readDataHeader()) {
            int header = mappedBytes.readInt();
            if (Wires.isNotComplete(header))
                break;
            long readPosition = mappedBytes.readPosition();
            int length = Wires.lengthOf(header);
            ValueIn valueIn = mappedWire.readEventName(sb);
            if (StringUtils.equalsCaseIgnore(key, sb)) {
                return valueIn.int64ForBinding(null);
            }
            mappedBytes.readPosition(readPosition + length);
        }
        // not found
        int safeLength = Maths.toUInt31(mappedBytes.realCapacity() - mappedBytes.readPosition());
        mappedBytes.writeLimit(mappedBytes.realCapacity());
        mappedBytes.writePosition(mappedBytes.readPosition());
        long pos = recovery.writeHeader(mappedWire, safeLength, timeoutMS, null, null);
        LongValue longValue = wireType.newLongReference().get();
        mappedWire.writeEventName(key).int64forBinding(Long.MIN_VALUE, longValue);
        mappedWire.updateHeader(pos, false);
        return longValue;
    } catch (StreamCorruptedException | EOFException e) {
        throw new IORuntimeException(e);
    } finally {
        mappedBytes.release();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) LongValue(net.openhft.chronicle.core.values.LongValue) EOFException(java.io.EOFException) StreamCorruptedException(java.io.StreamCorruptedException)

Aggregations

LongValue (net.openhft.chronicle.core.values.LongValue)6 Test (org.junit.Test)2 EOFException (java.io.EOFException)1 File (java.io.File)1 StreamCorruptedException (java.io.StreamCorruptedException)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 TwoLongValue (net.openhft.chronicle.core.values.TwoLongValue)1 TableStore (net.openhft.chronicle.queue.impl.TableStore)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1