kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3b30b4f33e7ecf7d15224fe09fa644f1457bb1b3fa35a74fd04b71928c7cfab5 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2016-02-24 11:04 UTC |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-01 16:57 +01:00 |
| Parents | dc23ffd69e7e |
9 files changed
modified changelog/README.md +1 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | - [bug] JAVA-1068: Unwrap StatementWrappers when hashing the paging state. |
| 22 | 22 | - [bug] JAVA-831: Mapper can't load an entity where the PK is a UDT. |
| 23 | 23 | - [improvement] JAVA-1021: Improve error message when connect() is called with an invalid keyspace name. |
| 24 | +- [improvement] JAVA-879: Mapper.map() accepts mapper-generated and user queries. | |
| 24 | 25 | |
| 25 | 26 | Merged from 2.0 branch: |
| 26 | 27 |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +22 −15
| @@ -52,11 +52,8 @@ | ||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | static ArrayBackedResultSet fromMessage(Responses.Result msg, SessionManager session, ProtocolVersion protocolVersion, ExecutionInfo info, Statement statement) { |
| 55 | - info = update(info, msg, session); | |
| 56 | 55 | |
| 57 | 56 | switch (msg.kind) { |
| 58 | - case VOID: | |
| 59 | - return empty(info); | |
| 60 | 57 | case ROWS: |
| 61 | 58 | Responses.Result.Rows r = (Responses.Result.Rows) msg; |
| 62 | 59 | |
| @@ -72,27 +69,38 @@ | ||
| 72 | 69 | Token.Factory tokenFactory = (session == null) ? null |
| 73 | 70 | : session.getCluster().manager.metadata.tokenFactory(); |
| 74 | 71 | |
| 72 | + info = update(info, r, session, r.metadata.pagingState, protocolVersion, statement); | |
| 73 | + | |
| 75 | 74 | // info can be null only for internal calls, but we don't page those. We assert |
| 76 | - // this explicitly because MultiPage implementation don't support info == null. | |
| 75 | + // this explicitly because MultiPage implementation doesn't support info == null. | |
| 77 | 76 | assert r.metadata.pagingState == null || info != null; |
| 78 | 77 | return r.metadata.pagingState == null |
| 79 | 78 | ? new SinglePage(columnDefs, tokenFactory, protocolVersion, r.data, info) |
| 80 | - : new MultiPage(columnDefs, tokenFactory, protocolVersion, r.data, info, r.metadata.pagingState, session, statement); | |
| 79 | + : new MultiPage(columnDefs, tokenFactory, protocolVersion, r.data, info, r.metadata.pagingState, session); | |
| 81 | 80 | |
| 81 | + case VOID: | |
| 82 | 82 | case SET_KEYSPACE: |
| 83 | 83 | case SCHEMA_CHANGE: |
| 84 | + info = update(info, msg, session, null, protocolVersion, statement); | |
| 84 | 85 | return empty(info); |
| 85 | 86 | case PREPARED: |
| 86 | 87 | throw new RuntimeException("Prepared statement received when a ResultSet was expected"); |
| 87 | 88 | default: |
| 88 | 89 | logger.error("Received unknown result type '{}'; returning empty result set", msg.kind); |
| 90 | + info = update(info, msg, session, null, protocolVersion, statement); | |
| 89 | 91 | return empty(info); |
| 90 | 92 | } |
| 91 | 93 | } |
| 92 | 94 | |
| 93 | - private static ExecutionInfo update(ExecutionInfo info, Responses.Result msg, SessionManager session) { | |
| 95 | + private static ExecutionInfo update(ExecutionInfo info, Responses.Result msg, SessionManager session, | |
| 96 | + ByteBuffer pagingState, ProtocolVersion protocolVersion, Statement statement) { | |
| 97 | + if (info == null) | |
| 98 | + return null; | |
| 99 | + | |
| 94 | 100 | UUID tracingId = msg.getTracingId(); |
| 95 | - return tracingId == null || info == null ? info : info.withTrace(new QueryTrace(tracingId, session)); | |
| 101 | + QueryTrace trace = (tracingId == null) ? null : new QueryTrace(tracingId, session); | |
| 102 | + | |
| 103 | + return info.with(trace, pagingState, statement, protocolVersion); | |
| 96 | 104 | } |
| 97 | 105 | |
| 98 | 106 | private static ArrayBackedResultSet empty(ExecutionInfo info) { |
| @@ -226,7 +234,6 @@ | ||
| 226 | 234 | private volatile FetchingState fetchState; |
| 227 | 235 | |
| 228 | 236 | private final SessionManager session; |
| 229 | - private final Statement statement; | |
| 230 | 237 | |
| 231 | 238 | private MultiPage(ColumnDefinitions metadata, |
| 232 | 239 | Token.Factory tokenFactory, |
| @@ -234,19 +241,17 @@ | ||
| 234 | 241 | Queue<List<ByteBuffer>> rows, |
| 235 | 242 | ExecutionInfo info, |
| 236 | 243 | ByteBuffer pagingState, |
| 237 | - SessionManager session, | |
| 238 | - Statement statement) { | |
| 244 | + SessionManager session) { | |
| 239 | 245 | |
| 240 | 246 | // Note: as of Cassandra 2.1.0, it turns out that the result of a CAS update is never paged, so |
| 241 | 247 | // we could hard-code the result of wasApplied in this class to "true". However, we can not be sure |
| 242 | 248 | // that this will never change, so apply the generic check by peeking at the first row. |
| 243 | 249 | super(metadata, tokenFactory, rows.peek(), protocolVersion); |
| 244 | 250 | this.currentPage = rows; |
| 245 | - this.infos.offer(info.withPagingState(pagingState, protocolVersion).withStatement(statement)); | |
| 251 | + this.infos.offer(info); | |
| 246 | 252 | |
| 247 | 253 | this.fetchState = new FetchingState(pagingState, null); |
| 248 | 254 | this.session = session; |
| 249 | - this.statement = statement; | |
| 250 | 255 | } |
| 251 | 256 | |
| 252 | 257 | @Override |
| @@ -320,6 +325,9 @@ | ||
| 320 | 325 | |
| 321 | 326 | private ListenableFuture<Void> queryNextPage(ByteBuffer nextStart, final SettableFuture<Void> future) { |
| 322 | 327 | |
| 328 | + Statement statement = this.infos.peek().getStatement(); | |
| 329 | + | |
| 330 | + | |
| 323 | 331 | assert !(statement instanceof BatchStatement); |
| 324 | 332 | |
| 325 | 333 | final Message.Request request = session.makeRequestMessage(statement, nextStart); |
| @@ -340,15 +348,14 @@ | ||
| 340 | 348 | switch (response.type) { |
| 341 | 349 | case RESULT: |
| 342 | 350 | Responses.Result rm = (Responses.Result) response; |
| 343 | - info = update(info, rm, MultiPage.this.session); | |
| 344 | 351 | if (rm.kind == Responses.Result.Kind.ROWS) { |
| 345 | 352 | Responses.Result.Rows rows = (Responses.Result.Rows) rm; |
| 346 | - if (rows.metadata.pagingState != null) | |
| 347 | - info = info.withPagingState(rows.metadata.pagingState, protocolVersion).withStatement(statement); | |
| 353 | + info = update(info, rm, MultiPage.this.session, rows.metadata.pagingState, protocolVersion, statement); | |
| 348 | 354 | MultiPage.this.nextPages.offer(rows.data); |
| 349 | 355 | MultiPage.this.fetchState = rows.metadata.pagingState == null ? null : new FetchingState(rows.metadata.pagingState, null); |
| 350 | 356 | } else if (rm.kind == Responses.Result.Kind.VOID) { |
| 351 | 357 | // We shouldn't really get a VOID message here but well, no harm in handling it I suppose |
| 358 | + info = update(info, rm, MultiPage.this.session, null, protocolVersion, statement); | |
| 352 | 359 | MultiPage.this.fetchState = null; |
| 353 | 360 | } else { |
| 354 | 361 | logger.error("Received unknown result type '{}' during paging: ignoring message", rm.kind); |
modified driver-core/src/main/java/com/datastax/driver/core/ExecutionInfo.java +16 −10
| @@ -46,20 +46,17 @@ | ||
| 46 | 46 | this(triedHosts, null, null, null, null, null, true); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - ExecutionInfo withTrace(QueryTrace newTrace) { | |
| 50 | - return new ExecutionInfo(triedHosts, achievedConsistency, newTrace, pagingState, protocolVersion, statement, schemaInAgreement); | |
| 51 | - } | |
| 52 | - | |
| 53 | 49 | ExecutionInfo withAchievedConsistency(ConsistencyLevel newConsistency) { |
| 54 | 50 | return new ExecutionInfo(triedHosts, newConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement); |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | - ExecutionInfo withPagingState(ByteBuffer pagingState, ProtocolVersion protocolVersion) { | |
| 58 | - return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement); | |
| 59 | - } | |
| 60 | - | |
| 61 | - ExecutionInfo withStatement(Statement statement) { | |
| 62 | - return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement); | |
| 53 | + ExecutionInfo with(QueryTrace newTrace, ByteBuffer newPagingState, Statement newStatement, ProtocolVersion protocolVersion) { | |
| 54 | + return new ExecutionInfo(this.triedHosts, this.achievedConsistency, | |
| 55 | + newTrace, | |
| 56 | + newPagingState, protocolVersion, | |
| 57 | + newStatement, | |
| 58 | + schemaInAgreement | |
| 59 | + ); | |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | 62 | /** |
| @@ -185,4 +182,13 @@ | ||
| 185 | 182 | void setSchemaInAgreement(boolean schemaAgreement) { |
| 186 | 183 | this.schemaInAgreement = schemaAgreement; |
| 187 | 184 | } |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Get the statement that has been executed. | |
| 188 | + * | |
| 189 | + * @return the statement executed. | |
| 190 | + */ | |
| 191 | + public Statement getStatement() { | |
| 192 | + return this.statement; | |
| 193 | + } | |
| 188 | 194 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +21 −24
| @@ -20,7 +20,6 @@ | ||
| 20 | 20 | import com.datastax.driver.core.querybuilder.Insert; |
| 21 | 21 | import com.datastax.driver.core.querybuilder.QueryBuilder; |
| 22 | 22 | import com.datastax.driver.mapping.Mapper.Option.SaveNullFields; |
| 23 | -import com.datastax.driver.mapping.annotations.Accessor; | |
| 24 | 23 | import com.datastax.driver.mapping.annotations.Computed; |
| 25 | 24 | import com.google.common.base.Function; |
| 26 | 25 | import com.google.common.base.Functions; |
| @@ -79,7 +78,7 @@ | ||
| 79 | 78 | this.mapOneFunction = new Function<ResultSet, T>() { |
| 80 | 79 | @Override |
| 81 | 80 | public T apply(ResultSet rs) { |
| 82 | - return Mapper.this.mapAliased(rs).one(); | |
| 81 | + return Mapper.this.map(rs).one(); | |
| 83 | 82 | } |
| 84 | 83 | }; |
| 85 | 84 | this.mapOneFunctionWithoutAliases = new Function<ResultSet, T>() { |
| @@ -322,7 +321,7 @@ | ||
| 322 | 321 | if (primaryKeys.size() != mapper.primaryKeySize()) |
| 323 | 322 | throw new IllegalArgumentException(String.format("Invalid number of PRIMARY KEY columns provided, %d expected but got %d", mapper.primaryKeySize(), primaryKeys.size())); |
| 324 | 323 | |
| 325 | - BoundStatement bs = getPreparedQuery(QueryType.GET, options).bind(); | |
| 324 | + BoundStatement bs = new MapperBoundStatement(getPreparedQuery(QueryType.GET, options)); | |
| 326 | 325 | int i = 0; |
| 327 | 326 | for (Object value : primaryKeys) { |
| 328 | 327 | ColumnMapper<T> column = mapper.getPrimaryKeyColumn(i); |
| @@ -359,7 +358,7 @@ | ||
| 359 | 358 | * at least one of those values is {@code null}. |
| 360 | 359 | */ |
| 361 | 360 | public T get(Object... objects) { |
| 362 | - return mapAliased(session().execute(getQuery(objects))).one(); | |
| 361 | + return map(session().execute(getQuery(objects))).one(); | |
| 363 | 362 | } |
| 364 | 363 | |
| 365 | 364 | /** |
| @@ -602,39 +601,37 @@ | ||
| 602 | 601 | /** |
| 603 | 602 | * Maps the rows from a {@code ResultSet} into the class this is a mapper of. |
| 604 | 603 | * <p/> |
| 605 | - * Use this method to map a result set that was not generated by the mapper (e.g. a result set coming from a | |
| 606 | - * manual query or an {@link Accessor method}. | |
| 607 | - * It expects that the result set contains all column mapped in the target class, | |
| 608 | - * and that they are not aliased. {@link Computed} fields will not be filled in mapped objects. | |
| 604 | + * If the query was user-generated (that is, passed directly to {@code session.execute()} or configured with | |
| 605 | + * {@link com.datastax.driver.mapping.annotations.Query @Query} in an | |
| 606 | + * {@link com.datastax.driver.mapping.annotations.Accessor @Accessor}-annotated interface), then only the columns | |
| 607 | + * present in the result set will be mapped to the corresponding fields, and {@link Computed} fields will not be | |
| 608 | + * populated. | |
| 609 | + * <p/> | |
| 610 | + * If the query was generated by the mapper (for example with {@link #getQuery(Object...)}), all fields will be | |
| 611 | + * mapped, as if the object came from a direct {@link #get(Object...)} call. | |
| 609 | 612 | * |
| 610 | 613 | * @param resultSet the {@code ResultSet} to map. |
| 611 | 614 | * @return the mapped result set. Note that the returned mapped result set |
| 612 | 615 | * will encapsulate {@code resultSet} and so consuming results from this |
| 613 | 616 | * returned mapped result set will consume results from {@code resultSet} |
| 614 | 617 | * and vice-versa. |
| 615 | - * @see #mapAliased(ResultSet) | |
| 616 | 618 | */ |
| 617 | 619 | public Result<T> map(ResultSet resultSet) { |
| 618 | - return new Result<T>(resultSet, mapper, protocolVersion); | |
| 620 | + boolean useAlias = !manager.isCassandraV1 && isFromMapperQuery(resultSet); | |
| 621 | + return new Result<T>(resultSet, mapper, protocolVersion, useAlias); | |
| 622 | + } | |
| 623 | + | |
| 624 | + private boolean isFromMapperQuery(ResultSet resultSet) { | |
| 625 | + return resultSet.getExecutionInfo().getStatement() instanceof MapperBoundStatement; | |
| 619 | 626 | } |
| 620 | 627 | |
| 621 | 628 | /** |
| 622 | - * Maps the rows from a {@code ResultSet} into the class this is a mapper of. | |
| 623 | - * <p/> | |
| 624 | - * Use this method to map a result set coming from the execution of the {@code Statement} returned by | |
| 625 | - * {@link #getQuery(Object...)}. {@link Computed} fields will be filled in the mapped object. | |
| 626 | - * | |
| 627 | - * @param resultSet the {@code ResultSet} to map. | |
| 628 | - * @return the mapped result set. Note that the returned mapped result set | |
| 629 | - * will encapsulate {@code resultSet} and so consuming results from this | |
| 630 | - * returned mapped result set will consume results from {@code resultSet} | |
| 631 | - * and vice-versa. | |
| 632 | - * @see #map(ResultSet) | |
| 629 | + * @deprecated you no longer need to specify whether a result set is aliased, it will be detected automatically. Use | |
| 630 | + * {@link #map(ResultSet)} instead of this method. | |
| 633 | 631 | */ |
| 632 | + @Deprecated | |
| 634 | 633 | public Result<T> mapAliased(ResultSet resultSet) { |
| 635 | - return (manager.isCassandraV1) | |
| 636 | - ? map(resultSet) // no aliases | |
| 637 | - : new Result<T>(resultSet, mapper, protocolVersion, true); | |
| 634 | + return map(resultSet); | |
| 638 | 635 | } |
| 639 | 636 | |
| 640 | 637 | /** |
added driver-mapping/src/main/java/com/datastax/driver/mapping/MapperBoundStatement.java +28 −0
| @@ -0,0 +1,28 @@ | ||
| 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.BoundStatement; | |
| 19 | +import com.datastax.driver.core.PreparedStatement; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * A bound statement that was generated by the object mapper. | |
| 23 | + */ | |
| 24 | +class MapperBoundStatement extends BoundStatement { | |
| 25 | + public MapperBoundStatement(PreparedStatement statement) { | |
| 26 | + super(statement); | |
| 27 | + } | |
| 28 | +} |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java +3 −6
| @@ -35,10 +35,6 @@ | ||
| 35 | 35 | private final ProtocolVersion protocolVersion; |
| 36 | 36 | private final boolean useAlias; |
| 37 | 37 | |
| 38 | - Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion) { | |
| 39 | - this(rs, mapper, protocolVersion, false); | |
| 40 | - } | |
| 41 | - | |
| 42 | 38 | Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) { |
| 43 | 39 | this.rs = rs; |
| 44 | 40 | this.mapper = mapper; |
| @@ -50,8 +46,9 @@ | ||
| 50 | 46 | T entity = mapper.newEntity(); |
| 51 | 47 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 52 | 48 | String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName(); |
| 53 | - if (!row.getColumnDefinitions().contains(name)) | |
| 49 | + if (!row.getColumnDefinitions().contains(name)) { | |
| 54 | 50 | continue; |
| 51 | + } | |
| 55 | 52 | ByteBuffer bytes = row.getBytesUnsafe(name); |
| 56 | 53 | if (bytes != null) |
| 57 | 54 | cm.setValue(entity, cm.getDataType().deserialize(bytes, protocolVersion)); |
| @@ -108,7 +105,7 @@ | ||
| 108 | 105 | * this mapped result set. |
| 109 | 106 | */ |
| 110 | 107 | @Override |
| 111 | - public Iterator<T> iterator() { | |
| 108 | + public Iterator<T> iterator() { | |
| 112 | 109 | return new Iterator<T>() { |
| 113 | 110 | private final Iterator<Row> rowIterator = rs.iterator(); |
| 114 | 111 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java +1 −1
| @@ -102,7 +102,7 @@ | ||
| 102 | 102 | getQuery.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| 103 | 103 | ResultSet rs = session().execute(getQuery); |
| 104 | 104 | |
| 105 | - Result<User> result = userMapper.mapAliased(rs); | |
| 105 | + Result<User> result = userMapper.map(rs); | |
| 106 | 106 | User user = result.one(); |
| 107 | 107 | |
| 108 | 108 | assertThat(user.getLogin()).isEqualTo("testlogin"); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperOptionTest.java +1 −1
| @@ -83,7 +83,7 @@ | ||
| 83 | 83 | |
| 84 | 84 | ResultSet rs = session().execute(bs); |
| 85 | 85 | assertThat(rs.getExecutionInfo().getQueryTrace()).isNotNull(); |
| 86 | - User us = mapper.mapAliased(rs).one(); | |
| 86 | + User us = mapper.map(rs).one(); | |
| 87 | 87 | assertThat(us.getV()).isEqualTo("toget"); |
| 88 | 88 | |
| 89 | 89 | mapper.getQuery(45, Option.timestamp(1337)); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +7 −7
| @@ -157,17 +157,18 @@ | ||
| 157 | 157 | @Table(name = "posts") |
| 158 | 158 | public static class Post { |
| 159 | 159 | |
| 160 | - @PartitionKey | |
| 161 | - @Column(name = "user_id") | |
| 162 | - private UUID userId; | |
| 160 | + private String title; | |
| 161 | + private String content; | |
| 162 | + private InetAddress device; | |
| 163 | 163 | |
| 164 | 164 | @ClusteringColumn |
| 165 | 165 | @Column(name = "post_id") |
| 166 | 166 | private UUID postId; |
| 167 | 167 | |
| 168 | - private String title; | |
| 169 | - private String content; | |
| 170 | - private InetAddress device; | |
| 168 | + @PartitionKey | |
| 169 | + @Column(name = "user_id") | |
| 170 | + private UUID userId; | |
| 171 | + | |
| 171 | 172 | |
| 172 | 173 | private Set<String> tags; |
| 173 | 174 | |
| @@ -423,7 +424,6 @@ | ||
| 423 | 424 | session().execute("delete from posts where user_id = " + u1.getUserId()); |
| 424 | 425 | } |
| 425 | 426 | |
| 426 | - | |
| 427 | 427 | @Test(groups = "short") |
| 428 | 428 | public void should_return_table_metadata() throws Exception { |
| 429 | 429 | MappingManager manager = new MappingManager(session()); |