kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a0c416644d7e37ab664dfca52fe24a128a1375bc7cdc7885cf6a20fe241d05e6 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-08-30 22:59 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-08-30 13:59 -07:00 |
| Parents | 09c916afb89d |
4 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [improvement] JAVA-1246: Driver swallows the real exception in a few cases |
| 6 | 6 | - [improvement] JAVA-1261: Throw error when attempting to page in I/O thread. |
| 7 | +- [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126. | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 3.0.3 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +18 −10
| @@ -15,10 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.ConsistencyLevel; | |
| 19 | -import com.datastax.driver.core.TableMetadata; | |
| 20 | -import com.datastax.driver.core.TypeCodec; | |
| 21 | -import com.datastax.driver.core.UserType; | |
| 18 | +import com.datastax.driver.core.*; | |
| 22 | 19 | import com.datastax.driver.mapping.MethodMapper.ParamMapper; |
| 23 | 20 | import com.datastax.driver.mapping.annotations.*; |
| 24 | 21 | import com.google.common.base.Strings; |
| @@ -67,11 +64,18 @@ | ||
| 67 | 64 | )); |
| 68 | 65 | } |
| 69 | 66 | |
| 70 | - EntityMapper<T> mapper = factory.create(entityClass, ksName, tableName, writeConsistency, readConsistency); | |
| 71 | - TableMetadata tableMetadata = mappingManager.getSession().getCluster().getMetadata().getKeyspace(ksName).getTable(tableName); | |
| 67 | + KeyspaceMetadata keyspaceMetadata = mappingManager.getSession().getCluster().getMetadata().getKeyspace(ksName); | |
| 68 | + if (keyspaceMetadata == null) | |
| 69 | + throw new IllegalArgumentException(String.format("Keyspace %s does not exist", ksName)); | |
| 70 | + | |
| 71 | + AbstractTableMetadata tableMetadata = keyspaceMetadata.getTable(tableName); | |
| 72 | + if (tableMetadata == null) { | |
| 73 | + tableMetadata = keyspaceMetadata.getMaterializedView(tableName); | |
| 74 | + if (tableMetadata == null) | |
| 75 | + throw new IllegalArgumentException(String.format("Table or materialized view %s does not exist in keyspace %s", tableName, ksName)); | |
| 76 | + } | |
| 72 | 77 | |
| 73 | - if (tableMetadata == null) | |
| 74 | - throw new IllegalArgumentException(String.format("Table %s does not exist in keyspace %s", tableName, ksName)); | |
| 78 | + EntityMapper<T> mapper = factory.create(entityClass, ksName, tableName, writeConsistency, readConsistency); | |
| 75 | 79 | |
| 76 | 80 | List<Field> pks = new ArrayList<Field>(); |
| 77 | 81 | List<Field> ccs = new ArrayList<Field>(); |
| @@ -119,7 +123,7 @@ | ||
| 119 | 123 | return mapper; |
| 120 | 124 | } |
| 121 | 125 | |
| 122 | - private static <T> List<ColumnMapper<T>> createColumnMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter, TableMetadata tableMetadata, String ksName, String tableName) { | |
| 126 | + private static <T> List<ColumnMapper<T>> createColumnMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter, AbstractTableMetadata tableMetadata, String ksName, String tableName) { | |
| 123 | 127 | List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size()); |
| 124 | 128 | for (int i = 0; i < fields.size(); i++) { |
| 125 | 129 | Field field = fields.get(i); |
| @@ -150,7 +154,11 @@ | ||
| 150 | 154 | )); |
| 151 | 155 | } |
| 152 | 156 | |
| 153 | - UserType userType = mappingManager.getSession().getCluster().getMetadata().getKeyspace(ksName).getUserType(udtName); | |
| 157 | + KeyspaceMetadata keyspaceMetadata = mappingManager.getSession().getCluster().getMetadata().getKeyspace(ksName); | |
| 158 | + if (keyspaceMetadata == null) | |
| 159 | + throw new IllegalArgumentException(String.format("Keyspace %s does not exist", ksName)); | |
| 160 | + | |
| 161 | + UserType userType = keyspaceMetadata.getUserType(udtName); | |
| 154 | 162 | if (userType == null) |
| 155 | 163 | throw new IllegalArgumentException(String.format("User type %s does not exist in keyspace %s", udtName, ksName)); |
| 156 | 164 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperMaterializedViewTest.java +158 −6
| @@ -17,10 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | -import com.datastax.driver.mapping.annotations.Accessor; | |
| 21 | -import com.datastax.driver.mapping.annotations.Param; | |
| 22 | -import com.datastax.driver.mapping.annotations.Query; | |
| 23 | -import com.datastax.driver.mapping.annotations.Table; | |
| 20 | +import com.datastax.driver.mapping.annotations.*; | |
| 24 | 21 | import org.testng.annotations.BeforeMethod; |
| 25 | 22 | import org.testng.annotations.Test; |
| 26 | 23 | |
| @@ -32,7 +29,6 @@ | ||
| 32 | 29 | @CassandraVersion(major = 3.0) |
| 33 | 30 | public class MapperMaterializedViewTest extends CCMTestsSupport { |
| 34 | 31 | |
| 35 | - | |
| 36 | 32 | @Override |
| 37 | 33 | public void onTestContextInitialized() { |
| 38 | 34 | // Example schema taken from: http://www.datastax.com/dev/blog/new-in-cassandra-3-0-materialized-views |
| @@ -73,11 +69,28 @@ | ||
| 73 | 69 | ); |
| 74 | 70 | } |
| 75 | 71 | |
| 72 | + | |
| 73 | + private Mapper<AllTimeHigh> mapper; | |
| 74 | + | |
| 76 | 75 | private ScoreAccessor accessor; |
| 77 | 76 | |
| 78 | 77 | @BeforeMethod(groups = "short") |
| 79 | 78 | public void setUp() { |
| 80 | - accessor = new MappingManager(session()).createAccessor(ScoreAccessor.class); | |
| 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(); | |
| 81 | 94 | } |
| 82 | 95 | |
| 83 | 96 | /** |
| @@ -159,14 +172,22 @@ | ||
| 159 | 172 | |
| 160 | 173 | @Table(name = "scores") |
| 161 | 174 | public static class Score { |
| 175 | + | |
| 176 | + //primary key: user, game, year, month, day | |
| 177 | + | |
| 178 | + @PartitionKey | |
| 162 | 179 | String user; |
| 163 | 180 | |
| 181 | + @ClusteringColumn(0) | |
| 164 | 182 | String game; |
| 165 | 183 | |
| 184 | + @ClusteringColumn(1) | |
| 166 | 185 | int year; |
| 167 | 186 | |
| 187 | + @ClusteringColumn(2) | |
| 168 | 188 | int month; |
| 169 | 189 | |
| 190 | + @ClusteringColumn(3) | |
| 170 | 191 | int day; |
| 171 | 192 | |
| 172 | 193 | int score; |
| @@ -277,4 +298,135 @@ | ||
| 277 | 298 | return result; |
| 278 | 299 | } |
| 279 | 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 | + } | |
| 280 | 432 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +1 −1
| @@ -471,7 +471,7 @@ | ||
| 471 | 471 | manager.mapper(User.class); |
| 472 | 472 | fail("Expected IllegalArgumentException"); |
| 473 | 473 | } catch (IllegalArgumentException e) { |
| 474 | - assertThat(e.getMessage()).isEqualTo("Table users does not exist in keyspace " + keyspace); | |
| 474 | + assertThat(e.getMessage()).isEqualTo("Table or materialized view users does not exist in keyspace " + keyspace); | |
| 475 | 475 | } |
| 476 | 476 | } |
| 477 | 477 |