Search in sources :

Example 1 with StoreRecovery

use of net.openhft.chronicle.queue.impl.single.StoreRecovery in project Chronicle-Queue by OpenHFT.

the class SingleTableBuilder method build.

// *************************************************************************
// 
// *************************************************************************
@NotNull
public TableStore build() {
    if (readOnly && !file.exists()) {
        throw new IORuntimeException("File not found in readOnly mode");
    }
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 64 << 10, 0, readOnly);
        Wire wire = wireType.apply(bytes);
        StoreRecovery recovery = recoverySupplier.apply(wireType);
        try {
            TableStore tableStore;
            if ((!readOnly) && wire.writeFirstHeader()) {
                tableStore = writeTableStore(bytes, wire, recovery);
            } else {
                wire.readFirstHeader(timeoutMS, TimeUnit.MILLISECONDS);
                StringBuilder name = Wires.acquireStringBuilder();
                ValueIn valueIn = wire.readEventName(name);
                if (StringUtils.isEqual(name, MetaDataKeys.header.name())) {
                    tableStore = valueIn.typedMarshallable();
                } else {
                    // noinspection unchecked
                    throw new StreamCorruptedException("The first message should be the header, was " + name);
                }
            }
            return tableStore;
        } catch (TimeoutException e) {
            recovery.recoverAndWriteHeader(wire, 10_000, null, null);
            return writeTableStore(bytes, wire, recovery);
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimedStoreRecovery(net.openhft.chronicle.queue.impl.single.TimedStoreRecovery) StoreRecovery(net.openhft.chronicle.queue.impl.single.StoreRecovery) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TableStore(net.openhft.chronicle.queue.impl.TableStore) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 TimeoutException (java.util.concurrent.TimeoutException)1 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 TableStore (net.openhft.chronicle.queue.impl.TableStore)1 StoreRecovery (net.openhft.chronicle.queue.impl.single.StoreRecovery)1 TimedStoreRecovery (net.openhft.chronicle.queue.impl.single.TimedStoreRecovery)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1 Wire (net.openhft.chronicle.wire.Wire)1 NotNull (org.jetbrains.annotations.NotNull)1