org.apache.cassandra.serializers.MarshalException Java Examples

The following examples show how to use org.apache.cassandra.serializers.MarshalException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimestampSerializerTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateStringToTimestamp()
{
    List<String> unparsedDates = new ArrayList<>();
    for (String date: dates)
    {
        try
        {
            long millis = TimestampSerializer.dateStringToTimestamp(date);
        }
        catch (MarshalException e)
        {
            unparsedDates.add(date);
        }
    }
    assertTrue("Unable to parse: " + unparsedDates, unparsedDates.isEmpty());
}
 
Example #2
Source File: FloatType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
  // Return an empty ByteBuffer for an empty string.
  if (source.isEmpty())
      return ByteBufferUtil.EMPTY_BYTE_BUFFER;

  try
  {
      float f = Float.parseFloat(source);
      return ByteBufferUtil.bytes(f);
  }
  catch (NumberFormatException e1)
  {
      throw new MarshalException(String.format("unable to coerce '%s' to a float", source), e1);
  }
}
 
Example #3
Source File: LongType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    long longType;

    try
    {
        longType = Long.parseLong(source);
    }
    catch (Exception e)
    {
        throw new MarshalException(String.format("unable to make long from '%s'", source), e);
    }

    return decompose(longType);
}
 
Example #4
Source File: ThriftValidation.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static void validateKey(CFMetaData metadata, ByteBuffer key) throws org.apache.cassandra.exceptions.InvalidRequestException
{
    if (key == null || key.remaining() == 0)
    {
        throw new org.apache.cassandra.exceptions.InvalidRequestException("Key may not be empty");
    }

    // check that key can be handled by FBUtilities.writeShortByteArray
    if (key.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)
    {
        throw new org.apache.cassandra.exceptions.InvalidRequestException("Key length of " + key.remaining() +
                                                                          " is longer than maximum of " +
                                                                          FBUtilities.MAX_UNSIGNED_SHORT);
    }

    try
    {
        metadata.getKeyValidator().validate(key);
    }
    catch (MarshalException e)
    {
        throw new org.apache.cassandra.exceptions.InvalidRequestException(e.getMessage());
    }
}
 
Example #5
Source File: InetAddressType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    InetAddress address;

    try
    {
        address = InetAddress.getByName(source);
    }
    catch (Exception e)
    {
        throw new MarshalException(String.format("unable to make inetaddress from '%s'", source), e);
    }

    return decompose(address);
}
 
Example #6
Source File: ColumnDefinition.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static ColumnDefinition fromThrift(String ksName, String cfName, AbstractType<?> thriftComparator, AbstractType<?> thriftSubcomparator, ColumnDef thriftColumnDef) throws SyntaxException, ConfigurationException
{
    // For super columns, the componentIndex is 1 because the ColumnDefinition applies to the column component.
    Integer componentIndex = thriftSubcomparator != null ? 1 : null;
    AbstractType<?> comparator = thriftSubcomparator == null ? thriftComparator : thriftSubcomparator;
    try
    {
        comparator.validate(thriftColumnDef.name);
    }
    catch (MarshalException e)
    {
        throw new ConfigurationException(String.format("Column name %s is not valid for comparator %s", ByteBufferUtil.bytesToHex(thriftColumnDef.name), comparator));
    }

    return new ColumnDefinition(ksName,
                                cfName,
                                new ColumnIdentifier(ByteBufferUtil.clone(thriftColumnDef.name), comparator),
                                TypeParser.parse(thriftColumnDef.validation_class),
                                thriftColumnDef.index_type == null ? null : IndexType.valueOf(thriftColumnDef.index_type.name()),
                                thriftColumnDef.index_options,
                                thriftColumnDef.index_name,
                                componentIndex,
                                Kind.REGULAR);
}
 
Example #7
Source File: Int32Type.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    int int32Type;

    try
    {
        int32Type = Integer.parseInt(source);
    }
    catch (Exception e)
    {
        throw new MarshalException(String.format("unable to make int from '%s'", source), e);
    }

    return decompose(int32Type);
}
 
