Skip to content

Commit 15bc49c

Browse files
committed
Make computed fields incompatible with C* 1.2 since they rely on aliases (JAVA-661)
1 parent 268b9cb commit 15bc49c

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.common.base.Strings;
2828

2929
import com.datastax.driver.core.ConsistencyLevel;
30+
import com.datastax.driver.core.ProtocolVersion;
3031
import com.datastax.driver.mapping.MethodMapper.EnumParamMapper;
3132
import com.datastax.driver.mapping.MethodMapper.NestedUDTParamMapper;
3233
import com.datastax.driver.mapping.MethodMapper.ParamMapper;
@@ -74,7 +75,10 @@ public static <T> EntityMapper<T> parseEntity(Class<T> entityClass, EntityMapper
7475
for (Field field : entityClass.getDeclaredFields()) {
7576
if(field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC)
7677
continue;
77-
78+
79+
if (!mappingManager.supportsAliases && field.getAnnotation(Computed.class) != null)
80+
throw new UnsupportedOperationException("Computed fields are not supported with native protocol v1");
81+
7882
AnnotationChecks.validateAnnotations(field, "entity",
7983
Column.class, ClusteringColumn.class, Enumerated.class, Frozen.class, FrozenKey.class,
8084
FrozenValue.class, PartitionKey.class, Transient.class, Computed.class);
@@ -95,7 +99,7 @@ public static <T> EntityMapper<T> parseEntity(Class<T> entityClass, EntityMapper
9599
}
96100
}
97101

98-
AtomicInteger columnCounter = new AtomicInteger(0);
102+
AtomicInteger columnCounter = mappingManager.supportsAliases ? new AtomicInteger(0) : null;
99103

100104
Collections.sort(pks, fieldComparator);
101105
Collections.sort(ccs, fieldComparator);

driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
import com.datastax.driver.core.*;
30-
import com.datastax.driver.mapping.annotations.Computed;
3130
import com.datastax.driver.mapping.annotations.Accessor;
31+
import com.datastax.driver.mapping.annotations.Computed;
3232

3333
/**
3434
* An object handling the mapping of a particular class.
@@ -327,7 +327,9 @@ public Result<T> map(ResultSet resultSet) {
327327
* @see #map(ResultSet)
328328
*/
329329
public Result<T> mapAliased(ResultSet resultSet) {
330-
return new Result<T>(resultSet, mapper, protocolVersion, true);
330+
return (manager.supportsAliases)
331+
? new Result<T>(resultSet, mapper, protocolVersion, true)
332+
: map(resultSet);
331333
}
332334

333335
/**

driver-mapping/src/main/java/com/datastax/driver/mapping/MappingManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public class MappingManager {
3333

3434
private final Session session;
35+
final boolean supportsAliases;
3536

3637
private volatile Map<Class<?>, Mapper<?>> mappers = Collections.<Class<?>, Mapper<?>>emptyMap();
3738
private volatile Map<Class<?>, UDTMapper<?>> udtMappers = Collections.<Class<?>, UDTMapper<?>>emptyMap();
@@ -44,6 +45,12 @@ public class MappingManager {
4445
*/
4546
public MappingManager(Session session) {
4647
this.session = session;
48+
49+
ProtocolVersion protocolVersion = session.getCluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum();
50+
// The real condition should be Cassandra version >= 2.0, but mappers need this so that generated queries are compatible,
51+
// and we don't know in advance the version of all future peers.
52+
// At least if protocol >=2 we know there won't be any 1.2 nodes.
53+
this.supportsAliases = (protocolVersion != ProtocolVersion.V1);
4754
}
4855

4956
/**

driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.datastax.driver.core.*;
2727
import com.datastax.driver.core.exceptions.InvalidQueryException;
28+
import com.datastax.driver.core.utils.CassandraVersion;
2829
import com.datastax.driver.mapping.annotations.*;
2930

3031
import static com.datastax.driver.core.Assertions.assertThat;
@@ -34,6 +35,7 @@
3435
* annotation to map computed fields.
3536
*/
3637
@SuppressWarnings("unused")
38+
@CassandraVersion(major = 2.0)
3739
public class MapperComputedFieldsTest extends CCMBridge.PerClassSingleNodeCluster {
3840

3941
@Override

0 commit comments

Comments
 (0)