kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e8f426346b0446cc5cd77f6a699697b82b30dc1b3585d53e4c935cae336d0171 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-05-21 12:09 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-21 13:12 +02:00 |
| Parents | 82f4b46d6c03 |
8 files changed
modified driver-core/CHANGELOG.rst +6 −0
| @@ -1,6 +1,12 @@ | ||
| 1 | 1 | CHANGELOG |
| 2 | 2 | ========= |
| 3 | 3 | |
| 4 | +2.1.7: | |
| 5 | +------ | |
| 6 | + | |
| 7 | +- [improvement] Unify "Target" enum for schema elements (JAVA-782) | |
| 8 | + | |
| 9 | + | |
| 4 | 10 | 2.1.6: |
| 5 | 11 | ------ |
| 6 | 12 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +23 −42
| @@ -30,7 +30,6 @@ | ||
| 30 | 30 | import com.google.common.base.Predicates; |
| 31 | 31 | import com.google.common.collect.*; |
| 32 | 32 | import com.google.common.util.concurrent.*; |
| 33 | - | |
| 34 | 33 | import org.slf4j.Logger; |
| 35 | 34 | import org.slf4j.LoggerFactory; |
| 36 | 35 | |
| @@ -39,6 +38,8 @@ | ||
| 39 | 38 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 40 | 39 | import com.datastax.driver.core.policies.*; |
| 41 | 40 | |
| 41 | +import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 42 | + | |
| 42 | 43 | /** |
| 43 | 44 | * Information and known state of a Cassandra cluster. |
| 44 | 45 | * <p> |
| @@ -2006,30 +2007,32 @@ | ||
| 2006 | 2007 | } |
| 2007 | 2008 | } |
| 2008 | 2009 | |
| 2009 | - public void submitSchemaRefresh(final String keyspace, final String table, final String udt) { | |
| 2010 | + public void submitSchemaRefresh(final SchemaElement targetType, final String targetKeyspace, final String targetName) { | |
| 2010 | 2011 | logger.trace("Submitting schema refresh"); |
| 2011 | 2012 | executor.submit(new ExceptionCatchingRunnable() { |
| 2012 | 2013 | @Override |
| 2013 | 2014 | public void runMayThrow() throws InterruptedException, ExecutionException { |
| 2014 | - controlConnection.refreshSchema(keyspace, table, udt); | |
| 2015 | + controlConnection.refreshSchema(targetType, targetKeyspace, targetName); | |
| 2015 | 2016 | } |
| 2016 | 2017 | }); |
| 2017 | 2018 | } |
| 2018 | 2019 | |
| 2019 | 2020 | // refresh the schema using the provided connection, and notice the future with the provided resultset once done |
| 2020 | - public void refreshSchemaAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs, final String keyspace, final String table, final String udt) { | |
| 2021 | + public void refreshSchemaAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs, final SchemaElement target, final String keyspace, final String name) { | |
| 2021 | 2022 | if (logger.isDebugEnabled()) |
| 2022 | - logger.debug("Refreshing schema for {}{}", keyspace == null ? "" : keyspace, table == null ? "" : '.' + table); | |
| 2023 | + logger.debug("Refreshing schema for {}{}", | |
| 2024 | + target == null ? "everything" : keyspace, | |
| 2025 | + (target == KEYSPACE) ? "" : "." + name + " (" + target + ")"); | |
| 2023 | 2026 | |
| 2024 | - maybeRefreshSchemaAndSignal(connection, future, rs, keyspace, table, udt); | |
| 2027 | + maybeRefreshSchemaAndSignal(connection, future, rs, target, keyspace, name); | |
| 2025 | 2028 | } |
| 2026 | 2029 | |
| 2027 | 2030 | public void waitForSchemaAgreementAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs) { |
| 2028 | 2031 | maybeRefreshSchemaAndSignal(connection, future, rs, null, null, null); |
| 2029 | 2032 | } |
| 2030 | 2033 | |
| 2031 | - private void maybeRefreshSchemaAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs, final String keyspace, final String table, final String udt) { | |
| 2032 | - final boolean refreshSchema = (keyspace != null); // if false, only wait for schema agreement | |
| 2034 | + private void maybeRefreshSchemaAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs, final SchemaElement targetType, final String targetKeyspace, final String targetName) { | |
| 2035 | + final boolean refreshSchema = (targetKeyspace != null); // if false, only wait for schema agreement | |
| 2033 | 2036 | |
| 2034 | 2037 | executor.submit(new Runnable() { |
| 2035 | 2038 | @Override |
| @@ -2042,11 +2045,11 @@ | ||
| 2042 | 2045 | if (!schemaInAgreement) |
| 2043 | 2046 | logger.warn("No schema agreement from live replicas after {} s. The schema may not be up to date on some nodes.", configuration.getProtocolOptions().getMaxSchemaAgreementWaitSeconds()); |
| 2044 | 2047 | if (refreshSchema) |
| 2045 | - ControlConnection.refreshSchema(connection, keyspace, table, udt, Manager.this, false); | |
| 2048 | + ControlConnection.refreshSchema(connection, targetType, targetKeyspace, targetName, Manager.this, false); | |
| 2046 | 2049 | } catch (Exception e) { |
| 2047 | 2050 | if (refreshSchema) { |
| 2048 | 2051 | logger.error("Error during schema refresh ({}). The schema from Cluster.getMetadata() might appear stale. Asynchronously submitting job to fix.", e.getMessage()); |
| 2049 | - submitSchemaRefresh(keyspace, table, udt); | |
| 2052 | + submitSchemaRefresh(targetType, targetKeyspace, targetName); | |
| 2050 | 2053 | } else { |
| 2051 | 2054 | logger.warn("Error while waiting for schema agreement", e); |
| 2052 | 2055 | } |
| @@ -2169,52 +2172,30 @@ | ||
| 2169 | 2172 | ProtocolEvent.SchemaChange scc = (ProtocolEvent.SchemaChange)event; |
| 2170 | 2173 | switch (scc.change) { |
| 2171 | 2174 | case CREATED: |
| 2172 | - switch (scc.target) { | |
| 2173 | - case KEYSPACE: | |
| 2174 | - submitSchemaRefresh(scc.keyspace, null, null); | |
| 2175 | - break; | |
| 2176 | - case TABLE: | |
| 2177 | - submitSchemaRefresh(scc.keyspace, scc.name, null); | |
| 2178 | - break; | |
| 2179 | - case TYPE: | |
| 2180 | - submitSchemaRefresh(scc.keyspace, null, scc.name); | |
| 2181 | - break; | |
| 2182 | - } | |
| 2175 | + case UPDATED: | |
| 2176 | + submitSchemaRefresh(scc.targetType, scc.targetKeyspace, scc.targetName); | |
| 2183 | 2177 | break; |
| 2184 | 2178 | case DROPPED: |
| 2185 | 2179 | KeyspaceMetadata keyspace; |
| 2186 | - switch (scc.target) { | |
| 2180 | + switch (scc.targetType) { | |
| 2187 | 2181 | case KEYSPACE: |
| 2188 | - manager.metadata.removeKeyspace(scc.keyspace); | |
| 2182 | + manager.metadata.removeKeyspace(scc.targetKeyspace); | |
| 2189 | 2183 | break; |
| 2190 | 2184 | case TABLE: |
| 2191 | - keyspace = manager.metadata.getKeyspaceInternal(scc.keyspace); | |
| 2185 | + keyspace = manager.metadata.getKeyspaceInternal(scc.targetKeyspace); | |
| 2192 | 2186 | if (keyspace == null) |
| 2193 | 2187 | logger.warn("Received a DROPPED notification for table {}.{}, but this keyspace is unknown in our metadata", |
| 2194 | - scc.keyspace, scc.name); | |
| 2188 | + scc.targetKeyspace, scc.targetName); | |
| 2195 | 2189 | else |
| 2196 | - keyspace.removeTable(scc.name); | |
| 2190 | + keyspace.removeTable(scc.targetName); | |
| 2197 | 2191 | break; |
| 2198 | 2192 | case TYPE: |
| 2199 | - keyspace = manager.metadata.getKeyspaceInternal(scc.keyspace); | |
| 2193 | + keyspace = manager.metadata.getKeyspaceInternal(scc.targetKeyspace); | |
| 2200 | 2194 | if (keyspace == null) |
| 2201 | 2195 | logger.warn("Received a DROPPED notification for UDT {}.{}, but this keyspace is unknown in our metadata", |
| 2202 | - scc.keyspace, scc.name); | |
| 2196 | + scc.targetKeyspace, scc.targetName); | |
| 2203 | 2197 | else |
| 2204 | - keyspace.removeUserType(scc.name); | |
| 2205 | - break; | |
| 2206 | - } | |
| 2207 | - break; | |
| 2208 | - case UPDATED: | |
| 2209 | - switch (scc.target) { | |
| 2210 | - case KEYSPACE: | |
| 2211 | - submitSchemaRefresh(scc.keyspace, null, null); | |
| 2212 | - break; | |
| 2213 | - case TABLE: | |
| 2214 | - submitSchemaRefresh(scc.keyspace, scc.name, null); | |
| 2215 | - break; | |
| 2216 | - case TYPE: | |
| 2217 | - submitSchemaRefresh(scc.keyspace, null, scc.name); | |
| 2198 | + keyspace.removeUserType(scc.targetName); | |
| 2218 | 2199 | break; |
| 2219 | 2200 | } |
| 2220 | 2201 | break; |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +27 −19
| @@ -32,6 +32,10 @@ | ||
| 32 | 32 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 33 | 33 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 34 | 34 | |
| 35 | +import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 36 | +import static com.datastax.driver.core.SchemaElement.TABLE; | |
| 37 | +import static com.datastax.driver.core.SchemaElement.TYPE; | |
| 38 | + | |
| 35 | 39 | class ControlConnection implements Host.StateListener { |
| 36 | 40 | |
| 37 | 41 | private static final Logger logger = LoggerFactory.getLogger(ControlConnection.class); |
| @@ -284,16 +288,16 @@ | ||
| 284 | 288 | } |
| 285 | 289 | } |
| 286 | 290 | |
| 287 | - public void refreshSchema(String keyspace, String table, String udt) throws InterruptedException { | |
| 291 | + public void refreshSchema(SchemaElement targetType, String targetKeyspace, String targetName) throws InterruptedException { | |
| 288 | 292 | logger.debug("[Control connection] Refreshing schema for {}{}", |
| 289 | - keyspace == null ? "everything" : keyspace, | |
| 290 | - table == null ? (udt == null ? "" : '.' + udt + " (UDT)") : '.' + table + " (table)"); | |
| 293 | + targetType == null ? "everything" : targetKeyspace, | |
| 294 | + (targetType == KEYSPACE) ? "" : "." + targetName + " (" + targetType + ")"); | |
| 291 | 295 | try { |
| 292 | 296 | Connection c = connectionRef.get(); |
| 293 | 297 | // At startup, when we add the initial nodes, this will be null, which is ok |
| 294 | 298 | if (c == null) |
| 295 | 299 | return; |
| 296 | - refreshSchema(c, keyspace, table, udt, cluster, false); | |
| 300 | + refreshSchema(c, targetType, targetKeyspace, targetName, cluster, false); | |
| 297 | 301 | } catch (ConnectionException e) { |
| 298 | 302 | logger.debug("[Control connection] Connection error while refreshing schema ({})", e.getMessage()); |
| 299 | 303 | signalError(); |
| @@ -308,7 +312,7 @@ | ||
| 308 | 312 | } |
| 309 | 313 | } |
| 310 | 314 | |
| 311 | - static void refreshSchema(Connection connection, String keyspace, String table, String udt, Cluster.Manager cluster, boolean isInitialConnection) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { | |
| 315 | + static void refreshSchema(Connection connection, SchemaElement targetType, String targetKeyspace, String targetName, Cluster.Manager cluster, boolean isInitialConnection) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { | |
| 312 | 316 | Host host = cluster.metadata.getHost(connection.address); |
| 313 | 317 | // Neither host, nor it's version should be null. But instead of dying if there is a race or something, we can kind of try to infer |
| 314 | 318 | // a Cassandra version from the protocol version (this is not full proof, we can have the protocol 1 against C* 2.0+, but it's worth |
| @@ -324,24 +328,24 @@ | ||
| 324 | 328 | |
| 325 | 329 | // Make sure we're up to date on schema |
| 326 | 330 | String whereClause = ""; |
| 327 | - if (keyspace != null) { | |
| 328 | - whereClause = " WHERE keyspace_name = '" + keyspace + '\''; | |
| 329 | - if (table != null) | |
| 330 | - whereClause += " AND columnfamily_name = '" + table + '\''; | |
| 331 | - else if (udt != null) | |
| 332 | - whereClause += " AND type_name = '" + udt + '\''; | |
| 331 | + if (targetType != null) { | |
| 332 | + whereClause = " WHERE keyspace_name = '" + targetKeyspace + '\''; | |
| 333 | + if (targetType == TABLE) | |
| 334 | + whereClause += " AND columnfamily_name = '" + targetName + '\''; | |
| 335 | + else if (targetType == TYPE) | |
| 336 | + whereClause += " AND type_name = '" + targetName + '\''; | |
| 333 | 337 | } |
| 334 | 338 | |
| 335 | - DefaultResultSetFuture ksFuture = (table == null && udt == null) | |
| 339 | + DefaultResultSetFuture ksFuture = (targetType == null || targetType == KEYSPACE) | |
| 336 | 340 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_KEYSPACES + whereClause)) |
| 337 | 341 | : null; |
| 338 | - DefaultResultSetFuture udtFuture = table == null && (cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1)) | |
| 342 | + DefaultResultSetFuture udtFuture = (targetType == KEYSPACE && supportsUdts(cassandraVersion) || targetType == TYPE) | |
| 339 | 343 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_USERTYPES + whereClause)) |
| 340 | 344 | : null; |
| 341 | - DefaultResultSetFuture cfFuture = udt == null | |
| 345 | + DefaultResultSetFuture cfFuture = (targetType == KEYSPACE || targetType == TABLE) | |
| 342 | 346 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_COLUMN_FAMILIES + whereClause)) |
| 343 | 347 | : null; |
| 344 | - DefaultResultSetFuture colsFuture = udt == null | |
| 348 | + DefaultResultSetFuture colsFuture = (targetType == KEYSPACE || targetType == TABLE) | |
| 345 | 349 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_COLUMNS + whereClause)) |
| 346 | 350 | : null; |
| 347 | 351 | |
| @@ -355,7 +359,7 @@ | ||
| 355 | 359 | connection.write(colsFuture); |
| 356 | 360 | |
| 357 | 361 | try { |
| 358 | - cluster.metadata.rebuildSchema(keyspace, table, udt, | |
| 362 | + cluster.metadata.rebuildSchema(targetType, targetKeyspace, targetName, | |
| 359 | 363 | ksFuture == null ? null : ksFuture.get(), |
| 360 | 364 | udtFuture == null ? null : udtFuture.get(), |
| 361 | 365 | cfFuture == null ? null : cfFuture.get(), |
| @@ -368,12 +372,16 @@ | ||
| 368 | 372 | logger.error("Error parsing schema from Cassandra system tables: the schema in Cluster#getMetadata() will appear incomplete or stale", e); |
| 369 | 373 | } |
| 370 | 374 | |
| 371 | - // If the table is null, we either rebuild all from scratch or have an updated keyspace. In both case, rebuild the token map | |
| 372 | - // since some replication on some keyspace may have changed | |
| 373 | - if (table == null) | |
| 375 | + // If we rebuild all from scratch or have an updated keyspace, rebuild the token map since some replication on some keyspace | |
| 376 | + // may have changed | |
| 377 | + if (targetType == null || targetType == KEYSPACE) | |
| 374 | 378 | refreshNodeListAndTokenMap(connection, cluster, false, false); |
| 375 | 379 | } |
| 376 | 380 | |
| 381 | + private static boolean supportsUdts(VersionNumber cassandraVersion) { | |
| 382 | + return cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1); | |
| 383 | + } | |
| 384 | + | |
| 377 | 385 | public void refreshNodeListAndTokenMap() { |
| 378 | 386 | Connection c = connectionRef.get(); |
| 379 | 387 | // At startup, when we add the initial nodes, this will be null, which is ok |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +10 −32
| @@ -71,61 +71,39 @@ | ||
| 71 | 71 | ResultSet rs = ArrayBackedResultSet.fromMessage(rm, session, protocolVersion, info, statement); |
| 72 | 72 | switch (scc.change) { |
| 73 | 73 | case CREATED: |
| 74 | - switch (scc.target) { | |
| 75 | - case KEYSPACE: | |
| 76 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null, null); | |
| 77 | - break; | |
| 78 | - case TABLE: | |
| 79 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, scc.name, null); | |
| 80 | - break; | |
| 81 | - case TYPE: | |
| 82 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null, scc.name); | |
| 83 | - break; | |
| 84 | - } | |
| 74 | + case UPDATED: | |
| 75 | + session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.targetType, scc.targetKeyspace, scc.targetName); | |
| 85 | 76 | break; |
| 86 | 77 | case DROPPED: |
| 87 | 78 | KeyspaceMetadata keyspace; |
| 88 | - switch (scc.target) { | |
| 79 | + switch (scc.targetType) { | |
| 89 | 80 | case KEYSPACE: |
| 90 | 81 | // If that the one keyspace we are logged in, reset to null (it shouldn't really happen but ...) |
| 91 | 82 | // Note: Actually, Cassandra doesn't do that so we don't either as this could confuse prepared statements. |
| 92 | 83 | // We'll add it back if CASSANDRA-5358 changes that behavior |
| 93 | 84 | //if (scc.keyspace.equals(session.poolsState.keyspace)) |
| 94 | 85 | // session.poolsState.setKeyspace(null); |
| 95 | - session.cluster.manager.metadata.removeKeyspace(scc.keyspace); | |
| 86 | + session.cluster.manager.metadata.removeKeyspace(scc.targetKeyspace); | |
| 96 | 87 | break; |
| 97 | 88 | case TABLE: |
| 98 | - keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.keyspace); | |
| 89 | + keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.targetKeyspace); | |
| 99 | 90 | if (keyspace == null) |
| 100 | 91 | logger.warn("Received a DROPPED notification for table {}.{}, but this keyspace is unknown in our metadata", |
| 101 | - scc.keyspace, scc.name); | |
| 92 | + scc.targetKeyspace, scc.targetName); | |
| 102 | 93 | else |
| 103 | - keyspace.removeTable(scc.name); | |
| 94 | + keyspace.removeTable(scc.targetName); | |
| 104 | 95 | break; |
| 105 | 96 | case TYPE: |
| 106 | - keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.keyspace); | |
| 97 | + keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.targetKeyspace); | |
| 107 | 98 | if (keyspace == null) |
| 108 | 99 | logger.warn("Received a DROPPED notification for UDT {}.{}, but this keyspace is unknown in our metadata", |
| 109 | - scc.keyspace, scc.name); | |
| 100 | + scc.targetKeyspace, scc.targetName); | |
| 110 | 101 | else |
| 111 | - keyspace.removeUserType(scc.name); | |
| 102 | + keyspace.removeUserType(scc.targetName); | |
| 112 | 103 | break; |
| 113 | 104 | } |
| 114 | 105 | session.cluster.manager.waitForSchemaAgreementAndSignal(connection, this, rs); |
| 115 | 106 | break; |
| 116 | - case UPDATED: | |
| 117 | - switch (scc.target) { | |
| 118 | - case KEYSPACE: | |
| 119 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null, null); | |
| 120 | - break; | |
| 121 | - case TABLE: | |
| 122 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, scc.name, null); | |
| 123 | - break; | |
| 124 | - case TYPE: | |
| 125 | - session.cluster.manager.refreshSchemaAndSignal(connection, this, rs, scc.keyspace, null, scc.name); | |
| 126 | - break; | |
| 127 | - } | |
| 128 | - break; | |
| 129 | 107 | default: |
| 130 | 108 | logger.info("Ignoring unknown schema change result"); |
| 131 | 109 | break; |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +19 −15
| @@ -30,6 +30,10 @@ | ||
| 30 | 30 | |
| 31 | 31 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 32 | 32 | |
| 33 | +import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 34 | +import static com.datastax.driver.core.SchemaElement.TABLE; | |
| 35 | +import static com.datastax.driver.core.SchemaElement.TYPE; | |
| 36 | + | |
| 33 | 37 | /** |
| 34 | 38 | * Keeps metadata on the connected cluster, including known nodes and schema definitions. |
| 35 | 39 | */ |
| @@ -52,7 +56,7 @@ | ||
| 52 | 56 | } |
| 53 | 57 | |
| 54 | 58 | // Synchronized to make it easy to detect dropped keyspaces |
| 55 | - synchronized void rebuildSchema(String keyspaceName, String tableName, String udtName, ResultSet ks, ResultSet udts, ResultSet cfs, ResultSet cols, VersionNumber cassandraVersion) { | |
| 59 | + synchronized void rebuildSchema(SchemaElement targetType, String targetKeyspace, String targetName, ResultSet ks, ResultSet udts, ResultSet cfs, ResultSet cols, VersionNumber cassandraVersion) { | |
| 56 | 60 | |
| 57 | 61 | Map<String, List<Row>> cfDefs = new HashMap<String, List<Row>>(); |
| 58 | 62 | Map<String, List<Row>> udtDefs = new HashMap<String, List<Row>>(); |
| @@ -104,7 +108,7 @@ | ||
| 104 | 108 | } |
| 105 | 109 | } |
| 106 | 110 | |
| 107 | - if (tableName == null && udtName == null) { // Refresh one or all keyspaces | |
| 111 | + if (targetType == null || targetType == KEYSPACE) { // Refresh one or all keyspaces | |
| 108 | 112 | assert ks != null; |
| 109 | 113 | Set<String> addedKs = new HashSet<String>(); |
| 110 | 114 | for (Row ksRow : ks) { |
| @@ -120,39 +124,39 @@ | ||
| 120 | 124 | |
| 121 | 125 | // If keyspace is null, it means we're rebuilding from scratch, so |
| 122 | 126 | // remove anything that was not just added as it means it's a dropped keyspace |
| 123 | - if (keyspaceName == null) { | |
| 127 | + if (targetKeyspace == null) { | |
| 124 | 128 | Iterator<String> iter = keyspaces.keySet().iterator(); |
| 125 | 129 | while (iter.hasNext()) { |
| 126 | 130 | if (!addedKs.contains(iter.next())) |
| 127 | 131 | iter.remove(); |
| 128 | 132 | } |
| 129 | 133 | } |
| 130 | - } else if (tableName != null) { | |
| 131 | - assert keyspaceName != null; | |
| 132 | - KeyspaceMetadata ksm = keyspaces.get(keyspaceName); | |
| 134 | + } else if (targetType == TABLE) { | |
| 135 | + assert targetKeyspace != null; | |
| 136 | + KeyspaceMetadata ksm = keyspaces.get(targetKeyspace); | |
| 133 | 137 | |
| 134 | 138 | // If we update a keyspace we don't know about, something went |
| 135 | 139 | // wrong. Log an error an schedule a full schema rebuilt. |
| 136 | 140 | if (ksm == null) { |
| 137 | - logger.error(String.format("Asked to rebuild table %s.%s but I don't know keyspace %s", keyspaceName, tableName, keyspaceName)); | |
| 141 | + logger.error(String.format("Asked to rebuild table %s.%s but I don't know keyspace %s", targetKeyspace, targetName, targetKeyspace)); | |
| 138 | 142 | cluster.submitSchemaRefresh(null, null, null); |
| 139 | 143 | return; |
| 140 | 144 | } |
| 141 | 145 | |
| 142 | - if (cfDefs.containsKey(keyspaceName)) | |
| 143 | - buildTableMetadata(ksm, cfDefs.get(keyspaceName), colsDefs.get(keyspaceName), cassandraVersion); | |
| 144 | - } else { // udtName != null | |
| 145 | - assert keyspaceName != null; | |
| 146 | - KeyspaceMetadata ksm = keyspaces.get(keyspaceName); | |
| 146 | + if (cfDefs.containsKey(targetKeyspace)) | |
| 147 | + buildTableMetadata(ksm, cfDefs.get(targetKeyspace), colsDefs.get(targetKeyspace), cassandraVersion); | |
| 148 | + } else if (targetType == TYPE) { | |
| 149 | + assert targetKeyspace != null; | |
| 150 | + KeyspaceMetadata ksm = keyspaces.get(targetKeyspace); | |
| 147 | 151 | |
| 148 | 152 | if (ksm == null) { |
| 149 | - logger.error(String.format("Asked to rebuild type %s.%s but I don't know keyspace %s", keyspaceName, udtName, keyspaceName)); | |
| 153 | + logger.error(String.format("Asked to rebuild type %s.%s but I don't know keyspace %s", targetKeyspace, targetName, targetKeyspace)); | |
| 150 | 154 | cluster.submitSchemaRefresh(null, null, null); |
| 151 | 155 | return; |
| 152 | 156 | } |
| 153 | 157 | |
| 154 | - if (udtDefs.containsKey(keyspaceName)) | |
| 155 | - ksm.addUserTypes(udtDefs.get(keyspaceName)); | |
| 158 | + if (udtDefs.containsKey(targetKeyspace)) | |
| 159 | + ksm.addUserTypes(udtDefs.get(targetKeyspace)); | |
| 156 | 160 | } |
| 157 | 161 | } |
| 158 | 162 |
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolEvent.java +16 −14
| @@ -19,6 +19,9 @@ | ||
| 19 | 19 | |
| 20 | 20 | import io.netty.buffer.ByteBuf; |
| 21 | 21 | |
| 22 | +import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 23 | +import static com.datastax.driver.core.SchemaElement.TABLE; | |
| 24 | + | |
| 22 | 25 | class ProtocolEvent { |
| 23 | 26 | |
| 24 | 27 | public enum Type { TOPOLOGY_CHANGE, STATUS_CHANGE, SCHEMA_CHANGE } |
| @@ -95,25 +98,24 @@ | ||
| 95 | 98 | public static class SchemaChange extends ProtocolEvent { |
| 96 | 99 | |
| 97 | 100 | public enum Change { CREATED, UPDATED, DROPPED } |
| 98 | - public enum Target { KEYSPACE, TABLE, TYPE } | |
| 99 | 101 | |
| 100 | 102 | public final Change change; |
| 101 | - public final Target target; | |
| 102 | - public final String keyspace; | |
| 103 | - public final String name; | |
| 104 | - | |
| 105 | - public SchemaChange(Change change, Target target, String keyspace, String name) { | |
| 103 | + public final SchemaElement targetType; | |
| 104 | + public final String targetKeyspace; | |
| 105 | + public final String targetName; | |
| 106 | + | |
| 107 | + public SchemaChange(Change change, SchemaElement targetType, String targetKeyspace, String targetName) { | |
| 106 | 108 | super(Type.SCHEMA_CHANGE); |
| 107 | 109 | this.change = change; |
| 108 | - this.target = target; | |
| 109 | - this.keyspace = keyspace; | |
| 110 | - this.name = name; | |
| 110 | + this.targetType = targetType; | |
| 111 | + this.targetKeyspace = targetKeyspace; | |
| 112 | + this.targetName = targetName; | |
| 111 | 113 | } |
| 112 | 114 | |
| 113 | 115 | // Assumes the type has already been deserialized |
| 114 | 116 | private static SchemaChange deserializeEvent(ByteBuf bb, ProtocolVersion version) { |
| 115 | 117 | Change change; |
| 116 | - Target target; | |
| 118 | + SchemaElement target; | |
| 117 | 119 | String keyspace, name; |
| 118 | 120 | switch (version) { |
| 119 | 121 | case V1: |
| @@ -121,13 +123,13 @@ | ||
| 121 | 123 | change = CBUtil.readEnumValue(Change.class, bb); |
| 122 | 124 | keyspace = CBUtil.readString(bb); |
| 123 | 125 | name = CBUtil.readString(bb); |
| 124 | - target = name.isEmpty() ? Target.KEYSPACE : Target.TABLE; | |
| 126 | + target = name.isEmpty() ? KEYSPACE : TABLE; | |
| 125 | 127 | return new SchemaChange(change, target, keyspace, name); |
| 126 | 128 | case V3: |
| 127 | 129 | change = CBUtil.readEnumValue(Change.class, bb); |
| 128 | - target = CBUtil.readEnumValue(Target.class, bb); | |
| 130 | + target = CBUtil.readEnumValue(SchemaElement.class, bb); | |
| 129 | 131 | keyspace = CBUtil.readString(bb); |
| 130 | - name = (target == Target.KEYSPACE) ? "" : CBUtil.readString(bb); | |
| 132 | + name = (target == KEYSPACE) ? "" : CBUtil.readString(bb); | |
| 131 | 133 | return new SchemaChange(change, target, keyspace, name); |
| 132 | 134 | default: |
| 133 | 135 | throw version.unsupported(); |
| @@ -136,7 +138,7 @@ | ||
| 136 | 138 | |
| 137 | 139 | @Override |
| 138 | 140 | public String toString() { |
| 139 | - return change.toString() + ' ' + target + ' ' + keyspace + (name.isEmpty() ? "" : '.' + name); | |
| 141 | + return change.toString() + ' ' + targetType + ' ' + targetKeyspace + (targetName.isEmpty() ? "" : '.' + targetName); | |
| 140 | 142 | } |
| 141 | 143 | } |
| 142 | 144 |
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +15 −28
| @@ -21,13 +21,13 @@ | ||
| 21 | 21 | |
| 22 | 22 | import io.netty.buffer.ByteBuf; |
| 23 | 23 | |
| 24 | -import com.datastax.driver.core.ProtocolEvent.SchemaChange; | |
| 25 | -import com.datastax.driver.core.ProtocolEvent.SchemaChange.Change; | |
| 26 | -import com.datastax.driver.core.ProtocolEvent.SchemaChange.Target; | |
| 27 | 24 | import com.datastax.driver.core.Responses.Result.Rows.Metadata; |
| 28 | 25 | import com.datastax.driver.core.exceptions.*; |
| 29 | 26 | import com.datastax.driver.core.utils.Bytes; |
| 30 | 27 | |
| 28 | +import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 29 | +import static com.datastax.driver.core.SchemaElement.TABLE; | |
| 30 | + | |
| 31 | 31 | class Responses { |
| 32 | 32 | |
| 33 | 33 | private Responses() {} |
| @@ -480,19 +480,18 @@ | ||
| 480 | 480 | public static class SchemaChange extends Result { |
| 481 | 481 | |
| 482 | 482 | public enum Change { CREATED, UPDATED, DROPPED } |
| 483 | - public enum Target { KEYSPACE, TABLE, TYPE } | |
| 484 | 483 | |
| 485 | 484 | public final Change change; |
| 486 | - public final Target target; | |
| 487 | - public final String keyspace; | |
| 488 | - public final String name; | |
| 485 | + public final SchemaElement targetType; | |
| 486 | + public final String targetKeyspace; | |
| 487 | + public final String targetName; | |
| 489 | 488 | |
| 490 | 489 | public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() { |
| 491 | 490 | public Result decode(ByteBuf body, ProtocolVersion version) |
| 492 | 491 | { |
| 493 | 492 | // Note: the CREATE KEYSPACE/TABLE/TYPE SCHEMA_CHANGE response is different from the SCHEMA_CHANGE EVENT type |
| 494 | 493 | Change change; |
| 495 | - Target target; | |
| 494 | + SchemaElement target; | |
| 496 | 495 | String keyspace, name; |
| 497 | 496 | switch (version) { |
| 498 | 497 | case V1: |
| @@ -500,43 +499,31 @@ | ||
| 500 | 499 | change = CBUtil.readEnumValue(Change.class, body); |
| 501 | 500 | keyspace = CBUtil.readString(body); |
| 502 | 501 | name = CBUtil.readString(body); |
| 503 | - target = name.isEmpty() ? Target.KEYSPACE : Target.TABLE; | |
| 502 | + target = name.isEmpty() ? KEYSPACE : TABLE; | |
| 504 | 503 | return new SchemaChange(change, target, keyspace, name); |
| 505 | 504 | case V3: |
| 506 | 505 | change = CBUtil.readEnumValue(Change.class, body); |
| 507 | - target = CBUtil.readEnumValue(Target.class, body); | |
| 506 | + target = CBUtil.readEnumValue(SchemaElement.class, body); | |
| 508 | 507 | keyspace = CBUtil.readString(body); |
| 509 | - name = (target == Target.KEYSPACE) ? "" : CBUtil.readString(body); | |
| 508 | + name = (target == KEYSPACE) ? "" : CBUtil.readString(body); | |
| 510 | 509 | return new SchemaChange(change, target, keyspace, name); |
| 511 | 510 | default: |
| 512 | 511 | throw version.unsupported(); |
| 513 | 512 | } |
| 514 | 513 | } |
| 515 | - | |
| 516 | - private Target maybeReadTarget(ByteBuf body, ProtocolVersion version) { | |
| 517 | - switch (version) { | |
| 518 | - case V1: | |
| 519 | - case V2: | |
| 520 | - return null; | |
| 521 | - case V3: | |
| 522 | - return CBUtil.readEnumValue(Target.class, body); | |
| 523 | - default: | |
| 524 | - throw version.unsupported(); | |
| 525 | - } | |
| 526 | - } | |
| 527 | 514 | }; |
| 528 | 515 | |
| 529 | - private SchemaChange(Change change, Target target, String keyspace, String name) { | |
| 516 | + private SchemaChange(Change change, SchemaElement targetType, String targetKeyspace, String targetName) { | |
| 530 | 517 | super(Kind.SCHEMA_CHANGE); |
| 531 | 518 | this.change = change; |
| 532 | - this.target = target; | |
| 533 | - this.keyspace = keyspace; | |
| 534 | - this.name = name; | |
| 519 | + this.targetType = targetType; | |
| 520 | + this.targetKeyspace = targetKeyspace; | |
| 521 | + this.targetName = targetName; | |
| 535 | 522 | } |
| 536 | 523 | |
| 537 | 524 | @Override |
| 538 | 525 | public String toString() { |
| 539 | - return "RESULT schema change " + change + " on " + target + ' ' + keyspace + (name.isEmpty() ? "" : '.' + name); | |
| 526 | + return "RESULT schema change " + change + " on " + targetType + ' ' + targetKeyspace + (targetName.isEmpty() ? "" : '.' + targetName); | |
| 540 | 527 | } |
| 541 | 528 | } |
| 542 | 529 | } |
added driver-core/src/main/java/com/datastax/driver/core/SchemaElement.java +20 −0
| @@ -0,0 +1,20 @@ | ||
| 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.core; | |
| 17 | + | |
| 18 | +enum SchemaElement { | |
| 19 | + KEYSPACE, TABLE, TYPE | |
| 20 | +} |