| 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 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | import com.datastax.driver.mapping.annotations.*; |
| 21 | import org.testng.annotations.BeforeMethod; |
| 22 | import org.testng.annotations.Test; |
| 23 | |
| 24 | import java.util.Iterator; |
| 25 | |
| 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | |
| 28 | @SuppressWarnings("unused") |
| 29 | @CassandraVersion("3.0") |
| 30 | public class MapperMaterializedViewTest extends CCMTestsSupport { |
| 31 | |
| 32 | @Override |
| 33 | public void onTestContextInitialized() { |
| 34 | // Example schema taken from: http://www.datastax.com/dev/blog/new-in-cassandra-3-0-materialized-views |
| 35 | execute( |
| 36 | "CREATE TABLE scores (user TEXT, game TEXT, year INT, month INT, day INT, score INT, PRIMARY KEY(user, game, year, month, day))", |
| 37 | "CREATE MATERIALIZED VIEW alltimehigh AS\n" |
| 38 | + " SELECT * FROM scores\n" |
| 39 | + " WHERE game IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL\n" |
| 40 | + " PRIMARY KEY (game, score, user, year, month, day)\n" |
| 41 | + " WITH CLUSTERING ORDER BY (score desc)", |
| 42 | "CREATE MATERIALIZED VIEW dailyhigh AS\n" |
| 43 | + " SELECT * FROM scores\n" |
| 44 | + " WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL\n" |
| 45 | + " PRIMARY KEY ((game, year, month, day), score, user)\n" |
| 46 | + " WITH CLUSTERING ORDER BY (score DESC)", |
| 47 | "CREATE MATERIALIZED VIEW monthlyhigh AS\n" |
| 48 | + " SELECT * FROM scores\n" |
| 49 | + " WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND day IS NOT NULL\n" |
| 50 | + " PRIMARY KEY ((game, year, month), score, user, day)\n" |
| 51 | + " WITH CLUSTERING ORDER BY (score DESC)", |
| 52 | "CREATE MATERIALIZED VIEW filtereduserhigh AS\n" |
| 53 | + " SELECT * FROM scores\n" |
| 54 | + " WHERE user in ('jbellis', 'pcmanus') AND game IS NOT NULL AND score IS NOT NULL AND year is NOT NULL AND day is not NULL and month IS NOT NULL\n" |
| 55 | + " PRIMARY KEY (game, score, user, year, month, day)\n" |
| 56 | + " WITH CLUSTERING ORDER BY (score DESC)", |
| 57 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 5, 1, 4000)", |
| 58 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 5, 3, 1750)", |
| 59 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('yukim', 'Coup', 2015, 5, 3, 2250)", |
| 60 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 5, 3, 500)", |
| 61 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('iamaleksey', 'Coup', 2015, 6, 1, 2500)", |
| 62 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 6, 2, 1000)", |
| 63 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 6, 2, 2000)", |
| 64 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jmckenzie', 'Coup', 2015, 6, 9, 2700)", |
| 65 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 6, 20, 3500)", |
| 66 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Checkers', 2015, 6, 20, 1200)", |
| 67 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Chess', 2015, 6, 21, 3500)", |
| 68 | "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Chess', 2015, 1, 25, 3200)" |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | private Mapper<AllTimeHigh> mapper; |
| 74 | |
| 75 | private ScoreAccessor accessor; |
| 76 | |
| 77 | @BeforeMethod(groups = "short") |
| 78 | public void setUp() { |
| 79 | MappingManager mappingManager = new MappingManager(session()); |
| 80 | mapper = mappingManager.mapper(AllTimeHigh.class); |
| 81 | accessor = mappingManager.createAccessor(ScoreAccessor.class); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Validates that a materialized view can be mapped as a regular table. |
| 86 | * |
| 87 | * @test_category materialized_view, mapper |
| 88 | * @jira_ticket JAVA-1258 |
| 89 | */ |
| 90 | @Test(groups = "short") |
| 91 | public void should_access_mapped_materialized_view() { |
| 92 | AllTimeHigh allTimeHigh = mapper.get("Coup", 4000, "pcmanus", 2015, 5, 1); |
| 93 | assertThat(allTimeHigh).isNotNull(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Validates that an accessor properly maps a single entity over a result from a materialized |
| 98 | * view. |
| 99 | * |
| 100 | * @test_category materialized_view, mapper |
| 101 | */ |
| 102 | @Test(groups = "short") |
| 103 | public void should_access_single_entity() { |
| 104 | Score score = accessor.allTimeHigh("Coup"); |
| 105 | |
| 106 | assertThat(score).isEqualTo(new Score("pcmanus", "Coup", 2015, 5, 1, 4000)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Validates that an accessor properly maps to a Result from a materialized |
| 111 | * view. |
| 112 | * |
| 113 | * @test_category materialized_view, mapper |
| 114 | */ |
| 115 | @Test(groups = "short") |
| 116 | public void should_access_mapped_result() { |
| 117 | Result<Score> scores = accessor.dailyHigh("Coup", 2015, 6, 2); |
| 118 | |
| 119 | Iterator<Score> iterator = scores.iterator(); |
| 120 | assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Coup", 2015, 6, 2, 2000)); |
| 121 | assertThat(iterator.next()).isEqualTo(new Score("tjake", "Coup", 2015, 6, 2, 1000)); |
| 122 | assertThat(scores.isExhausted()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Validates that an accessor properly maps to a Result from a materialized |
| 127 | * view given a range criteria query. |
| 128 | * |
| 129 | * @test_category materialized_view, mapper |
| 130 | */ |
| 131 | @Test(groups = "short") |
| 132 | public void should_access_mapped_result_range() { |
| 133 | Result<Score> scores = accessor.monthlyHighRange("Coup", 2015, 6, 2500, 3500); |
| 134 | |
| 135 | Iterator<Score> iterator = scores.iterator(); |
| 136 | assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Coup", 2015, 6, 20, 3500)); |
| 137 | assertThat(iterator.next()).isEqualTo(new Score("jmckenzie", "Coup", 2015, 6, 9, 2700)); |
| 138 | assertThat(iterator.next()).isEqualTo(new Score("iamaleksey", "Coup", 2015, 6, 1, 2500)); |
| 139 | assertThat(scores.isExhausted()); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Validates that an accessor properly maps to a Result from a materialized |
| 144 | * view that is filtered on a primary key. |
| 145 | * |
| 146 | * @test_category materialized_view, mapper |
| 147 | */ |
| 148 | @Test(groups = "short") |
| 149 | public void should_access_filtered_user_high() { |
| 150 | Result<Score> scores = accessor.filteredUserHigh("Chess"); |
| 151 | |
| 152 | Iterator<Score> iterator = scores.iterator(); |
| 153 | assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Chess", 2015, 6, 21, 3500)); |
| 154 | assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Chess", 2015, 1, 25, 3200)); |
| 155 | assertThat(scores.isExhausted()); |
| 156 | } |
| 157 | |
| 158 | @Accessor |
| 159 | public interface ScoreAccessor { |
| 160 | @Query("select * from alltimehigh where game=:game") |
| 161 | Score allTimeHigh(@Param("game") String game); |
| 162 | |
| 163 | @Query("select * from monthlyhigh where game=? and year=? and month=? and score >= ? and score <= ?") |
| 164 | Result<Score> monthlyHighRange(String game, int year, int month, int lowScore, int highScore); |
| 165 | |
| 166 | @Query("select * from dailyhigh where game=? and year=? and month=? and day=?") |
| 167 | Result<Score> dailyHigh(String game, int year, int month, int day); |
| 168 | |
| 169 | @Query("select * from filtereduserhigh where game=:game") |
| 170 | Result<Score> filteredUserHigh(String game); |
| 171 | } |
| 172 | |
| 173 | @Table(name = "scores") |
| 174 | public static class Score { |
| 175 | |
| 176 | //primary key: user, game, year, month, day |
| 177 | |
| 178 | @PartitionKey |
| 179 | String user; |
| 180 | |
| 181 | @ClusteringColumn(0) |
| 182 | String game; |
| 183 | |
| 184 | @ClusteringColumn(1) |
| 185 | int year; |
| 186 | |
| 187 | @ClusteringColumn(2) |
| 188 | int month; |
| 189 | |
| 190 | @ClusteringColumn(3) |
| 191 | int day; |
| 192 | |
| 193 | int score; |
| 194 | |
| 195 | public Score() { |
| 196 | } |
| 197 | |
| 198 | public Score(String user, String game, int year, int month, int day, int score) { |
| 199 | this.user = user; |
| 200 | this.game = game; |
| 201 | this.year = year; |
| 202 | this.month = month; |
| 203 | this.day = day; |
| 204 | this.score = score; |
| 205 | } |
| 206 | |
| 207 | public String getUser() { |
| 208 | return user; |
| 209 | } |
| 210 | |
| 211 | public void setUser(String user) { |
| 212 | this.user = user; |
| 213 | } |
| 214 | |
| 215 | public String getGame() { |
| 216 | return game; |
| 217 | } |
| 218 | |
| 219 | public void setGame(String game) { |
| 220 | this.game = game; |
| 221 | } |
| 222 | |
| 223 | public int getYear() { |
| 224 | return year; |
| 225 | } |
| 226 | |
| 227 | public void setYear(int year) { |
| 228 | this.year = year; |
| 229 | } |
| 230 | |
| 231 | public int getMonth() { |
| 232 | return month; |
| 233 | } |
| 234 | |
| 235 | public void setMonth(int month) { |
| 236 | this.month = month; |
| 237 | } |
| 238 | |
| 239 | public int getDay() { |
| 240 | return day; |
| 241 | } |
| 242 | |
| 243 | public void setDay(int day) { |
| 244 | this.day = day; |
| 245 | } |
| 246 | |
| 247 | public int getScore() { |
| 248 | return score; |
| 249 | } |
| 250 | |
| 251 | public void setScore(int score) { |
| 252 | this.score = score; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public String toString() { |
| 257 | return "Score{" + |
| 258 | "user='" + user + '\'' + |
| 259 | ", game='" + game + '\'' + |
| 260 | ", year=" + year + |
| 261 | ", month=" + month + |
| 262 | ", day=" + day + |
| 263 | ", score=" + score + |
| 264 | '}'; |
| 265 | } |
| 266 | |
| 267 | @SuppressWarnings("SimplifiableIfStatement") |
| 268 | @Override |
| 269 | public boolean equals(Object o) { |
| 270 | if (this == o) |
| 271 | return true; |
| 272 | if (!(o instanceof Score)) |
| 273 | return false; |
| 274 | |
| 275 | Score score1 = (Score) o; |
| 276 | |
| 277 | if (year != score1.year) |
| 278 | return false; |
| 279 | if (month != score1.month) |
| 280 | return false; |
| 281 | if (day != score1.day) |
| 282 | return false; |
| 283 | if (score != score1.score) |
| 284 | return false; |
| 285 | if (!user.equals(score1.user)) |
| 286 | return false; |
| 287 | return game.equals(score1.game); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public int hashCode() { |
| 292 | int result = user.hashCode(); |
| 293 | result = 31 * result + game.hashCode(); |
| 294 | result = 31 * result + year; |
| 295 | result = 31 * result + month; |
| 296 | result = 31 * result + day; |
| 297 | result = 31 * result + score; |
| 298 | return result; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | @Table(name = "alltimehigh") |
| 303 | public static class AllTimeHigh { |
| 304 | |
| 305 | //primary key: game, score, user, year, month, day |
| 306 | |
| 307 | @PartitionKey |
| 308 | String game; |
| 309 | |
| 310 | @ClusteringColumn(0) |
| 311 | int score; |
| 312 | |
| 313 | @ClusteringColumn(1) |
| 314 | String user; |
| 315 | |
| 316 | @ClusteringColumn(2) |
| 317 | int year; |
| 318 | |
| 319 | @ClusteringColumn(3) |
| 320 | int month; |
| 321 | |
| 322 | @ClusteringColumn(4) |
| 323 | int day; |
| 324 | |
| 325 | public AllTimeHigh() { |
| 326 | } |
| 327 | |
| 328 | public AllTimeHigh(String user, String game, int year, int month, int day, int score) { |
| 329 | this.user = user; |
| 330 | this.game = game; |
| 331 | this.year = year; |
| 332 | this.month = month; |
| 333 | this.day = day; |
| 334 | this.score = score; |
| 335 | } |
| 336 | |
| 337 | public String getUser() { |
| 338 | return user; |
| 339 | } |
| 340 | |
| 341 | public void setUser(String user) { |
| 342 | this.user = user; |
| 343 | } |
| 344 | |
| 345 | public String getGame() { |
| 346 | return game; |
| 347 | } |
| 348 | |
| 349 | public void setGame(String game) { |
| 350 | this.game = game; |
| 351 | } |
| 352 | |
| 353 | public int getYear() { |
| 354 | return year; |
| 355 | } |
| 356 | |
| 357 | public void setYear(int year) { |
| 358 | this.year = year; |
| 359 | } |
| 360 | |
| 361 | public int getMonth() { |
| 362 | return month; |
| 363 | } |
| 364 | |
| 365 | public void setMonth(int month) { |
| 366 | this.month = month; |
| 367 | } |
| 368 | |
| 369 | public int getDay() { |
| 370 | return day; |
| 371 | } |
| 372 | |
| 373 | public void setDay(int day) { |
| 374 | this.day = day; |
| 375 | } |
| 376 | |
| 377 | public int getScore() { |
| 378 | return score; |
| 379 | } |
| 380 | |
| 381 | public void setScore(int score) { |
| 382 | this.score = score; |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public String toString() { |
| 387 | return "AllTimeHigh{" + |
| 388 | "user='" + user + '\'' + |
| 389 | ", game='" + game + '\'' + |
| 390 | ", year=" + year + |
| 391 | ", month=" + month + |
| 392 | ", day=" + day + |
| 393 | ", score=" + score + |
| 394 | '}'; |
| 395 | } |
| 396 | |
| 397 | @SuppressWarnings("SimplifiableIfStatement") |
| 398 | @Override |
| 399 | public boolean equals(Object o) { |
| 400 | if (this == o) |
| 401 | return true; |
| 402 | if (!(o instanceof Score)) |
| 403 | return false; |
| 404 | |
| 405 | Score score1 = (Score) o; |
| 406 | |
| 407 | if (year != score1.year) |
| 408 | return false; |
| 409 | if (month != score1.month) |
| 410 | return false; |
| 411 | if (day != score1.day) |
| 412 | return false; |
| 413 | if (score != score1.score) |
| 414 | return false; |
| 415 | if (!user.equals(score1.user)) |
| 416 | return false; |
| 417 | return game.equals(score1.game); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public int hashCode() { |
| 422 | int result = user.hashCode(); |
| 423 | result = 31 * result + game.hashCode(); |
| 424 | result = 31 * result + year; |
| 425 | result = 31 * result + month; |
| 426 | result = 31 * result + day; |
| 427 | result = 31 * result + score; |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | } |
| 432 | } |