Search in sources :

Example 31 with DataOutputBuffer

use of org.apache.cassandra.io.util.DataOutputBuffer 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)

Example 32 with DataOutputBuffer

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

the class MerkleTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    // populate and validate the tree
    mt.maxsize(256);
    mt.init();
    for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
    byte[] initialhash = mt.hash(full);
    DataOutputBuffer out = new DataOutputBuffer();
    MerkleTree.serializer.serialize(mt, out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();
    DataInputPlus in = new DataInputBuffer(serialized);
    MerkleTree restored = MerkleTree.serializer.deserialize(in, MessagingService.current_version);
    assertHashEquals(initialhash, restored.hash(full));
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 33 with DataOutputBuffer

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

the class StreamingHistogramTest method testNumericTypes.

@Test
public void testNumericTypes() throws Exception {
    StreamingHistogram hist = new StreamingHistogram(5, 0, 1);
    hist.update(2);
    hist.update(2.0);
    hist.update(2L);
    Map<Number, long[]> asMap = hist.getAsMap();
    assertEquals(1, asMap.size());
    assertEquals(3L, asMap.get(2)[0]);
    //Make sure it's working with Serde
    DataOutputBuffer out = new DataOutputBuffer();
    StreamingHistogram.serializer.serialize(hist, out);
    byte[] bytes = out.toByteArray();
    StreamingHistogram deserialized = StreamingHistogram.serializer.deserialize(new DataInputBuffer(bytes));
    deserialized.update(2L);
    asMap = deserialized.getAsMap();
    assertEquals(1, asMap.size());
    assertEquals(4L, asMap.get(2)[0]);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) Test(org.junit.Test)

Example 34 with DataOutputBuffer

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

the class BatchlogManager method store.

public static void store(Batch batch, boolean durableWrites) {
    List<ByteBuffer> mutations = new ArrayList<>(batch.encodedMutations.size() + batch.decodedMutations.size());
    mutations.addAll(batch.encodedMutations);
    for (Mutation mutation : batch.decodedMutations) {
        try (DataOutputBuffer buffer = new DataOutputBuffer()) {
            Mutation.serializer.serialize(mutation, buffer, MessagingService.current_version);
            mutations.add(buffer.buffer());
        } catch (IOException e) {
            // shouldn't happen
            throw new AssertionError(e);
        }
    }
    PartitionUpdate.SimpleBuilder builder = PartitionUpdate.simpleBuilder(SystemKeyspace.Batches, batch.id);
    builder.row().timestamp(batch.creationTime).add("version", MessagingService.current_version).appendAll("mutations", mutations);
    builder.buildAsMutation().apply(durableWrites);
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 35 with DataOutputBuffer

use of org.apache.cassandra.io.util.DataOutputBuffer 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)

Aggregations

DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)68 Test (org.junit.Test)25 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)17 IOException (java.io.IOException)16 ByteBuffer (java.nio.ByteBuffer)15 ByteArrayInputStream (java.io.ByteArrayInputStream)10 DataInputStream (java.io.DataInputStream)10 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)8 File (java.io.File)6 CRC32 (java.util.zip.CRC32)5 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)3 SequentialWriter (org.apache.cassandra.io.util.SequentialWriter)3 Message (org.apache.cassandra.net.Message)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Mutation (org.apache.cassandra.db.Mutation)2