Java源码示例:org.apache.cassandra.exceptions.InvalidRequestException

示例1
@Override
protected SecondaryIndexSearcher createSecondaryIndexSearcher(Set<ByteBuffer> columns)
{
    return new SecondaryIndexSearcher(baseCfs.indexManager, columns)
    {
        
        @Override
        public List<Row> search(ExtendedFilter filter)
        {
            return Arrays.asList(new Row(LAST_INDEXED_KEY, LAST_INDEXED_ROW));
        }

        @Override
        public void validate(IndexExpression indexExpression) throws InvalidRequestException
        {
            if (indexExpression.value.equals(ByteBufferUtil.bytes("invalid")))
                throw new InvalidRequestException("Invalid search!");
        }
        
    };
}
 
示例2
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);
}
 
示例3
public Map<Term, Operation> getColumns() throws InvalidRequestException
{
    // Created from an UPDATE
    if (columns != null)
        return columns;

    // Created from an INSERT

    // Don't hate, validate.
    if (columnNames.size() != columnValues.size())
        throw new InvalidRequestException("unmatched column names/values");
    if (columnNames.size() < 1)
        throw new InvalidRequestException("no columns specified for INSERT");

    columns = new HashMap<Term, Operation>();

    for (int i = 0; i < columnNames.size(); i++)
        columns.put(columnNames.get(i), new Operation(columnValues.get(i)));

    return columns;
}
 
示例4
public Value bind(QueryOptions options) throws InvalidRequestException
{
    Map<ByteBuffer, ByteBuffer> buffers = new TreeMap<ByteBuffer, ByteBuffer>(comparator);
    for (Map.Entry<Term, Term> entry : elements.entrySet())
    {
        // We don't support values > 64K because the serialization format encode the length as an unsigned short.
        ByteBuffer keyBytes = entry.getKey().bindAndGet(options);
        if (keyBytes == null)
            throw new InvalidRequestException("null is not supported inside collections");
        if (keyBytes.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)
            throw new InvalidRequestException(String.format("Map key is too long. Map keys are limited to %d bytes but %d bytes keys provided",
                                                            FBUtilities.MAX_UNSIGNED_SHORT,
                                                            keyBytes.remaining()));

        ByteBuffer valueBytes = entry.getValue().bindAndGet(options);
        if (valueBytes == null)
            throw new InvalidRequestException("null is not supported inside collections");
        if (valueBytes.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)
            throw new InvalidRequestException(String.format("Map value is too long. Map values are limited to %d bytes but %d bytes value provided",
                                                            FBUtilities.MAX_UNSIGNED_SHORT,
                                                            valueBytes.remaining()));

        buffers.put(keyBytes, valueBytes);
    }
    return new Value(buffers);
}
 
示例5
/**
 * Test 'clustering_0 IN (1, 2, 3)' with only one clustering column
 */
@Test
public void testBuildBoundWithOneInRestrictionsAndOneClusteringColumn() throws InvalidRequestException
{
    ByteBuffer value1 = ByteBufferUtil.bytes(1);
    ByteBuffer value2 = ByteBufferUtil.bytes(2);
    ByteBuffer value3 = ByteBufferUtil.bytes(3);
    SingleColumnRestriction.IN in = new SingleColumnRestriction.InWithValues(toTerms(value1, value2, value3));
    Restriction[] restrictions = new Restriction[] { in };

    List<Composite> bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(3, bounds.size());
    assertComposite(bounds.get(0), value1, EOC.START);
    assertComposite(bounds.get(1), value2, EOC.START);
    assertComposite(bounds.get(2), value3, EOC.START);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(3, bounds.size());
    assertComposite(bounds.get(0), value1, EOC.END);
    assertComposite(bounds.get(1), value2, EOC.END);
    assertComposite(bounds.get(2), value3, EOC.END);
}
 
示例6
public Mutation mutationForKey(ByteBuffer key, String keyspace, Long timestamp, ThriftClientState clientState, List<ByteBuffer> variables, CFMetaData metadata)
throws InvalidRequestException
{
    Mutation mutation = new Mutation(keyspace, key);

    QueryProcessor.validateKeyAlias(metadata, keyName);

    if (columns.size() < 1)
    {
        // No columns, delete the partition
        mutation.delete(columnFamily, (timestamp == null) ? getTimestamp(clientState) : timestamp);
    }
    else
    {
        // Delete specific columns
        AbstractType<?> at = metadata.comparator.asAbstractType();
        for (Term column : columns)
        {
            CellName columnName = metadata.comparator.cellFromByteBuffer(column.getByteBuffer(at, variables));
            validateColumnName(columnName);
            mutation.delete(columnFamily, columnName, (timestamp == null) ? getTimestamp(clientState) : timestamp);
        }
    }

    return mutation;
}
 
