Java Code Examples for com.datastax.driver.core.ConsistencyLevel#ONE

The following examples show how to use com.datastax.driver.core.ConsistencyLevel#ONE . 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: StatementHandler.java    From scalardb with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link ConsistencyLevel} based on the specified {@link Operation} and {@link
 * Consistency}
 *
 * @param operation an {@code Operation}
 * @param consistency a {@code Consistency}
 * @return a {@code ConsistencyLevel}
 */
public static ConsistencyLevel convert(Operation operation, Consistency consistency) {
  switch (consistency) {
    case SEQUENTIAL:
      return ConsistencyLevel.QUORUM;
    case EVENTUAL:
      return ConsistencyLevel.ONE;
    case LINEARIZABLE:
      if (operation instanceof Selection) {
        // setConsistencyLevel() with SERIAL is only valid when an operation is read.
        // when an operation is write, it comes with conditional update and consistency level is
        // automatically set in overwriteConsistency().
        return ConsistencyLevel.SERIAL;
      } else {
        return ConsistencyLevel.QUORUM;
      }
    default:
      LOGGER.warn("Unsupported consistency is specified. SEQUENTIAL is being used instead.");
      return ConsistencyLevel.QUORUM;
  }
}
 
Example 2
Source File: CassandraQueryOptions.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
protected ConsistencyLevel getDefaultReadConsistencyLevel() {
    if (defaultReadConsistencyLevel == null) {
        if (readConsistencyLevel != null) {
            defaultReadConsistencyLevel = ConsistencyLevel.valueOf(readConsistencyLevel.toUpperCase());
        } else {
            defaultReadConsistencyLevel = ConsistencyLevel.ONE;
        }
    }
    return defaultReadConsistencyLevel;
}
 
Example 3
Source File: CassandraQueryOptions.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
protected ConsistencyLevel getDefaultWriteConsistencyLevel() {
    if (defaultWriteConsistencyLevel == null) {
        if (writeConsistencyLevel != null) {
            defaultWriteConsistencyLevel = ConsistencyLevel.valueOf(writeConsistencyLevel.toUpperCase());
        } else {
            defaultWriteConsistencyLevel = ConsistencyLevel.ONE;
        }
    }
    return defaultWriteConsistencyLevel;
}
 
Example 4
Source File: CassandraSessionImplTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
    Session session1 = mock(Session.class);
    Session session2 = mock(Session.class);
    when(session1.prepare(any(String.class))).thenReturn(preparedStatement1);
    when(session2.prepare(any(String.class))).thenReturn(preparedStatement2);

    ResultSetFuture rsFuture = mock(ResultSetFuture.class);
    ResultSet rs = mock(ResultSet.class);
    Iterator it = mock(Iterator.class);
    when(it.hasNext()).thenReturn(true);
    when(it.next()).thenReturn(mock(Row.class));
    when(rs.iterator()).thenReturn(it);
    when(rsFuture.getUninterruptibly()).thenReturn(rs);
    /* @formatter:off */
    when(session1.executeAsync(any(Statement.class)))
        .thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"))
        .thenThrow(new RuntimeException("this session should be refreshed / recreated"));
    when(session2.executeAsync(boundStatement1))
        .thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
    when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
    /* @formatter:on */

    Cluster cluster = mock(Cluster.class);
    when(cluster.connect()).thenReturn(session1).thenReturn(session2);
    when(session1.getCluster()).thenReturn(cluster);
    when(session2.getCluster()).thenReturn(cluster);

    Cluster.Builder builder = mock(Cluster.Builder.class);
    when(builder.build()).thenReturn(cluster);

    CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null,
            ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));

    BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        data.add(String.valueOf(i));
    }
    cassandraSession.execute(batchExecutionAssistant, data);

    verify(cluster, times(2)).connect();
    verify(session1, times(1)).prepare(any(String.class));
    verify(session2, times(1)).prepare(any(String.class));
    assertEquals(10, batchExecutionAssistant.processedCount());
}