Search in sources :

Example 16 with MarshalException

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

the class UserType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof Map))
        throw new MarshalException(String.format("Expected a map, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    Map<String, Object> map = (Map<String, Object>) parsed;
    Json.handleCaseSensitivity(map);
    List<Term> terms = new ArrayList<>(types.size());
    Set keys = map.keySet();
    assert keys.isEmpty() || keys.iterator().next() instanceof String;
    int foundValues = 0;
    for (int i = 0; i < types.size(); i++) {
        Object value = map.get(stringFieldNames.get(i));
        if (value == null) {
            terms.add(Constants.NULL_VALUE);
        } else {
            terms.add(types.get(i).fromJSONObject(value));
            foundValues += 1;
        }
    }
    // check for extra, unrecognized fields
    if (foundValues != map.size()) {
        for (Object fieldName : keys) {
            if (!stringFieldNames.contains(fieldName))
                throw new MarshalException(String.format("Unknown field '%s' in value of user defined type %s", fieldName, getNameAsString()));
        }
    }
    return new UserTypes.DelayedValue(this, terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException)

Example 17 with MarshalException

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

the class ListType 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, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    List list = (List) parsed;
    List<Term> terms = new ArrayList<>(list.size());
    for (Object element : list) {
        if (element == null)
            throw new MarshalException("Invalid null element in list");
        terms.add(elements.fromJSONObject(element));
    }
    return new Lists.DelayedValue(terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) Term(org.apache.cassandra.cql3.Term)

Example 18 with MarshalException

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

the class MapType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof Map))
        throw new MarshalException(String.format("Expected a map, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    Map<Object, Object> map = (Map<Object, Object>) parsed;
    Map<Term, Term> terms = new HashMap<>(map.size());
    for (Map.Entry<Object, Object> entry : map.entrySet()) {
        if (entry.getKey() == null)
            throw new MarshalException("Invalid null key in map");
        if (entry.getValue() == null)
            throw new MarshalException("Invalid null value in map");
        terms.put(keys.fromJSONObject(entry.getKey()), values.fromJSONObject(entry.getValue()));
    }
    return new Maps.DelayedValue(keys, terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Term(org.apache.cassandra.cql3.Term) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 19 with MarshalException

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

the class DynamicCompositeType method validateComparator.

protected <V> AbstractType<?> validateComparator(int i, V input, ValueAccessor<V> accessor, int offset) throws MarshalException {
    AbstractType<?> comparator = null;
    if (accessor.sizeFromOffset(input, offset) < 2)
        throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
    int header = accessor.getShort(input, offset);
    offset += TypeSizes.SHORT_SIZE;
    if ((header & 0x8000) == 0) {
        if (accessor.sizeFromOffset(input, offset) < header)
            throw new MarshalException("Not enough bytes to read comparator name of component " + i);
        V value = accessor.slice(input, offset, header);
        String valueStr = null;
        try {
            valueStr = accessor.toString(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 when decoding the byte buffer in ByteBufferUtil.string()", ce);
        } 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);
        }
    } 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) CharacterCodingException(java.nio.charset.CharacterCodingException) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) SyntaxException(org.apache.cassandra.exceptions.SyntaxException) MarshalException(org.apache.cassandra.serializers.MarshalException)

Example 20 with MarshalException

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

the class NonTokenizingAnalyzer method hasNext.

public boolean hasNext() {
    // check that we know how to handle the input, otherwise bail
    if (!VALID_ANALYZABLE_TYPES.contains(validator))
        return false;
    if (hasNext) {
        String inputStr;
        try {
            inputStr = validator.getString(input);
            if (inputStr == null)
                throw new MarshalException(String.format("'null' deserialized value for %s with %s", ByteBufferUtil.bytesToHex(input), validator));
            Object pipelineRes = FilterPipelineExecutor.execute(filterPipeline, inputStr);
            if (pipelineRes == null)
                return false;
            next = validator.fromString(normalize((String) pipelineRes));
            return true;
        } catch (MarshalException e) {
            logger.error("Failed to deserialize value with " + validator, e);
            return false;
        } finally {
            hasNext = false;
        }
    }
    return false;
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException)

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