示例7
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof SetType))
    {
        // We've parsed empty maps as a set literal to break the ambiguity so
        // handle that case now
        if ((receiver.type instanceof MapType) && elements.isEmpty())
            return;

        throw new InvalidRequestException(String.format("Invalid set literal for %s of type %s", receiver.name, receiver.type.asCQL3Type()));
    }

    ColumnSpecification valueSpec = Sets.valueSpecOf(receiver);
    for (Term.Raw rt : elements)
    {
        if (!rt.isAssignableTo(keyspace, valueSpec))
            throw new InvalidRequestException(String.format("Invalid set literal for %s: value %s is not of type %s", receiver.name, rt, valueSpec.type.asCQL3Type()));
    }
}
 
示例8
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());
    }
}
 
示例9
public Value bind(QueryOptions options) throws InvalidRequestException
{
    SortedSet<ByteBuffer> buffers = new TreeSet<>(comparator);
    for (Term t : elements)
    {
        ByteBuffer bytes = t.bindAndGet(options);

        if (bytes == null)
            throw new InvalidRequestException("null is not supported inside collections");

        // We don't support value > 64K because the serialization format encode the length as an unsigned short.
        if (bytes.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)
            throw new InvalidRequestException(String.format("Set value is too long. Set values are limited to %d bytes but %d bytes value provided",
                                                            FBUtilities.MAX_UNSIGNED_SHORT,
                                                            bytes.remaining()));

        buffers.add(bytes);
    }
    return new Value(buffers);
}
 
示例10
/**
 * The column values must correspond to the order in which
 * they appear in the insert stored procedure. 
 * 
 * Key is not used, so it can be null or any object.
 * </p>
 *
 * @param key
 *            any object or null.
 * @param values
 *            the values to write.
 * @throws IOException
 */
@Override
public void write(Object key, List<ByteBuffer> values) throws IOException
{
    prepareWriter();
    try
    {
        ((CQLSSTableWriter) writer).rawAddRow(values);
        
        if (null != progress)
            progress.progress();
        if (null != context)
            HadoopCompat.progress(context);
    } 
    catch (InvalidRequestException e)
    {
        throw new IOException("Error adding row with key: " + key, e);
    }
}
 
示例11
private void validateForSinglePartition(AbstractType<?> keyValidator,
                                        UUID cfId,
                                        ByteBuffer key,
                                        Collection<Mutation> tmutations)
throws InvalidRequestException
{
    for (Mutation mutation : tmutations)
    {
        if (keyValidator.compare(mutation.key(), key) != 0)
            throw new InvalidRequestException("Partition key of additional mutation does not match primary update key");

        for (ColumnFamily cf : mutation.getColumnFamilies())
        {
            if (! cf.id().equals(cfId))
                throw new InvalidRequestException("Column family of additional mutation does not match primary update cf");
        }
    }
    validate(tmutations);
}
 
示例12
public Operation prepare(String keyspace, ColumnDefinition receiver) throws InvalidRequestException
{
    if (!(receiver.type.isCollection()))
        throw new InvalidRequestException(String.format("Invalid deletion operation for non collection column %s", receiver.name));
    else if (!(receiver.type.isMultiCell()))
        throw new InvalidRequestException(String.format("Invalid deletion operation for frozen collection column %s", receiver.name));

    switch (((CollectionType)receiver.type).kind)
    {
        case LIST:
            Term idx = element.prepare(keyspace, Lists.indexSpecOf(receiver));
            return new Lists.DiscarderByIndex(receiver, idx);
        case SET:
            Term elt = element.prepare(keyspace, Sets.valueSpecOf(receiver));
            return new Sets.ElementDiscarder(receiver, elt);
        case MAP:
            Term key = element.prepare(keyspace, Maps.keySpecOf(receiver));
            return new Maps.DiscarderByKey(receiver, key);
    }
    throw new AssertionError();
}
 
示例13
/**
 * Calls the <code>SelectStatement.buildBound</code> with the specified restrictions.
 *
 * @param restrictions the restrictions
 * @return the result from the method call to <code>SelectStatement.buildBound</code>
 * @throws InvalidRequestException if the method call throw an exception
 */
