Elffuss BraidHub

kikocis/java-driver public

9b39ff6a48bf · 4.2 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMTestsSupport;
19import com.datastax.driver.core.SimpleStatement;
20import com.datastax.driver.core.Statement;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.PartitionKey;
23import com.datastax.driver.mapping.annotations.Table;
24import com.google.common.util.concurrent.AsyncFunction;
25import com.google.common.util.concurrent.Futures;
26import com.google.common.util.concurrent.ListenableFuture;
27import org.testng.annotations.BeforeMethod;
28import org.testng.annotations.Test;
29
30import java.util.Set;
31import java.util.concurrent.ConcurrentSkipListSet;
32
33import static org.assertj.core.api.Assertions.assertThat;
34
35/**
36 * Tests that a Result can be paged asynchronously.
37 *
38 * @jira_ticket JAVA-1157
39 */
40@SuppressWarnings("unused")
41@CassandraVersion(value = "2.0", description = "uses paging")
42public class MapperAsyncResultTest extends CCMTestsSupport {
43
44 @Override
45 public void onTestContextInitialized() {
46 execute("CREATE TABLE users (id int PRIMARY KEY, name text)");
47 }
48
49 @Table(name = "users")
50 static class User {
51
52 @PartitionKey
53 private int id;
54
55 private String name;
56
57 public int getId() {
58 return id;
59 }
60
61 public void setId(int id) {
62 this.id = id;
63 }
64
65 public String getName() {
66 return name;
67 }
68
69 public void setName(String name) {
70 this.name = name;
71 }
72
73 }
74
75 @BeforeMethod(groups = "short")
76 public void cleanup() {
77 session().execute("TRUNCATE users");
78 }
79
80 @Test(groups = "short")
81 public void should_iterate_single_page_result_set_asynchronously() {
82 should_iterate_result_set_asynchronously(100, 500);
83 }
84
85 @Test(groups = "short")
86 public void should_iterate_multi_page_result_set_asynchronously() {
87 should_iterate_result_set_asynchronously(1000, 20);
88 }
89
90 private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) {
91 for (int i = 0; i < totalCount; i++)
92 session().execute(String.format("INSERT INTO users (id, name) values (%d, '%s')", i, "user" + i));
93 Statement statement = new SimpleStatement("SELECT * FROM users").setFetchSize(fetchSize);
94 Mapper<User> mapper = new MappingManager(session()).mapper(User.class);
95 ResultsAccumulator accumulator = new ResultsAccumulator();
96 ListenableFuture<Result<User>> results = mapper.mapAsync(session().executeAsync(statement));
97 ListenableFuture<Result<User>> future = Futures.transform(
98 results,
99 accumulator);
100 Futures.getUnchecked(future);
101 assertThat(accumulator.all.size()).isEqualTo(totalCount);
102 }
103
104 /**
105 * Dummy transformation that accumulates all traversed results
106 */
107 static class ResultsAccumulator implements AsyncFunction<Result<User>, Result<User>> {
108
109 final Set<Integer> all = new ConcurrentSkipListSet<Integer>();
110
111 @Override
112 public ListenableFuture<Result<User>> apply(Result<User> users) throws Exception {
113 int remainingInPage = users.getAvailableWithoutFetching();
114 for (User user : users) {
115 all.add(user.getId());
116 if (--remainingInPage == 0)
117 break;
118 }
119 boolean wasLastPage = users.getExecutionInfo().getPagingState() == null;
120 if (wasLastPage)
121 return Futures.immediateFuture(users);
122 else
123 return Futures.transform(users.fetchMoreResults(), this);
124 }
125 }
126}