Skip to content

Commit

Permalink
Add method to update consistency level in query options (apache#525)
Browse files Browse the repository at this point in the history
Adds public API to update consistency level in query options.
Adds a test and fixes name of BatchTest, so it is run in CI.

Co-authored-by: sergio <sergio.bossa@gmail.com>
(cherry picked from commit c71af49)
(cherry picked from commit 95e69bd)
  • Loading branch information
k-rus authored and djatnieks committed Jul 24, 2023
1 parent de19f5b commit c0eb9ef
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/java/org/apache/cassandra/cql3/BatchQueryOptions.java
Expand Up @@ -59,6 +59,11 @@ public void prepareStatement(int i, List<ColumnSpecification> boundNames)
forStatement(i).prepare(boundNames);
}

public void updateConsistency(ConsistencyLevel updatedLevel)
{
wrapped.updateConsistency(updatedLevel);
}

public ConsistencyLevel getConsistency()
{
return wrapped.getConsistency();
Expand Down
14 changes: 13 additions & 1 deletion src/java/org/apache/cassandra/cql3/QueryOptions.java
Expand Up @@ -104,6 +104,7 @@ public static QueryOptions addColumnSpecifications(QueryOptions options, List<Co
return new OptionsWithColumnSpecifications(options, columnSpecs);
}

public abstract void updateConsistency(ConsistencyLevel updatedLevel);
public abstract ConsistencyLevel getConsistency();
public abstract List<ByteBuffer> getValues();
public abstract boolean skipMetadata();
Expand Down Expand Up @@ -224,7 +225,7 @@ public QueryOptions prepare(List<ColumnSpecification> specs)

static class DefaultQueryOptions extends QueryOptions
{
private final ConsistencyLevel consistency;
private volatile ConsistencyLevel consistency;
private final List<ByteBuffer> values;
private final boolean skipMetadata;

Expand All @@ -241,6 +242,12 @@ static class DefaultQueryOptions extends QueryOptions
this.protocolVersion = protocolVersion;
}

@Override
public void updateConsistency(ConsistencyLevel updatedLevel)
{
consistency = updatedLevel;
}

public ConsistencyLevel getConsistency()
{
return consistency;
Expand Down Expand Up @@ -281,6 +288,11 @@ public List<ByteBuffer> getValues()
return this.wrapped.getValues();
}

public void updateConsistency(ConsistencyLevel updatedLevel)
{
wrapped.updateConsistency(updatedLevel);
}

public ConsistencyLevel getConsistency()
{
return wrapped.getConsistency();
Expand Down
16 changes: 15 additions & 1 deletion test/unit/org/apache/cassandra/cql3/BatchTest.java
Expand Up @@ -24,16 +24,20 @@
import com.datastax.driver.core.exceptions.InvalidQueryException;
import org.apache.cassandra.ServerTestUtils;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.ConsistencyLevel;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.service.EmbeddedCassandraService;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertSame;

import java.io.IOException;

public class BatchTests extends CQLTester
public class BatchTest extends CQLTester
{
private static EmbeddedCassandraService cassandra;

Expand Down Expand Up @@ -159,6 +163,7 @@ public void testCounterInLoggedBatch()
sendBatch(BatchStatement.Type.LOGGED, true, false, false);
}

@Ignore
@Test(expected = InvalidQueryException.class)
public void testOversizedBatch()
{
Expand All @@ -171,6 +176,15 @@ public void testOversizedBatch()
session.execute(b);
}

@Test
public void testQueryOptionConsistency()
{
BatchQueryOptions queryOptions = BatchQueryOptions.withoutPerStatementVariables(QueryOptions.DEFAULT);
assertSame(ConsistencyLevel.ONE, queryOptions.getConsistency());
queryOptions.updateConsistency(ConsistencyLevel.ALL);
assertSame(ConsistencyLevel.ALL, queryOptions.getConsistency());
}

public void sendBatch(BatchStatement.Type type, boolean addCounter, boolean addNonCounter, boolean addClustering)
{

Expand Down
@@ -0,0 +1,21 @@
--- a/test/unit/org/apache/cassandra/cql3/BatchTest.java
+++ b/test/unit/org/apache/cassandra/cql3/BatchTest.java
@@ -33,15 +33,11 @@
import org.junit.Ignore;
import org.junit.Test;

-import java.io.IOException;
-
-<<<<<<<
import static org.junit.Assert.assertSame;

-public class BatchTest
-=======
-public class BatchTests extends CQLTester
->>>>>>>
+import java.io.IOException;
+
+public class BatchTest extends CQLTester
{
private static EmbeddedCassandraService cassandra;

0 comments on commit c0eb9ef

Please sign in to comment.