private static List<Composite> executeBuildBound(Restriction[] restrictions,
                                                 Bound bound) throws InvalidRequestException
{
    List<AbstractType<?>> types = new ArrayList<>();

    for (int i = 0, m = restrictions.length; i < m; i++)
        types.add(Int32Type.instance);

    CompoundSparseCellNameType cType = new CompoundSparseCellNameType(types);
    CFMetaData cfMetaData = new CFMetaData("keyspace", "test", ColumnFamilyType.Standard, cType);

    List<ColumnDefinition> columnDefs = new ArrayList<>();
    for (int i = 0, m = restrictions.length; i < m; i++)
    {
        ByteBuffer name = ByteBufferUtil.bytes("clustering_" + i);
        columnDefs.add(ColumnDefinition.clusteringKeyDef(cfMetaData, name, types.get(i), i));
    }

    return SelectStatement.buildBound(bound, columnDefs, restrictions, false, cType, QueryOptions.DEFAULT);
}
 
示例14
/**
 * Test '(clustering_0, clustering_1) = (1, 2)' with two clustering column
 */
@Test
public void testBuildBoundWithMultiEqRestrictions() throws InvalidRequestException
{
    ByteBuffer value1 = ByteBufferUtil.bytes(1);
    ByteBuffer value2 = ByteBufferUtil.bytes(2);
    MultiColumnRestriction.EQ eq = new MultiColumnRestriction.EQ(toMultiItemTerminal(value1, value2), false);
    Restriction[] restrictions = new Restriction[] { eq, eq };

    List<Composite> bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.START);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.END);
}
 
示例15
@Test
public void differentKeyRowMutations() throws ConfigurationException, InvalidRequestException
{
    CFMetaData metadata = makeCfMetaData("ks1", "cf1", TriggerDefinition.create("test", DifferentKeyTrigger.class.getName()));
    ColumnFamily cf = makeCf(metadata, "v1", null);
    Mutation rm = new Mutation(UTF8Type.instance.fromString("k1"), cf);

    List<? extends IMutation> tmutations = new ArrayList<>(TriggerExecutor.instance.execute(Arrays.asList(rm)));
    assertEquals(2, tmutations.size());
    Collections.sort(tmutations, new RmComparator());

    assertEquals(bytes("k1"), tmutations.get(0).key());
    assertEquals(bytes("otherKey"), tmutations.get(1).key());

    List<ColumnFamily> mutatedCFs = new ArrayList<>(tmutations.get(0).getColumnFamilies());
    assertEquals(1, mutatedCFs.size());
    assertEquals(bytes("v1"), mutatedCFs.get(0).getColumn(getColumnName(metadata, "c1")).value());
    assertNull(mutatedCFs.get(0).getColumn(getColumnName(metadata, "c2")));

    mutatedCFs = new ArrayList<>(tmutations.get(1).getColumnFamilies());
    assertEquals(1, mutatedCFs.size());
    assertNull(mutatedCFs.get(0).getColumn(getColumnName(metadata, "c1")));
    assertEquals(bytes("trigger"), mutatedCFs.get(0).getColumn(getColumnName(metadata, "c2")).value());
}
 
示例16
@Test
public void testInvalidSearch() throws IOException
{
    Mutation rm;
    rm = new Mutation("PerRowSecondaryIndex", ByteBufferUtil.bytes("k4"));
    rm.add("Indexed1", Util.cellname("indexed"), ByteBufferUtil.bytes("foo"), 1);
    rm.apply();
    
    // test we can search:
    UntypedResultSet result = QueryProcessor.executeInternal("SELECT * FROM \"PerRowSecondaryIndex\".\"Indexed1\" WHERE indexed = 'foo'");
    assertEquals(1, result.size());

    // test we can't search if the searcher doesn't validate the expression:
    try
    {
        QueryProcessor.executeInternal("SELECT * FROM \"PerRowSecondaryIndex\".\"Indexed1\" WHERE indexed = 'invalid'");
        fail("Query should have been invalid!");
    }
    catch (Exception e)
    {
        assertTrue(e instanceof InvalidRequestException || (e.getCause() != null && (e.getCause() instanceof InvalidRequestException)));
    }
}
 
