net.openhft.chronicle.wire.TextWire Java Examples

The following examples show how to use net.openhft.chronicle.wire.TextWire. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
/**
 * @return ByteBuffer, with self bootstrapping header in [position, limit) range
 */
private static <K, V> ByteBuffer writeHeader(
        FileChannel fileChannel, VanillaChronicleMap<K, V, ?> map) throws IOException {
    ByteBuffer headerBuffer = ByteBuffer.allocate(
            SELF_BOOTSTRAPPING_HEADER_OFFSET + MAX_BOOTSTRAPPING_HEADER_SIZE);
    headerBuffer.order(LITTLE_ENDIAN);

    Bytes<ByteBuffer> headerBytes = Bytes.wrapForWrite(headerBuffer);
    headerBytes.writePosition(SELF_BOOTSTRAPPING_HEADER_OFFSET);
    Wire wire = new TextWire(headerBytes);
    wire.getValueOut().typedMarshallable(map);

    int headerLimit = (int) headerBytes.writePosition();
    int headerSize = headerLimit - SELF_BOOTSTRAPPING_HEADER_OFFSET;
    // First set readiness bit to READY, to compute checksum correctly
    //noinspection PointlessBitwiseExpression
    headerBuffer.putInt(SIZE_WORD_OFFSET, READY | DATA | headerSize);

    long checksum = headerChecksum(headerBuffer, headerSize);
    headerBuffer.putLong(HEADER_OFFSET, checksum);

    // Set readiness bit to NOT_COMPLETE, because the Chronicle Map instance is not actually
    // ready yet
    //noinspection PointlessBitwiseExpression
    headerBuffer.putInt(SIZE_WORD_OFFSET, NOT_COMPLETE | DATA | headerSize);

    // Write the size-prefixed blob to the file
    headerBuffer.position(0).limit(headerLimit);
    writeFully(fileChannel, 0, headerBuffer);

    headerBuffer.position(SELF_BOOTSTRAPPING_HEADER_OFFSET);
    return headerBuffer;
}
 
Example #2
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters
public static Collection<Class[]> data() {
    return Arrays.asList(new Class[][]{
            {BinaryWire.class},
            {TextWire.class}
    });
}
 
Example #3
Source File: SslClusterIntegrationTest.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    TextWire.fromFile("src/test/resources/ssl-test-cluster.yaml").read().marshallable(nodeOne);
    TextWire.fromFile("src/test/resources/ssl-test-cluster.yaml").read().marshallable(nodeTwo);
    TCPRegistry.createServerSocketChannelFor("host1.port", "host2.port");
}
 
Example #4
Source File: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private VanillaChronicleMap<K, V, ?> openWithExistingFile(
        File file, RandomAccessFile raf, ChronicleHashResources resources,
        boolean recover, boolean overrideBuilderConfig,
        ChronicleHashCorruption.Listener corruptionListener)
        throws IOException {
    ChronicleHashCorruptionImpl corruption = recover ? new ChronicleHashCorruptionImpl() : null;
    try {
        int headerSize = waitUntilReady(raf, file, recover);
        FileChannel fileChannel = raf.getChannel();
        ByteBuffer headerBuffer = readSelfBootstrappingHeader(
                file, raf, headerSize, recover, corruptionListener, corruption);
        if (headerSize != headerBuffer.remaining())
            throw new AssertionError();
        boolean headerCorrect = checkSumSelfBootstrappingHeader(headerBuffer, headerSize);
        boolean headerWritten = false;
        if (!headerCorrect) {
            if (overrideBuilderConfig) {
                VanillaChronicleMap<K, V, ?> mapObjectForHeaderOverwrite = newMap();
                headerBuffer = writeHeader(fileChannel, mapObjectForHeaderOverwrite);
                headerSize = headerBuffer.remaining();
                headerWritten = true;
            } else {
                throw throwRecoveryOrReturnIOException(file,
                        "Self Bootstrapping Header checksum doesn't match the stored checksum",
                        recover);
            }
        }
        Bytes<ByteBuffer> headerBytes = Bytes.wrapForRead(headerBuffer);
        headerBytes.readPosition(headerBuffer.position());
        headerBytes.readLimit(headerBuffer.limit());
        Wire wire = new TextWire(headerBytes);
        VanillaChronicleMap<K, V, ?> map = wire.getValueIn().typedMarshallable();
        map.initBeforeMapping(file, raf, headerBuffer.limit(), recover);
        long dataStoreSize = map.globalMutableState().getDataStoreSize();
        if (!recover && dataStoreSize > file.length()) {
            throw new IOException("The file " + file + " the map is serialized from " +
                    "has unexpected length " + file.length() + ", probably corrupted. " +
                    "Data store size is " + dataStoreSize);
        }
        map.initTransientsFromBuilder(this);
        if (!recover) {
            map.createMappedStoreAndSegments(resources);
        } else {
            if (!headerWritten)
                writeNotComplete(fileChannel, headerBuffer, headerSize);
            map.recover(resources, corruptionListener, corruption);
            commitChronicleMapReady(map, raf, headerBuffer, headerSize);
        }
        return map;
    } catch (Throwable t) {
        if (recover && !(t instanceof IOException) &&
                !(t instanceof ChronicleHashRecoveryFailedException)) {
            throw new ChronicleHashRecoveryFailedException(t);
        }
        throw Throwables.propagateNotWrapping(t, IOException.class);
    }
}
 
Example #5
Source File: ClientWiredChronicleQueueStateless.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public ExcerptTailer createTailer() throws IOException {
    return new ClientWiredExcerptTailerStateless(this, hub, TextWire::new);
}
 
Example #6
Source File: ClientWiredChronicleQueueStateless.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public ExcerptAppender createAppender() throws IOException {
    return new ClientWiredExcerptAppenderStateless(this, hub, TextWire::new);
}