Search in sources :

Example 16 with DataOutputBuffer

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

the class ColumnIndex method reuseOrAllocateBuffer.

private DataOutputBuffer reuseOrAllocateBuffer() {
    // ColumnIndex instance and return it.
    if (reusableBuffer != null) {
        DataOutputBuffer buffer = reusableBuffer;
        buffer.clear();
        return buffer;
    }
    // don't use the standard RECYCLER as that only recycles up to 1MB and requires proper cleanup
    return new DataOutputBuffer(DatabaseDescriptor.getColumnIndexCacheSize() * 2);
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer)

Example 17 with DataOutputBuffer

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

the class UnfilteredSerializer method serialize.

private void serialize(Row row, SerializationHeader header, DataOutputPlus out, long previousUnfilteredSize, int version) throws IOException {
    int flags = 0;
    int extendedFlags = 0;
    boolean isStatic = row.isStatic();
    Columns headerColumns = header.columns(isStatic);
    LivenessInfo pkLiveness = row.primaryKeyLivenessInfo();
    Row.Deletion deletion = row.deletion();
    boolean hasComplexDeletion = row.hasComplexDeletion();
    boolean hasAllColumns = (row.size() == headerColumns.size());
    boolean hasExtendedFlags = hasExtendedFlags(row);
    if (isStatic)
        extendedFlags |= IS_STATIC;
    if (!pkLiveness.isEmpty())
        flags |= HAS_TIMESTAMP;
    if (pkLiveness.isExpiring())
        flags |= HAS_TTL;
    if (!deletion.isLive()) {
        flags |= HAS_DELETION;
        if (deletion.isShadowable())
            extendedFlags |= HAS_SHADOWABLE_DELETION;
    }
    if (hasComplexDeletion)
        flags |= HAS_COMPLEX_DELETION;
    if (hasAllColumns)
        flags |= HAS_ALL_COLUMNS;
    if (hasExtendedFlags)
        flags |= EXTENSION_FLAG;
    out.writeByte((byte) flags);
    if (hasExtendedFlags)
        out.writeByte((byte) extendedFlags);
    if (!isStatic)
        Clustering.serializer.serialize(row.clustering(), out, version, header.clusteringTypes());
    if (header.isForSSTable()) {
        try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
            serializeRowBody(row, flags, header, dob);
            out.writeUnsignedVInt(dob.position() + TypeSizes.sizeofUnsignedVInt(previousUnfilteredSize));
            // We write the size of the previous unfiltered to make reverse queries more efficient (and simpler).
            // This is currently not used however and using it is tbd.
            out.writeUnsignedVInt(previousUnfilteredSize);
            out.write(dob.getData(), 0, dob.getLength());
        }
    } else {
        serializeRowBody(row, flags, header, out);
    }
}
Also used : Deletion(org.apache.cassandra.db.rows.Row.Deletion) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer)

Example 18 with DataOutputBuffer

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

the class HintsWriter method create.

// HintsWriter owns channel
@SuppressWarnings("resource")
static HintsWriter create(File directory, HintsDescriptor descriptor) throws IOException {
    File file = new File(directory, descriptor.fileName());
    FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
    int fd = CLibrary.getfd(channel);
    CRC32 crc = new CRC32();
    try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
        // write the descriptor
        descriptor.serialize(dob);
        ByteBuffer descriptorBytes = dob.buffer();
        updateChecksum(crc, descriptorBytes);
        channel.write(descriptorBytes);
    } catch (Throwable e) {
        channel.close();
        throw e;
    }
    if (descriptor.isEncrypted())
        return new EncryptedHintsWriter(directory, descriptor, file, channel, fd, crc);
    if (descriptor.isCompressed())
        return new CompressedHintsWriter(directory, descriptor, file, channel, fd, crc);
    return new HintsWriter(directory, descriptor, file, channel, fd, crc);
}
Also used : CRC32(java.util.zip.CRC32) FileChannel(java.nio.channels.FileChannel) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 19 with DataOutputBuffer

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

the class MessageDeliveryTask method handleFailure.

private void handleFailure(Throwable t) {
    if (message.doCallbackOnFailure()) {
        MessageOut response = new MessageOut(MessagingService.Verb.INTERNAL_RESPONSE).withParameter(MessagingService.FAILURE_RESPONSE_PARAM, MessagingService.ONE_BYTE);
        if (t instanceof TombstoneOverwhelmingException) {
            try (DataOutputBuffer out = new DataOutputBuffer()) {
                out.writeShort(RequestFailureReason.READ_TOO_MANY_TOMBSTONES.code);
                response = response.withParameter(MessagingService.FAILURE_REASON_PARAM, out.getData());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        MessagingService.instance().sendReply(response, id, message.from);
    }
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) TombstoneOverwhelmingException(org.apache.cassandra.db.filter.TombstoneOverwhelmingException)

Example 20 with DataOutputBuffer

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

the class MessageOut method serialize.

public void serialize(DataOutputPlus out, int version) throws IOException {
    CompactEndpointSerializationHelper.serialize(from, out);
    out.writeInt(verb.getId());
    out.writeInt(parameters.size());
    for (Map.Entry<String, byte[]> entry : parameters.entrySet()) {
        out.writeUTF(entry.getKey());
        out.writeInt(entry.getValue().length);
        out.write(entry.getValue());
    }
    if (payload != null) {
        try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
            serializer.serialize(payload, dob, version);
            int size = dob.getLength();
            out.writeInt(size);
            out.write(dob.getData(), 0, size);
        }
    } else {
        out.writeInt(0);
    }
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

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