示例17
public void execute(ByteBuffer rowKey, ColumnFamily cf, Composite prefix, UpdateParameters params) throws InvalidRequestException
{
    assert column.type.isMultiCell() : "Attempted to prepend to a frozen list";
    Term.Terminal value = t.bind(params.options);
    if (value == null)
        return;

    assert value instanceof Lists.Value;
    long time = PrecisionTime.REFERENCE_TIME - (System.currentTimeMillis() - PrecisionTime.REFERENCE_TIME);

    List<ByteBuffer> toAdd = ((Lists.Value)value).elements;
    for (int i = toAdd.size() - 1; i >= 0; i--)
    {
        PrecisionTime pt = PrecisionTime.getNext(time);
        ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(pt.millis, pt.nanos));
        cf.addColumn(params.makeColumn(cf.getComparator().create(prefix, column, uuid), toAdd.get(i)));
    }
}
 
示例18
/**
 * The <code>CqlParser</code> only goes as far as extracting the keyword arguments
 * from these statements, so this method is responsible for processing and
 * validating.
 *
 * @throws InvalidRequestException if arguments are missing or unacceptable
 */
public void validate(ClientState state) throws RequestValidationException
{
    ThriftValidation.validateKeyspaceNotSystem(name);

    // keyspace name
    if (!name.matches("\\w+"))
        throw new InvalidRequestException(String.format("\"%s\" is not a valid keyspace name", name));
    if (name.length() > Schema.NAME_LENGTH)
        throw new InvalidRequestException(String.format("Keyspace names shouldn't be more than %s characters long (got \"%s\")", Schema.NAME_LENGTH, name));

    attrs.validate();

    if (attrs.getReplicationStrategyClass() == null)
        throw new ConfigurationException("Missing mandatory replication strategy class");

    // The strategy is validated through KSMetaData.validate() in announceNewKeyspace below.
    // However, for backward compatibility with thrift, this doesn't validate unexpected options yet,
    // so doing proper validation here.
    AbstractReplicationStrategy.validateReplicationStrategy(name,
                                                            AbstractReplicationStrategy.getClass(attrs.getReplicationStrategyClass()),
                                                            StorageService.instance.getTokenMetadata(),
                                                            DatabaseDescriptor.getEndpointSnitch(),
                                                            attrs.getReplicationOptions());
}
 
示例19
public boolean isAssignableTo(String keyspace, ColumnSpecification receiver)
{
    try
    {
        validateAssignableTo(keyspace, receiver);
        return true;
    }
    catch (InvalidRequestException e)
    {
        return false;
    }
}
 
示例20
public int getTimeToLive(QueryOptions options) throws InvalidRequestException
{
    if (timeToLive == null)
        return 0;

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

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

    int ttl = Int32Type.instance.compose(tval);
    if (ttl < 0)
        throw new InvalidRequestException("A TTL must be greater or equal to 0");

    if (ttl > ExpiringCell.MAX_TTL)
        throw new InvalidRequestException(String.format("ttl is too large. requested (%d) maximum (%d)", ttl, ExpiringCell.MAX_TTL));

    return ttl;
}
 
示例21
static void doAppend(Term t, ColumnFamily cf, Composite prefix, ColumnDefinition column, UpdateParameters params) throws InvalidRequestException
{
    Term.Terminal value = t.bind(params.options);
    Lists.Value listValue = (Lists.Value)value;
    if (column.type.isMultiCell())
    {
        // If we append null, do nothing. Note that for Setter, we've
        // already removed the previous value so we're good here too
        if (value == null)
            return;

        List<ByteBuffer> toAdd = listValue.elements;
        for (int i = 0; i < toAdd.size(); i++)
        {
            ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
            cf.addColumn(params.makeColumn(cf.getComparator().create(prefix, column, uuid), toAdd.get(i)));
        }
    }
    else
    {
        // for frozen lists, we're overwriting the whole cell value
        CellName name = cf.getComparator().create(prefix, column);
        if (value == null)
            cf.addAtom(params.makeTombstone(name));
        else
            cf.addColumn(params.makeColumn(name, listValue.getWithProtocolVersion(Server.CURRENT_VERSION)));
    }
}
 
示例22
public boolean announceMigration(boolean isLocalOnly) throws ConfigurationException, InvalidRequestException
{
    CFMetaData cfm = Schema.instance.getCFMetaData(keyspace(), columnFamily()).copy();
    if (cfm.removeTrigger(triggerName))
    {
        logger.info("Dropping trigger with name {}", triggerName);
        MigrationManager.announceColumnFamilyUpdate(cfm, false, isLocalOnly);
        return true;
    }
    if (!ifExists)
        throw new InvalidRequestException(String.format("Trigger %s was not found", triggerName));
    return false;
}
 
