Search in sources :

Example 6 with org.apache.cassandra.cql3.statements

use of org.apache.cassandra.cql3.statements in project cassandra by apache.

the class AuditLogManager method buildEntriesForBatch.

private static List<AuditLogEntry> buildEntriesForBatch(List<? extends CQLStatement> statements, List<String> queries, QueryState state, QueryOptions options, long queryStartTimeMillis) {
    List<AuditLogEntry> auditLogEntries = new ArrayList<>(statements.size() + 1);
    UUID batchId = UUID.randomUUID();
    String queryString = String.format("BatchId:[%s] - BATCH of [%d] statements", batchId, statements.size());
    AuditLogEntry entry = new AuditLogEntry.Builder(state).setOperation(queryString).setOptions(options).setTimestamp(queryStartTimeMillis).setBatch(batchId).setType(AuditLogEntryType.BATCH).build();
    auditLogEntries.add(entry);
    for (int i = 0; i < statements.size(); i++) {
        CQLStatement statement = statements.get(i);
        entry = new AuditLogEntry.Builder(state).setType(statement.getAuditLogContext().auditLogEntryType).setOperation(queries.get(i)).setTimestamp(queryStartTimeMillis).setScope(statement).setKeyspace(state, statement).setOptions(options).setBatch(batchId).build();
        auditLogEntries.add(entry);
    }
    return auditLogEntries;
}
Also used : CQLStatement(org.apache.cassandra.cql3.CQLStatement) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 7 with org.apache.cassandra.cql3.statements

use of org.apache.cassandra.cql3.statements in project cassandra by apache.

the class CassandraAuthorizer method executeLoggedBatch.

private void executeLoggedBatch(List<CQLStatement> statements) throws RequestExecutionException, RequestValidationException {
    BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED, VariableSpecifications.empty(), Lists.newArrayList(Iterables.filter(statements, ModificationStatement.class)), Attributes.none());
    processBatch(batch);
}
Also used : BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement)

Example 8 with org.apache.cassandra.cql3.statements

use of org.apache.cassandra.cql3.statements in project cassandra by apache.

the class UFIdentificationTest method testBatchStatement.

@Test
public void testBatchStatement() throws Throwable {
    String iFunc2 = createEchoFunction("int");
    List<ModificationStatement> statements = new ArrayList<>();
    statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (%s, 0, 'foo')", functionCall(iFunc, "0"))));
    statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (1, %s, 'foo')", functionCall(iFunc2, "1"))));
    statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (2, 2, %s)", functionCall(tFunc, "'foo'"))));
    BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED, VariableSpecifications.empty(), statements, Attributes.none());
    assertFunctions(batch, iFunc, iFunc2, tFunc);
}
Also used : ModificationStatement(org.apache.cassandra.cql3.statements.ModificationStatement) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) Test(org.junit.Test)

Example 9 with org.apache.cassandra.cql3.statements

use of org.apache.cassandra.cql3.statements in project cassandra by apache.

the class UFTest method testFunctionDropPreparedStatement.

@Test
public void testFunctionDropPreparedStatement() throws Throwable {
    createTable("CREATE TABLE %s (key int PRIMARY KEY, d double)");
    String fSin = createFunction(KEYSPACE_PER_TEST, "double", "CREATE FUNCTION %s ( input double ) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE java " + "AS 'return Double.valueOf(Math.sin(input.doubleValue()));'");
    FunctionName fSinName = parseFunctionName(fSin);
    Assert.assertEquals(1, Schema.instance.getFunctions(parseFunctionName(fSin)).size());
    // create a pairs of Select and Inserts. One statement in each pair uses the function so when we
    // drop it those statements should be removed from the cache in QueryProcessor. The other statements
    // should be unaffected.
    ResultMessage.Prepared preparedSelect1 = QueryProcessor.instance.prepare(String.format("SELECT key, %s(d) FROM %s.%s", fSin, KEYSPACE, currentTable()), ClientState.forInternalCalls());
    ResultMessage.Prepared preparedSelect2 = QueryProcessor.instance.prepare(String.format("SELECT key FROM %s.%s", KEYSPACE, currentTable()), ClientState.forInternalCalls());
    ResultMessage.Prepared preparedInsert1 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, %s(?))", KEYSPACE, currentTable(), fSin), ClientState.forInternalCalls());
    ResultMessage.Prepared preparedInsert2 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, ?)", KEYSPACE, currentTable()), ClientState.forInternalCalls());
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
    execute("DROP FUNCTION " + fSin + "(double);");
    // the statements which use the dropped function should be removed from cache, with the others remaining
    Assert.assertNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
    Assert.assertNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
    execute("CREATE FUNCTION " + fSin + " ( input double ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE java " + "AS 'return Double.valueOf(Math.sin(input));'");
    Assert.assertEquals(1, Schema.instance.getFunctions(fSinName).size());
    preparedSelect1 = QueryProcessor.instance.prepare(String.format("SELECT key, %s(d) FROM %s.%s", fSin, KEYSPACE, currentTable()), ClientState.forInternalCalls());
    preparedInsert1 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, %s(?))", KEYSPACE, currentTable(), fSin), ClientState.forInternalCalls());
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
    dropPerTestKeyspace();
    // again, only the 2 statements referencing the function should be removed from cache
    // this time because the statements select from tables in KEYSPACE, only the function
    // is scoped to KEYSPACE_PER_TEST
    Assert.assertNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
    Assert.assertNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) Test(org.junit.Test)

