@Test(groups = "short") public void multipageResultSetTest() { session().execute("TRUNCATE test"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); // This is really contrived, we just want to cover the code path in // ArrayBackedResultSet#MultiPage. // Currently CAS update results are never multipage, so it's hard to come up with a meaningful // example. ResultSet rs = session().execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); assertTrue(rs.wasApplied()); }
@Test(groups = "short") @CassandraVersion(value = "2.0.0") public void should_reuse_wrapped_simple_statement_for_multipage_query() { loadBalancingPolicy.customStatementsHandled.set(0); for (int v = 1; v <= 100; v++) session().execute(new SimpleStatement(INSERT_MULTIPAGE_QUERY, "key_simple_multipage", v)); SimpleStatement s = new SimpleStatement(SELECT_MULTIPAGE_QUERY, "key_simple_multipage"); s.setFetchSize(1); CustomStatement customStatement = new CustomStatement(s); ResultSet rs = session().execute(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); Iterator<Row> it = rs.iterator(); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(2); }
private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) { for (int i = 0; i < totalCount; i++) session().execute(String.format("insert into ints (i) values (%d)", i)); Statement statement = new SimpleStatement("select * from ints").setFetchSize(fetchSize); ResultsAccumulator results = new ResultsAccumulator(); ListenableFuture<ResultSet> future = GuavaCompatibility.INSTANCE.transformAsync(session().executeAsync(statement), results); Futures.getUnchecked(future); assertThat(results.all.size()).isEqualTo(totalCount); }
/** * Validates that the "unsafe" paging state can be reused with the same Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided raw paging state starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_complete_when_using_unsafe_paging_state() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session().execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
/** * Validates that {@link PagingState} can be reused with the same Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided {@link PagingState} starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_complete_when_using_paging_state() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session() .execute( st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
/** * Validates if all results of a query are paged in through a queries result set that the {@link * PagingState} it returns will return an empty set when queried with. * * @test_category paging * @expected_result Query with the {@link PagingState} returns 0 rows. */ @Test(groups = "short") public void should_return_no_rows_when_paged_to_end() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); // Consume enough of the iterator to cause all the results to be paged in. Iterator<Row> rowIt = result.iterator(); for (int i = 0; i < 83; i++) { rowIt.next().getInt("v"); } String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session() .execute( st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); assertThat(result.one()).isNull(); }
st.setFetchSize(5); // Ridiculously small fetch size for testing purpose. Don't do at home. ResultSet rs = session().execute(st);
ResultSet result = session().execute(st.setFetchSize(20)); st.setFetchSize(20).setPagingState(PagingState.fromBytes(savedPagingStateBuffer)); } catch (PagingStateException e) { setWithFalseContent = true; st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString)); } catch (PagingStateException e) { setWithWrongStatement = true;
/** * Validates that for each page the {@link ExecutionInfo} will have a different tracing ID. * * @test_category tracing * @expected_result {@link ResultSet} where all the {@link ExecutionInfo} will contains a * different tracing ID and that the events can be retrieved for the last query. */ @Test(groups = "short") public void should_have_a_different_tracingId_for_each_page() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(40).enableTracing()); result.all(); // sleep 10 seconds to make sure the trace will be complete Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); List<ExecutionInfo> executions = result.getAllExecutionInfo(); UUID previousTraceId = null; for (ExecutionInfo executionInfo : executions) { QueryTrace queryTrace = executionInfo.getQueryTrace(); assertThat(queryTrace).isNotNull(); assertThat(queryTrace.getTraceId()).isNotEqualTo(previousTraceId); previousTraceId = queryTrace.getTraceId(); } assertThat(result.getExecutionInfo().getQueryTrace().getEvents()).isNotNull().isNotEmpty(); }
private SimpleStatement getStatement(final String cql, final Integer fetchSize, final Object... values) { SimpleStatement statement = values != null && values.length > 0 ? new SimpleStatement(cql, values) : new SimpleStatement(cql); if (fetchSize != null) statement.setFetchSize(fetchSize); return statement; }
@Test(groups = "short") @CassandraVersion(value = "2.0.0") public void should_reuse_wrapped_simple_statement_for_multipage_query() { loadBalancingPolicy.customStatementsHandled.set(0); for (int v = 1; v <= 100; v++) session().execute(new SimpleStatement(INSERT_MULTIPAGE_QUERY, "key_simple_multipage", v)); SimpleStatement s = new SimpleStatement(SELECT_MULTIPAGE_QUERY, "key_simple_multipage"); s.setFetchSize(1); CustomStatement customStatement = new CustomStatement(s); ResultSet rs = session().execute(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); Iterator<Row> it = rs.iterator(); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(2); }
@Test(groups = "short") public void multipageResultSetTest() { session().execute("TRUNCATE test"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); // This is really contrived, we just want to cover the code path in // ArrayBackedResultSet#MultiPage. // Currently CAS update results are never multipage, so it's hard to come up with a meaningful // example. ResultSet rs = session().execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); assertTrue(rs.wasApplied()); }
private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) { for (int i = 0; i < totalCount; i++) session().execute(String.format("insert into ints (i) values (%d)", i)); Statement statement = new SimpleStatement("select * from ints").setFetchSize(fetchSize); ResultsAccumulator results = new ResultsAccumulator(); ListenableFuture<ResultSet> future = GuavaCompatibility.INSTANCE.transformAsync(session().executeAsync(statement), results); Futures.getUnchecked(future); assertThat(results.all.size()).isEqualTo(totalCount); }
/** * Validates that the "unsafe" paging state can be reused with the same Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided raw paging state starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_complete_when_using_unsafe_paging_state() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session().execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
stmt.setFetchSize(fetchSize); try { if (nextPageState != null) {
stmt.setFetchSize(fetchSize); try { if (nextPageState != null) {
/** * Validates that {@link PagingState} can be reused with the same Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided {@link PagingState} starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_complete_when_using_paging_state() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session() .execute( st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
st.setFetchSize(5); // Ridiculously small fetch size for testing purpose. Don't do at home. ResultSet rs = session().execute(st);
/** * Validates if all results of a query are paged in through a queries result set that the {@link * PagingState} it returns will return an empty set when queried with. * * @test_category paging * @expected_result Query with the {@link PagingState} returns 0 rows. */ @Test(groups = "short") public void should_return_no_rows_when_paged_to_end() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); // Consume enough of the iterator to cause all the results to be paged in. Iterator<Row> rowIt = result.iterator(); for (int i = 0; i < 83; i++) { rowIt.next().getInt("v"); } String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session() .execute( st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); assertThat(result.one()).isNull(); }
/** * Validates that for each page the {@link ExecutionInfo} will have a different tracing ID. * * @test_category tracing * @expected_result {@link ResultSet} where all the {@link ExecutionInfo} will contains a * different tracing ID and that the events can be retrieved for the last query. */ @Test(groups = "short") public void should_have_a_different_tracingId_for_each_page() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(40).enableTracing()); result.all(); // sleep 10 seconds to make sure the trace will be complete Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); List<ExecutionInfo> executions = result.getAllExecutionInfo(); UUID previousTraceId = null; for (ExecutionInfo executionInfo : executions) { QueryTrace queryTrace = executionInfo.getQueryTrace(); assertThat(queryTrace).isNotNull(); assertThat(queryTrace.getTraceId()).isNotEqualTo(previousTraceId); previousTraceId = queryTrace.getTraceId(); } assertThat(result.getExecutionInfo().getQueryTrace().getEvents()).isNotNull().isNotEmpty(); }