Elffuss BraidHub

kikocis/java-driver public

Unify "Target" enum for schema elements (JAVA-782).
Changesete8f426346b0446cc5cd77f6a699697b82b30dc1b3585d53e4c935cae336d0171
Authorolim7t <omichallat+github@gmail.com> · 2015-05-21 12:09 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-05-21 13:12 +02:00
Parents82f4b46d6c03

Browse the files at this changeset

8 files changed

modified driver-core/CHANGELOG.rst +6 −0
@@ -1,6 +1,12 @@
11 CHANGELOG
22 =========
33
4+2.1.7:
5+------
6+
7+- [improvement] Unify "Target" enum for schema elements (JAVA-782)
8+
9+
410 2.1.6:
511 ------
612
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +23 −42
@@ -30,7 +30,6 @@
3030 import com.google.common.base.Predicates;
3131 import com.google.common.collect.*;
3232 import com.google.common.util.concurrent.*;
33-
3433 import org.slf4j.Logger;
3534 import org.slf4j.LoggerFactory;
3635
@@ -39,6 +38,8 @@
3938 import com.datastax.driver.core.exceptions.NoHostAvailableException;
4039 import com.datastax.driver.core.policies.*;
4140
41+import static com.datastax.driver.core.SchemaElement.KEYSPACE;
42+
4243 /**
4344 * Information and known state of a Cassandra cluster.
4445 * <p>
@@ -2006,30 +2007,32 @@
20062007 }
20072008 }
20082009
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) {
20102011 logger.trace("Submitting schema refresh");
20112012 executor.submit(new ExceptionCatchingRunnable() {
20122013 @Override
20132014 public void runMayThrow() throws InterruptedException, ExecutionException {
2014- controlConnection.refreshSchema(keyspace, table, udt);
2015+ controlConnection.refreshSchema(targetType, targetKeyspace, targetName);
20152016 }
20162017 });
20172018 }
20182019
20192020 // 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) {
20212022 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 + ")");
20232026
2024- maybeRefreshSchemaAndSignal(connection, future, rs, keyspace, table, udt);
2027+ maybeRefreshSchemaAndSignal(connection, future, rs, target, keyspace, name);
20252028 }
20262029
20272030 public void waitForSchemaAgreementAndSignal(final Connection connection, final DefaultResultSetFuture future, final ResultSet rs) {
20282031 maybeRefreshSchemaAndSignal(connection, future, rs, null, null, null);
20292032 }
20302033
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
20332036
20342037 executor.submit(new Runnable() {
20352038 @Override
@@ -2042,11 +2045,11 @@
20422045 if (!schemaInAgreement)
20432046 logger.warn("No schema agreement from live replicas after {} s. The schema may not be up to date on some nodes.", configuration.getProtocolOptions().getMaxSchemaAgreementWaitSeconds());
20442047 if (refreshSchema)
2045- ControlConnection.refreshSchema(connection, keyspace, table, udt, Manager.this, false);
2048+ ControlConnection.refreshSchema(connection, targetType, targetKeyspace, targetName, Manager.this, false);
20462049 } catch (Exception e) {
20472050 if (refreshSchema) {
20482051 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);
20502053 } else {
20512054 logger.warn("Error while waiting for schema agreement", e);
20522055 }
@@ -2169,52 +2172,30 @@
21692172 ProtocolEvent.SchemaChange scc = (ProtocolEvent.SchemaChange)event;
21702173 switch (scc.change) {
21712174 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);
21832177 break;
21842178 case DROPPED:
21852179 KeyspaceMetadata keyspace;
2186- switch (scc.target) {
2180+ switch (scc.targetType) {
21872181 case KEYSPACE:
2188- manager.metadata.removeKeyspace(scc.keyspace);
2182+ manager.metadata.removeKeyspace(scc.targetKeyspace);
21892183 break;
21902184 case TABLE:
2191- keyspace = manager.metadata.getKeyspaceInternal(scc.keyspace);
2185+ keyspace = manager.metadata.getKeyspaceInternal(scc.targetKeyspace);
21922186 if (keyspace == null)
21932187 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);
21952189 else
2196- keyspace.removeTable(scc.name);
2190+ keyspace.removeTable(scc.targetName);
21972191 break;
21982192 case TYPE:
2199- keyspace = manager.metadata.getKeyspaceInternal(scc.keyspace);
2193+ keyspace = manager.metadata.getKeyspaceInternal(scc.targetKeyspace);
22002194 if (keyspace == null)
22012195 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);
22032197 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);
22182199 break;
22192200 }
22202201 break;
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +27 −19
@@ -32,6 +32,10 @@
3232 import com.datastax.driver.core.exceptions.DriverInternalError;
3333 import com.datastax.driver.core.exceptions.NoHostAvailableException;
3434
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+
3539 class ControlConnection implements Host.StateListener {
3640
3741 private static final Logger logger = LoggerFactory.getLogger(ControlConnection.class);
@@ -284,16 +288,16 @@
284288 }
285289 }
286290
287- public void refreshSchema(String keyspace, String table, String udt) throws InterruptedException {
291+ public void refreshSchema(SchemaElement targetType, String targetKeyspace, String targetName) throws InterruptedException {
288292 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 + ")");
291295 try {
292296 Connection c = connectionRef.get();
293297 // At startup, when we add the initial nodes, this will be null, which is ok
294298 if (c == null)
295299 return;
296- refreshSchema(c, keyspace, table, udt, cluster, false);
300+ refreshSchema(c, targetType, targetKeyspace, targetName, cluster, false);
297301 } catch (ConnectionException e) {
298302 logger.debug("[Control connection] Connection error while refreshing schema ({})", e.getMessage());
299303 signalError();
@@ -308,7 +312,7 @@
308312 }
309313 }
310314
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 {
312316 Host host = cluster.metadata.getHost(connection.address);
313317 // 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
314318 // 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 @@
324328
325329 // Make sure we're up to date on schema
326330 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 + '\'';
333337 }
334338
335- DefaultResultSetFuture ksFuture = (table == null && udt == null)
339+ DefaultResultSetFuture ksFuture = (targetType == null || targetType == KEYSPACE)
336340 ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_KEYSPACES + whereClause))
337341 : null;
338- DefaultResultSetFuture udtFuture = table == null && (cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1))
342+ DefaultResultSetFuture udtFuture = (targetType == KEYSPACE && supportsUdts(cassandraVersion) || targetType == TYPE)
339343 ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_USERTYPES + whereClause))
340344 : null;
341- DefaultResultSetFuture cfFuture = udt == null
345+ DefaultResultSetFuture cfFuture = (targetType == KEYSPACE || targetType == TABLE)
342346 ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_COLUMN_FAMILIES + whereClause))
343347 : null;
344- DefaultResultSetFuture colsFuture = udt == null
348+ DefaultResultSetFuture colsFuture = (targetType == KEYSPACE || targetType == TABLE)
345349 ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_COLUMNS + whereClause))
346350 : null;
347351
@@ -355,7 +359,7 @@
355359 connection.write(colsFuture);
356360
357361 try {
358- cluster.metadata.rebuildSchema(keyspace, table, udt,
362+ cluster.metadata.rebuildSchema(targetType, targetKeyspace, targetName,
359363 ksFuture == null ? null : ksFuture.get(),
360364 udtFuture == null ? null : udtFuture.get(),
361365 cfFuture == null ? null : cfFuture.get(),
@@ -368,12 +372,16 @@
368372 logger.error("Error parsing schema from Cassandra system tables: the schema in Cluster#getMetadata() will appear incomplete or stale", e);
369373 }
370374
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)
374378 refreshNodeListAndTokenMap(connection, cluster, false, false);
375379 }
376380
381+ private static boolean supportsUdts(VersionNumber cassandraVersion) {
382+ return cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1);
383+ }
384+
377385 public void refreshNodeListAndTokenMap() {
378386 Connection c = connectionRef.get();
379387 // 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 @@
7171 ResultSet rs = ArrayBackedResultSet.fromMessage(rm, session, protocolVersion, info, statement);
7272 switch (scc.change) {
7373 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);
8576 break;
8677 case DROPPED:
8778 KeyspaceMetadata keyspace;
88- switch (scc.target) {
79+ switch (scc.targetType) {
8980 case KEYSPACE:
9081 // If that the one keyspace we are logged in, reset to null (it shouldn't really happen but ...)
9182 // Note: Actually, Cassandra doesn't do that so we don't either as this could confuse prepared statements.
9283 // We'll add it back if CASSANDRA-5358 changes that behavior
9384 //if (scc.keyspace.equals(session.poolsState.keyspace))
9485 // session.poolsState.setKeyspace(null);
95- session.cluster.manager.metadata.removeKeyspace(scc.keyspace);
86+ session.cluster.manager.metadata.removeKeyspace(scc.targetKeyspace);
9687 break;
9788 case TABLE:
98- keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.keyspace);
89+ keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.targetKeyspace);
9990 if (keyspace == null)
10091 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);
10293 else
103- keyspace.removeTable(scc.name);
94+ keyspace.removeTable(scc.targetName);
10495 break;
10596 case TYPE:
106- keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.keyspace);
97+ keyspace = session.cluster.manager.metadata.getKeyspaceInternal(scc.targetKeyspace);
10798 if (keyspace == null)
10899 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);
110101 else
111- keyspace.removeUserType(scc.name);
102+ keyspace.removeUserType(scc.targetName);
112103 break;
113104 }
114105 session.cluster.manager.waitForSchemaAgreementAndSignal(connection, this, rs);
115106 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;
129107 default:
130108 logger.info("Ignoring unknown schema change result");
131109 break;
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +19 −15
@@ -30,6 +30,10 @@
3030
3131 import com.datastax.driver.core.exceptions.DriverInternalError;
3232
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+
3337 /**
3438 * Keeps metadata on the connected cluster, including known nodes and schema definitions.
3539 */
@@ -52,7 +56,7 @@
5256 }
5357
5458 // 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) {
5660
5761 Map<String, List<Row>> cfDefs = new HashMap<String, List<Row>>();
5862 Map<String, List<Row>> udtDefs = new HashMap<String, List<Row>>();
@@ -104,7 +108,7 @@
104108 }
105109 }
106110
107- if (tableName == null && udtName == null) { // Refresh one or all keyspaces
111+ if (targetType == null || targetType == KEYSPACE) { // Refresh one or all keyspaces
108112 assert ks != null;
109113 Set<String> addedKs = new HashSet<String>();
110114 for (Row ksRow : ks) {
@@ -120,39 +124,39 @@
120124
121125 // If keyspace is null, it means we're rebuilding from scratch, so
122126 // remove anything that was not just added as it means it's a dropped keyspace
123- if (keyspaceName == null) {
127+ if (targetKeyspace == null) {
124128 Iterator<String> iter = keyspaces.keySet().iterator();
125129 while (iter.hasNext()) {
126130 if (!addedKs.contains(iter.next()))
127131 iter.remove();
128132 }
129133 }
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);
133137
134138 // If we update a keyspace we don't know about, something went
135139 // wrong. Log an error an schedule a full schema rebuilt.
136140 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));
138142 cluster.submitSchemaRefresh(null, null, null);
139143 return;
140144 }
141145
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);
147151
148152 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));
150154 cluster.submitSchemaRefresh(null, null, null);
151155 return;
152156 }
153157
154- if (udtDefs.containsKey(keyspaceName))
155- ksm.addUserTypes(udtDefs.get(keyspaceName));
158+ if (udtDefs.containsKey(targetKeyspace))
159+ ksm.addUserTypes(udtDefs.get(targetKeyspace));
156160 }
157161 }
158162
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolEvent.java +16 −14
@@ -19,6 +19,9 @@
1919
2020 import io.netty.buffer.ByteBuf;
2121
22+import static com.datastax.driver.core.SchemaElement.KEYSPACE;
23+import static com.datastax.driver.core.SchemaElement.TABLE;
24+
2225 class ProtocolEvent {
2326
2427 public enum Type { TOPOLOGY_CHANGE, STATUS_CHANGE, SCHEMA_CHANGE }
@@ -95,25 +98,24 @@
9598 public static class SchemaChange extends ProtocolEvent {
9699
97100 public enum Change { CREATED, UPDATED, DROPPED }
98- public enum Target { KEYSPACE, TABLE, TYPE }
99101
100102 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) {
106108 super(Type.SCHEMA_CHANGE);
107109 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;
111113 }
112114
113115 // Assumes the type has already been deserialized
114116 private static SchemaChange deserializeEvent(ByteBuf bb, ProtocolVersion version) {
115117 Change change;
116- Target target;
118+ SchemaElement target;
117119 String keyspace, name;
118120 switch (version) {
119121 case V1:
@@ -121,13 +123,13 @@
121123 change = CBUtil.readEnumValue(Change.class, bb);
122124 keyspace = CBUtil.readString(bb);
123125 name = CBUtil.readString(bb);
124- target = name.isEmpty() ? Target.KEYSPACE : Target.TABLE;
126+ target = name.isEmpty() ? KEYSPACE : TABLE;
125127 return new SchemaChange(change, target, keyspace, name);
126128 case V3:
127129 change = CBUtil.readEnumValue(Change.class, bb);
128- target = CBUtil.readEnumValue(Target.class, bb);
130+ target = CBUtil.readEnumValue(SchemaElement.class, bb);
129131 keyspace = CBUtil.readString(bb);
130- name = (target == Target.KEYSPACE) ? "" : CBUtil.readString(bb);
132+ name = (target == KEYSPACE) ? "" : CBUtil.readString(bb);
131133 return new SchemaChange(change, target, keyspace, name);
132134 default:
133135 throw version.unsupported();
@@ -136,7 +138,7 @@
136138
137139 @Override
138140 public String toString() {
139- return change.toString() + ' ' + target + ' ' + keyspace + (name.isEmpty() ? "" : '.' + name);
141+ return change.toString() + ' ' + targetType + ' ' + targetKeyspace + (targetName.isEmpty() ? "" : '.' + targetName);
140142 }
141143 }
142144
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +15 −28
@@ -21,13 +21,13 @@
2121
2222 import io.netty.buffer.ByteBuf;
2323
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;
2724 import com.datastax.driver.core.Responses.Result.Rows.Metadata;
2825 import com.datastax.driver.core.exceptions.*;
2926 import com.datastax.driver.core.utils.Bytes;
3027
28+import static com.datastax.driver.core.SchemaElement.KEYSPACE;
29+import static com.datastax.driver.core.SchemaElement.TABLE;
30+
3131 class Responses {
3232
3333 private Responses() {}
@@ -480,19 +480,18 @@
480480 public static class SchemaChange extends Result {
481481
482482 public enum Change { CREATED, UPDATED, DROPPED }
483- public enum Target { KEYSPACE, TABLE, TYPE }
484483
485484 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;
489488
490489 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
491490 public Result decode(ByteBuf body, ProtocolVersion version)
492491 {
493492 // Note: the CREATE KEYSPACE/TABLE/TYPE SCHEMA_CHANGE response is different from the SCHEMA_CHANGE EVENT type
494493 Change change;
495- Target target;
494+ SchemaElement target;
496495 String keyspace, name;
497496 switch (version) {
498497 case V1:
@@ -500,43 +499,31 @@
500499 change = CBUtil.readEnumValue(Change.class, body);
501500 keyspace = CBUtil.readString(body);
502501 name = CBUtil.readString(body);
503- target = name.isEmpty() ? Target.KEYSPACE : Target.TABLE;
502+ target = name.isEmpty() ? KEYSPACE : TABLE;
504503 return new SchemaChange(change, target, keyspace, name);
505504 case V3:
506505 change = CBUtil.readEnumValue(Change.class, body);
507- target = CBUtil.readEnumValue(Target.class, body);
506+ target = CBUtil.readEnumValue(SchemaElement.class, body);
508507 keyspace = CBUtil.readString(body);
509- name = (target == Target.KEYSPACE) ? "" : CBUtil.readString(body);
508+ name = (target == KEYSPACE) ? "" : CBUtil.readString(body);
510509 return new SchemaChange(change, target, keyspace, name);
511510 default:
512511 throw version.unsupported();
513512 }
514513 }
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- }
527514 };
528515
529- private SchemaChange(Change change, Target target, String keyspace, String name) {
516+ private SchemaChange(Change change, SchemaElement targetType, String targetKeyspace, String targetName) {
530517 super(Kind.SCHEMA_CHANGE);
531518 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;
535522 }
536523
537524 @Override
538525 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);
540527 }
541528 }
542529 }
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+}