示例23
public void execute(ByteBuffer rowKey, ColumnFamily cf, Composite prefix, UpdateParameters params) throws InvalidRequestException
{
    if (column.type.isMultiCell())
    {
        // delete + put
        CellName name = cf.getComparator().create(prefix, column);
        cf.addAtom(params.makeTombstoneForOverwrite(name.slice()));
    }
    Putter.doPut(t, cf, prefix, column, params);
}
 
示例24
private void assertInvalidAlterWithMessage(String createTableStatement, String errorMessage) throws Throwable
{
    try
    {
        alterTableMayThrow(createTableStatement);
        Assert.fail("Expected CREATE TABLE statement to error: " + createTableStatement);
    }
    catch (InvalidRequestException | ConfigurationException ex)
    {
        Assert.assertTrue("Expected error message to contain '" + errorMessage + "', but got '" + ex.getMessage() + "'",
                ex.getMessage().contains(errorMessage));
    }
}
 
示例25
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof MapType))
        throw new InvalidRequestException(String.format("Invalid map literal for %s of type %s", receiver.name, receiver.type.asCQL3Type()));

    ColumnSpecification keySpec = Maps.keySpecOf(receiver);
    ColumnSpecification valueSpec = Maps.valueSpecOf(receiver);
    for (Pair<Term.Raw, Term.Raw> entry : entries)
    {
        if (!entry.left.isAssignableTo(keyspace, keySpec))
            throw new InvalidRequestException(String.format("Invalid map literal for %s: key %s is not of type %s", receiver.name, entry.left, keySpec.type.asCQL3Type()));
        if (!entry.right.isAssignableTo(keyspace, valueSpec))
            throw new InvalidRequestException(String.format("Invalid map literal for %s: value %s is not of type %s", receiver.name, entry.right, valueSpec.type.asCQL3Type()));
    }
}
 
示例26
public static void validateComposite(Composite name, CType type) throws InvalidRequestException
{
    long serializedSize = type.serializer().serializedSize(name, TypeSizes.NATIVE);
    if (serializedSize > Cell.MAX_NAME_LENGTH)
        throw new InvalidRequestException(String.format("The sum of all clustering columns is too long (%s > %s)",
                                                        serializedSize,
                                                        Cell.MAX_NAME_LENGTH));
}
 
示例27
public ResultMessage process(String queryString, QueryState queryState, QueryOptions options)
throws RequestExecutionException, RequestValidationException
{
    ParsedStatement.Prepared p = getStatement(queryString, queryState.getClientState());
    options.prepare(p.boundNames);
    CQLStatement prepared = p.statement;
    if (prepared.getBoundTerms() != options.getValues().size())
        throw new InvalidRequestException("Invalid amount of bind variables");

    if (!queryState.getClientState().isInternal)
        metrics.regularStatementsExecuted.inc();

    return processStatement(prepared, queryState, options);
}
 
示例28
public void execute(ByteBuffer rowKey, ColumnFamily cf, Composite prefix, UpdateParameters params) throws InvalidRequestException
{
    CellName cname = cf.getComparator().create(prefix, column);
    if (column.type.isMultiCell())
        cf.addAtom(params.makeRangeTombstone(cname.slice()));
    else
        cf.addColumn(params.makeTombstone(cname));
}
 
示例29
private static void assertThrowsIRE(ColumnCondition.Bound bound, ByteBuffer conditionValue, ByteBuffer columnValue)
{
    try
    {
        isSatisfiedBy(bound, conditionValue, columnValue);
        fail("Expected InvalidRequestException was not thrown");
    } catch (InvalidRequestException e) { }
}
 
示例30
private ByteBuffer[] bindInternal(QueryOptions options) throws InvalidRequestException
{
    int version = options.getProtocolVersion();

    ByteBuffer[] buffers = new ByteBuffer[elements.size()];
    for (int i = 0; i < elements.size(); i++)
    {
        buffers[i] = elements.get(i).bindAndGet(options);
        // Inside tuples, we must force the serialization of collections to v3 whatever protocol
        // version is in use since we're going to store directly that serialized value.
        if (version < 3 && type.type(i).isCollection())
            buffers[i] = ((CollectionType)type.type(i)).getSerializer().reserializeToV3(buffers[i]);
    }
    return buffers;
}