Search in sources :

Example 11 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class UserType method validate.

// Note: the only reason we override this is to provide nicer error message, but since that's not that much code...
@Override
public void validate(ByteBuffer bytes) throws MarshalException {
    ByteBuffer input = bytes.duplicate();
    for (int i = 0; i < size(); i++) {
        // we allow the input to have less fields than declared so as to support field addition.
        if (!input.hasRemaining())
            return;
        if (input.remaining() < 4)
            throw new MarshalException(String.format("Not enough bytes to read size of %dth field %s", i, fieldNameAsString(i)));
        int size = input.getInt();
        // size < 0 means null value
        if (size < 0)
            continue;
        if (input.remaining() < size)
            throw new MarshalException(String.format("Not enough bytes to read %dth field %s", i, fieldNameAsString(i)));
        ByteBuffer field = ByteBufferUtil.readBytes(input, size);
        types.get(i).validate(field);
    }
    // We're allowed to get less fields than declared, but not more
    if (input.hasRemaining())
        throw new MarshalException("Invalid remaining data after end of UDT value");
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class DynamicCompositeType method validateComparator.

protected AbstractType<?> validateComparator(int i, ByteBuffer bb) throws MarshalException {
    AbstractType<?> comparator = null;
    if (bb.remaining() < 2)
        throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
    int header = ByteBufferUtil.readShortLength(bb);
    if ((header & 0x8000) == 0) {
        if (bb.remaining() < header)
            throw new MarshalException("Not enough bytes to read comparator name of component " + i);
        ByteBuffer value = ByteBufferUtil.readBytes(bb, header);
        String valueStr = null;
        try {
            valueStr = ByteBufferUtil.string(value);
            comparator = TypeParser.parse(valueStr);
        } catch (CharacterCodingException ce) {
            // ByteBufferUtil.string failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed with [{}] when decoding the byte buffer in ByteBufferUtil.string()", ce.toString());
        } catch (Exception e) {
            // parse failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed to parse value string \"{}\" with exception: [{}]", valueStr, e.toString());
        }
    } else {
        comparator = aliases.get((byte) (header & 0xFF));
    }
    if (comparator == null)
        throw new MarshalException("Cannot find comparator for component " + i);
    else
        return comparator;
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) SyntaxException(org.apache.cassandra.exceptions.SyntaxException) MarshalException(org.apache.cassandra.serializers.MarshalException)

Example 13 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class Attributes method getTimeToLive.

public int getTimeToLive(QueryOptions options, int defaultTimeToLive) throws InvalidRequestException {
    if (timeToLive == null)
        return defaultTimeToLive;
    ByteBuffer tval = timeToLive.bindAndGet(options);
    if (tval == null)
        return 0;
    if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)
        return defaultTimeToLive;
    try {
        Int32Type.instance.validate(tval);
    } catch (MarshalException e) {
        throw new InvalidRequestException("Invalid timestamp value: " + tval);
    }
    int ttl = Int32Type.instance.compose(tval);
    if (ttl < 0)
        throw new InvalidRequestException("A TTL must be greater or equal to 0, but was " + ttl);
    if (ttl > MAX_TTL)
        throw new InvalidRequestException(String.format("ttl is too large. requested (%d) maximum (%d)", ttl, MAX_TTL));
    if (defaultTimeToLive != LivenessInfo.NO_TTL && ttl == LivenessInfo.NO_TTL)
        return LivenessInfo.NO_TTL;
    return ttl;
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ByteBuffer(java.nio.ByteBuffer)

Example 14 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class AbstractRow method validateData.

public void validateData(TableMetadata metadata) {
    validateClustering(metadata, clustering());
    primaryKeyLivenessInfo().validate();
    if (deletion().time().localDeletionTime() < 0)
        throw new MarshalException("A local deletion time should not be negative in '" + metadata + "'");
    apply(cd -> cd.validate());
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException)

Example 15 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class SetType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof List))
        throw new MarshalException(String.format("Expected a list (representing a set), but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    List list = (List) parsed;
    Set<Term> terms = new HashSet<>(list.size());
    for (Object element : list) {
        if (element == null)
            throw new MarshalException("Invalid null element in set");
        terms.add(elements.fromJSONObject(element));
    }
    return new Sets.DelayedValue(elements, terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) Term(org.apache.cassandra.cql3.Term)

Aggregations

MarshalException (org.apache.cassandra.serializers.MarshalException)24 ByteBuffer (java.nio.ByteBuffer)10 CharacterCodingException (java.nio.charset.CharacterCodingException)3 Term (org.apache.cassandra.cql3.Term)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 IOException (java.io.IOException)2 UUID (java.util.UUID)2 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)2 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 SyntaxException (org.apache.cassandra.exceptions.SyntaxException)2 IVerbHandler (org.apache.cassandra.net.IVerbHandler)2 MessagingService (org.apache.cassandra.net.MessagingService)2 StorageProxy (org.apache.cassandra.service.StorageProxy)2 StorageService (org.apache.cassandra.service.StorageService)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Objects (com.google.common.base.Objects)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.concat (com.google.common.collect.Iterables.concat)1 Iterables.transform (com.google.common.collect.Iterables.transform)1