Elffuss BraidHub

kikocis/java-driver public

JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126. (#721)

Changeseta0c416644d7e37ab664dfca52fe24a128a1375bc7cdc7885cf6a20fe241d05e6
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-08-30 22:59 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-08-30 13:59 -07:00
Parents09c916afb89d

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -4,6 +4,7 @@
44
55 - [improvement] JAVA-1246: Driver swallows the real exception in a few cases
66 - [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.
78
89
910 ### 3.0.3
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +18 −10
@@ -15,10 +15,7 @@
1515 */
1616 package com.datastax.driver.mapping;
1717
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.*;
2219 import com.datastax.driver.mapping.MethodMapper.ParamMapper;
2320 import com.datastax.driver.mapping.annotations.*;
2421 import com.google.common.base.Strings;
@@ -67,11 +64,18 @@
6764 ));
6865 }
6966
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+ }
7277
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);
7579
7680 List<Field> pks = new ArrayList<Field>();
7781 List<Field> ccs = new ArrayList<Field>();
@@ -119,7 +123,7 @@
119123 return mapper;
120124 }
121125
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) {
123127 List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size());
124128 for (int i = 0; i < fields.size(); i++) {
125129 Field field = fields.get(i);
@@ -150,7 +154,11 @@
150154 ));
151155 }
152156
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);
154162 if (userType == null)
155163 throw new IllegalArgumentException(String.format("User type %s does not exist in keyspace %s", udtName, ksName));
156164
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperMaterializedViewTest.java +158 −6
@@ -17,10 +17,7 @@
1717
1818 import com.datastax.driver.core.CCMTestsSupport;
1919 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.*;
2421 import org.testng.annotations.BeforeMethod;
2522 import org.testng.annotations.Test;
2623
@@ -32,7 +29,6 @@
3229 @CassandraVersion(major = 3.0)
3330 public class MapperMaterializedViewTest extends CCMTestsSupport {
3431
35-
3632 @Override
3733 public void onTestContextInitialized() {
3834 // Example schema taken from: http://www.datastax.com/dev/blog/new-in-cassandra-3-0-materialized-views
@@ -73,11 +69,28 @@
7369 );
7470 }
7571
72+
73+ private Mapper<AllTimeHigh> mapper;
74+
7675 private ScoreAccessor accessor;
7776
7877 @BeforeMethod(groups = "short")
7978 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();
8194 }
8295
8396 /**
@@ -159,14 +172,22 @@
159172
160173 @Table(name = "scores")
161174 public static class Score {
175+
176+ //primary key: user, game, year, month, day
177+
178+ @PartitionKey
162179 String user;
163180
181+ @ClusteringColumn(0)
164182 String game;
165183
184+ @ClusteringColumn(1)
166185 int year;
167186
187+ @ClusteringColumn(2)
168188 int month;
169189
190+ @ClusteringColumn(3)
170191 int day;
171192
172193 int score;
@@ -277,4 +298,135 @@
277298 return result;
278299 }
279300 }
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+ }
280432 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +1 −1
@@ -471,7 +471,7 @@
471471 manager.mapper(User.class);
472472 fail("Expected IllegalArgumentException");
473473 } 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);
475475 }
476476 }
477477