Search in sources :

Example 1 with DataOutputBufferFixed

use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.

the class DurationSerializer method serialize.

public ByteBuffer serialize(Duration duration) {
    if (duration == null)
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;
    long months = duration.getMonths();
    long days = duration.getDays();
    long nanoseconds = duration.getNanoseconds();
    int size = VIntCoding.computeVIntSize(months) + VIntCoding.computeVIntSize(days) + VIntCoding.computeVIntSize(nanoseconds);
    try (DataOutputBufferFixed output = new DataOutputBufferFixed(size)) {
        output.writeVInt(months);
        output.writeVInt(days);
        output.writeVInt(nanoseconds);
        return output.buffer();
    } catch (IOException e) {
        // this should never happen with a DataOutputBufferFixed
        throw new AssertionError("Unexpected error", e);
    }
}
Also used : IOException(java.io.IOException) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Example 2 with DataOutputBufferFixed

use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.

the class RepairMessageSerializationsTest method serializeRoundTrip.

private <T extends RepairMessage> T serializeRoundTrip(T msg, IVersionedSerializer<T> serializer) throws IOException {
    long size = serializer.serializedSize(msg, PROTOCOL_VERSION);
    ByteBuffer buf = ByteBuffer.allocate((int) size);
    DataOutputPlus out = new DataOutputBufferFixed(buf);
    serializer.serialize(msg, out, PROTOCOL_VERSION);
    Assert.assertEquals(size, buf.position());
    buf.flip();
    DataInputPlus in = new DataInputBuffer(buf, false);
    T deserialized = serializer.deserialize(in, PROTOCOL_VERSION);
    Assert.assertEquals(msg, deserialized);
    Assert.assertEquals(msg.hashCode(), deserialized.hashCode());
    return deserialized;
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) ByteBuffer(java.nio.ByteBuffer) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Example 3 with DataOutputBufferFixed

use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.

the class CommitLog method add.

/**
     * Add a Mutation to the commit log. If CDC is enabled, this can fail.
     *
     * @param mutation the Mutation to add to the log
     * @throws WriteTimeoutException
     */
public CommitLogPosition add(Mutation mutation) throws WriteTimeoutException {
    assert mutation != null;
    try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
        Mutation.serializer.serialize(mutation, dob, MessagingService.current_version);
        int size = dob.getLength();
        int totalSize = size + ENTRY_OVERHEAD_SIZE;
        if (totalSize > MAX_MUTATION_SIZE) {
            throw new IllegalArgumentException(String.format("Mutation of %s is too large for the maximum size of %s", FBUtilities.prettyPrintMemory(totalSize), FBUtilities.prettyPrintMemory(MAX_MUTATION_SIZE)));
        }
        Allocation alloc = segmentManager.allocate(mutation, totalSize);
        CRC32 checksum = new CRC32();
        final ByteBuffer buffer = alloc.getBuffer();
        try (BufferedDataOutputStreamPlus dos = new DataOutputBufferFixed(buffer)) {
            // checksummed length
            dos.writeInt(size);
            updateChecksumInt(checksum, size);
            buffer.putInt((int) checksum.getValue());
            // checksummed mutation
            dos.write(dob.getData(), 0, size);
            updateChecksum(checksum, buffer, buffer.position() - size, size);
            buffer.putInt((int) checksum.getValue());
        } catch (IOException e) {
            throw new FSWriteError(e, alloc.getSegment().getPath());
        } finally {
            alloc.markWritten();
        }
        executor.finishWriteFor(alloc);
        return alloc.getCommitLogPosition();
    } catch (IOException e) {
        throw new FSWriteError(e, segmentManager.allocatingFrom().getPath());
    }
}
Also used : Allocation(org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation) CRC32(java.util.zip.CRC32) FSWriteError(org.apache.cassandra.io.FSWriteError) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) ByteBuffer(java.nio.ByteBuffer) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Example 4 with DataOutputBufferFixed

use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.

the class PagingState method serialize.

public ByteBuffer serialize(ProtocolVersion protocolVersion) {
    assert rowMark == null || protocolVersion == rowMark.protocolVersion;
    try (DataOutputBuffer out = new DataOutputBufferFixed(serializedSize(protocolVersion))) {
        ByteBuffer pk = partitionKey == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : partitionKey;
        ByteBuffer mark = rowMark == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : rowMark.mark;
        if (protocolVersion.isSmallerOrEqualTo(ProtocolVersion.V3)) {
            ByteBufferUtil.writeWithShortLength(pk, out);
            ByteBufferUtil.writeWithShortLength(mark, out);
            out.writeInt(remaining);
            out.writeInt(remainingInPartition);
        } else {
            ByteBufferUtil.writeWithVIntLength(pk, out);
            ByteBufferUtil.writeWithVIntLength(mark, out);
            out.writeUnsignedVInt(remaining);
            out.writeUnsignedVInt(remainingInPartition);
        }
        return out.buffer();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Example 5 with DataOutputBufferFixed

use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.

the class StreamInitMessage method createMessage.

/**
     * Create serialized message.
     *
     * @param compress true if message is compressed
     * @param version Streaming protocol version
     * @return serialized message in ByteBuffer format
     */
public ByteBuffer createMessage(boolean compress, int version) {
    int header = 0;
    // set compression bit.
    if (compress)
        header |= 4;
    // set streaming bit
    header |= 8;
    // Setting up the version bit
    header |= (version << 8);
    byte[] bytes;
    try {
        int size = (int) StreamInitMessage.serializer.serializedSize(this, version);
        try (DataOutputBuffer buffer = new DataOutputBufferFixed(size)) {
            StreamInitMessage.serializer.serialize(this, buffer, version);
            bytes = buffer.getData();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    assert bytes.length > 0;
    ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + bytes.length);
    buffer.putInt(MessagingService.PROTOCOL_MAGIC);
    buffer.putInt(header);
    buffer.put(bytes);
    buffer.flip();
    return buffer;
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Aggregations

DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)6 ByteBuffer (java.nio.ByteBuffer)4 IOException (java.io.IOException)3 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)3 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)2 CRC32 (java.util.zip.CRC32)1 Allocation (org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation)1 FSWriteError (org.apache.cassandra.io.FSWriteError)1 BufferedDataOutputStreamPlus (org.apache.cassandra.io.util.BufferedDataOutputStreamPlus)1 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)1 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)1 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1