Example #8
Source File: QueryProcessor.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static void validateColumn(CFMetaData metadata, CellName name, ByteBuffer value)
throws InvalidRequestException
{
    validateColumnName(name);
    AbstractType<?> validator = metadata.getValueValidator(name);

    try
    {
        if (validator != null)
            validator.validate(value);
    }
    catch (MarshalException me)
    {
        throw new InvalidRequestException(String.format("Invalid column value for column (name=%s); %s",
                                                        ByteBufferUtil.bytesToHex(name.toByteBuffer()),
                                                        me.getMessage()));
    }
}
 
Example #9
Source File: Term.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the typed value, serialized to a ByteBuffer according to a
 * comparator/validator.
 *
 * @return a ByteBuffer of the value.
 * @throws InvalidRequestException if unable to coerce the string to its type.
 */
public ByteBuffer getByteBuffer(AbstractType<?> validator, List<ByteBuffer> variables) throws InvalidRequestException
{
    try
    {
        if (!isBindMarker()) return validator.fromStringCQL2(text);

        // must be a marker term so check for a CqlBindValue stored in the term
        if (bindIndex == null)
            throw new AssertionError("a marker Term was encountered with no index value");

        return variables.get(bindIndex);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #10
Source File: FunctionCall.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer executeInternal(Function fun, List<ByteBuffer> params) throws InvalidRequestException
{
    ByteBuffer result = fun.execute(params);
    try
    {
        // Check the method didn't lied on it's declared return type
        if (result != null)
            fun.returnType().validate(result);
        return result;
    }
    catch (MarshalException e)
    {
        throw new RuntimeException(String.format("Return of function %s (%s) is not a valid value for its declared return type %s", 
                                                 fun, ByteBufferUtil.bytesToHex(result), fun.returnType().asCQL3Type()));
    }
}
 
Example #11
Source File: Maps.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Value fromSerialized(ByteBuffer value, MapType type, int version) throws InvalidRequestException
{
    try
    {
        // Collections have this small hack that validate cannot be called on a serialized object,
        // but compose does the validation (so we're fine).
        Map<?, ?> m = (Map<?, ?>)type.getSerializer().deserializeForNativeProtocol(value, version);
        Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<ByteBuffer, ByteBuffer>(m.size());
        for (Map.Entry<?, ?> entry : m.entrySet())
            map.put(type.getKeysType().decompose(entry.getKey()), type.getValuesType().decompose(entry.getValue()));
        return new Value(map);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #12
Source File: SelectStatement.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private int getLimit(QueryOptions options) throws InvalidRequestException
{
    int l = Integer.MAX_VALUE;
    if (limit != null)
    {
        ByteBuffer b = limit.bindAndGet(options);
        if (b == null)
            throw new InvalidRequestException("Invalid null value of limit");

        try
        {
            Int32Type.instance.validate(b);
            l = Int32Type.instance.compose(b);
        }
        catch (MarshalException e)
        {
            throw new InvalidRequestException("Invalid limit value");
        }
    }

    if (l <= 0)
        throw new InvalidRequestException("LIMIT must be strictly positive");

    return l;
}
 
Example #13
Source File: DoubleType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
  // Return an empty ByteBuffer for an empty string.
  if (source.isEmpty())
      return ByteBufferUtil.EMPTY_BYTE_BUFFER;

  Double d;
  try
  {
      d = Double.valueOf(source);
  }
  catch (NumberFormatException e1)
  {
      throw new MarshalException(String.format("unable to coerce '%s' to a double", source), e1);
  }

  return decompose(d);
}
 
Example #14
Source File: BytesConversionFcts.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Function makeFromBlobFunction(final AbstractType<?> toType)
{
    final String name = "blobas" + toType.asCQL3Type();
    return new AbstractFunction(name, toType, BytesType.instance)
    {
        public ByteBuffer execute(List<ByteBuffer> parameters) throws InvalidRequestException
        {
            ByteBuffer val = parameters.get(0);
            try
            {
                if (val != null)
                    toType.validate(val);
                return val;
            }
            catch (MarshalException e)
            {
                throw new InvalidRequestException(String.format("In call to function %s, value 0x%s is not a valid binary representation for type %s",
                                                                name, ByteBufferUtil.bytesToHex(val), toType.asCQL3Type()));
            }
        }
    };
}
 
Example #15
Source File: Constants.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private ByteBuffer parsedValue(AbstractType<?> validator) throws InvalidRequestException
{
    if (validator instanceof ReversedType<?>)
        validator = ((ReversedType<?>) validator).baseType;
    try
    {
        // BytesType doesn't want it's input prefixed by '0x'.
        if (type == Type.HEX && validator instanceof BytesType)
            return validator.fromString(text.substring(2));
        if (validator instanceof CounterColumnType)
            return LongType.instance.fromString(text);
        return validator.fromString(text);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #16
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Value fromSerialized(ByteBuffer value, ListType type, int version) throws InvalidRequestException
{
    try
    {
        // Collections have this small hack that validate cannot be called on a serialized object,
        // but compose does the validation (so we're fine).
        List<?> l = (List<?>)type.getSerializer().deserializeForNativeProtocol(value, version);
        List<ByteBuffer> elements = new ArrayList<ByteBuffer>(l.size());
        for (Object element : l)
            // elements can be null in lists that represent a set of IN values
            elements.add(element == null ? null : type.getElementsType().decompose(element));
        return new Value(elements);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #17
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Value fromSerialized(ByteBuffer value, SetType type, int version) throws InvalidRequestException
{
    try
    {
        // Collections have this small hack that validate cannot be called on a serialized object,
        // but compose does the validation (so we're fine).
        Set<?> s = (Set<?>)type.getSerializer().deserializeForNativeProtocol(value, version);
        SortedSet<ByteBuffer> elements = new TreeSet<ByteBuffer>(type.getElementsType());
        for (Object element : s)
            elements.add(type.getElementsType().decompose(element));
        return new Value(elements);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #18
Source File: Attributes.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public long getTimestamp(long now, QueryOptions options) throws InvalidRequestException
{
    if (timestamp == null)
        return now;

    ByteBuffer tval = timestamp.bindAndGet(options);
    if (tval == null)
        throw new InvalidRequestException("Invalid null value of timestamp");

    try
    {
        LongType.instance.validate(tval);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException("Invalid timestamp value");
    }

    return LongType.instance.compose(tval);
}
 
Example #19
Source File: TimeUUIDTypeTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test(expected = MarshalException.class)
public void testInvalidTimeVersion()
{
    UUID uuid2 = UUID.fromString("00000000-0000-2100-0000-000000000000");
    assert uuid2.version() == 2;
    timeUUIDType.validate(ByteBuffer.wrap(UUIDGen.decompose(uuid2)));
}
 
Example #20
Source File: ThriftValidation.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Validates the data part of the column (everything in the column object but the name, which is assumed to be valid)
 */
public static void validateColumnData(CFMetaData metadata, ByteBuffer scName, Column column) throws org.apache.cassandra.exceptions.InvalidRequestException
{
    validateTtl(column);
    if (!column.isSetValue())
        throw new org.apache.cassandra.exceptions.InvalidRequestException("Column value is required");
    if (!column.isSetTimestamp())
        throw new org.apache.cassandra.exceptions.InvalidRequestException("Column timestamp is required");

    CellName cn = scName == null
                ? metadata.comparator.cellFromByteBuffer(column.name)
                : metadata.comparator.makeCellName(scName, column.name);
    try
    {
        AbstractType<?> validator = metadata.getValueValidator(cn);
        if (validator != null)
            validator.validate(column.value);
    }
    catch (MarshalException me)
    {
        if (logger.isDebugEnabled())
            logger.debug("rejecting invalid value {}", ByteBufferUtil.bytesToHex(summarize(column.value)));

        throw new org.apache.cassandra.exceptions.InvalidRequestException(String.format("(%s) [%s][%s][%s] failed validation",
                                                                  me.getMessage(),
                                                                  metadata.ksName,
                                                                  metadata.cfName,
                                                                  (SuperColumns.getComparatorFor(metadata, scName != null)).getString(column.name)));
    }

    // Indexed column values cannot be larger than 64K.  See CASSANDRA-3057/4240 for more details
    if (!Keyspace.open(metadata.ksName).getColumnFamilyStore(metadata.cfName).indexManager.validate(asDBColumn(cn, column)))
                throw new org.apache.cassandra.exceptions.InvalidRequestException(String.format("Can't index column value of size %d for index %s in CF %s of KS %s",
                                                                          column.value.remaining(),
                                                                          metadata.getColumnDefinition(cn).getIndexName(),
                                                                          metadata.cfName,
                                                                          metadata.ksName));
}
 
Example #21
Source File: CliClient.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Converts object represented as string into byte[] according to comparator
 * @param object - object to covert into byte array
 * @param comparator - comparator used to convert object
 * @return byte[] - object in the byte array representation
 */
private ByteBuffer getBytesAccordingToType(String object, AbstractType<?> comparator)
{
    if (comparator == null) // default comparator is BytesType
        comparator = BytesType.instance;

    try
    {
        return comparator.fromString(object);
    }
    catch (MarshalException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #22
Source File: SSTableImport.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a string to bytes (ByteBuffer) according to type
 * @param content string to convert
 * @param type type to use for conversion
 * @return byte buffer representation of the given string
 */
private static ByteBuffer stringAsType(String content, AbstractType<?> type)
{
    try
    {
        return type.fromString(content);
    }
    catch (MarshalException e)
    {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #23
Source File: SSTableIdentityIterator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public OnDiskAtom next()
{
    try
    {
        OnDiskAtom atom = atomIterator.next();
        if (validateColumns)
            atom.validateFields(columnFamily.metadata());
        return atom;
    }
    catch (MarshalException me)
    {
        throw new CorruptSSTableException(me, filename);
    }
}
 
Example #24
Source File: ColumnToCollectionType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source)
{
    try
    {
        return ByteBufferUtil.hexToBytes(source);
    }
    catch (NumberFormatException e)
    {
        throw new MarshalException(String.format("cannot parse '%s' as hex bytes", source), e);
    }
}
 
Example #25
Source File: CollectionType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void validateCellValue(ByteBuffer cellValue) throws MarshalException
{
    if (isMultiCell())
        valueComparator().validate(cellValue);
    else
        super.validateCellValue(cellValue);
}
 
Example #26
Source File: CollectionType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source)
{
    try
    {
        return ByteBufferUtil.hexToBytes(source);
    }
    catch (NumberFormatException e)
    {
        throw new MarshalException(String.format("cannot parse '%s' as hex bytes", source), e);
    }
}
 
Example #27
Source File: BooleanType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{

    if (source.isEmpty()|| source.equalsIgnoreCase(Boolean.FALSE.toString()))
        return decompose(false);

    if (source.equalsIgnoreCase(Boolean.TRUE.toString()))
        return decompose(true);

    throw new MarshalException(String.format("unable to make boolean from '%s'", source));
}
 
Example #28
Source File: NonTokenizingAnalyzer.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Override
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 || !(pipelineRes instanceof String))
                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;
}
 
Example #29
Source File: TimestampType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
  // Return an empty ByteBuffer for an empty string.
  if (source.isEmpty())
      return ByteBufferUtil.EMPTY_BYTE_BUFFER;

  return ByteBufferUtil.bytes(TimestampSerializer.dateStringToTimestamp(source));
}
 
Example #30
Source File: ColumnToCollectionType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void validateCollectionMember(ByteBuffer bytes, ByteBuffer collectionName) throws MarshalException
{
    CollectionType t = defined.get(collectionName);
    if (t == null)
        throw new MarshalException(ByteBufferUtil.bytesToHex(collectionName) + " is not defined as a collection");

    t.nameComparator().validate(bytes);
}