Example 10 with org.apache.cassandra.cql3.statements

use of org.apache.cassandra.cql3.statements in project cassandra by apache.

the class FQLReplayTest method testFQLQueryBatchToStatement.

@Test
public void testFQLQueryBatchToStatement() {
    List<List<ByteBuffer>> values = new ArrayList<>();
    List<String> queries = new ArrayList<>();
    for (int bqCount = 0; bqCount < 10; bqCount++) {
        queries.add("select * from asdf where x = ? and y = " + bqCount);
        List<ByteBuffer> queryValues = new ArrayList<>();
        for (int i = 0; i < 10; i++) queryValues.add(ByteBufferUtil.bytes(i + ":" + bqCount));
        values.add(queryValues);
    }
    FQLQuery.Batch batch = new FQLQuery.Batch("xyz", QueryOptions.DEFAULT.getProtocolVersion().asInt(), QueryOptions.DEFAULT, 1234, 12345, 54321, com.datastax.driver.core.BatchStatement.Type.UNLOGGED, queries, values);
    Statement stmt = batch.toStatement();
    assertEquals(stmt.getDefaultTimestamp(), 12345);
    assertTrue(stmt instanceof com.datastax.driver.core.BatchStatement);
    com.datastax.driver.core.BatchStatement batchStmt = (com.datastax.driver.core.BatchStatement) stmt;
    List<Statement> statements = Lists.newArrayList(batchStmt.getStatements());
    List<Statement> fromFQLQueries = batch.queries.stream().map(FQLQuery.Single::toStatement).collect(Collectors.toList());
    assertEquals(statements.size(), fromFQLQueries.size());
    assertEquals(12345, batchStmt.getDefaultTimestamp());
    for (int i = 0; i < statements.size(); i++) compareStatements(statements.get(i), fromFQLQueries.get(i));
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) Statement(com.datastax.driver.core.Statement) ArrayList(java.util.ArrayList) ParsedTargetHost.fromString(org.apache.cassandra.fqltool.QueryReplayer.ParsedTargetHost.fromString) ByteBuffer(java.nio.ByteBuffer) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

BatchStatement (org.apache.cassandra.cql3.statements.BatchStatement)8 ArrayList (java.util.ArrayList)6 ModificationStatement (org.apache.cassandra.cql3.statements.ModificationStatement)6 Test (org.junit.Test)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)4 ByteBuffer (java.nio.ByteBuffer)3 UUID (java.util.UUID)2 CQLStatement (org.apache.cassandra.cql3.CQLStatement)2 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)2 PreparedQueryNotFoundException (org.apache.cassandra.exceptions.PreparedQueryNotFoundException)2 ClientState (org.apache.cassandra.service.ClientState)2 MD5Digest (org.apache.cassandra.utils.MD5Digest)2 SimpleStatement (com.datastax.driver.core.SimpleStatement)1 Statement (com.datastax.driver.core.Statement)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 List (java.util.List)1 AssignmentTestable (org.apache.cassandra.cql3.AssignmentTestable)1 BatchQueryOptions (org.apache.cassandra.cql3.BatchQueryOptions)1 QueryHandler (org.apache.cassandra.cql3.QueryHandler)1 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)1