|
8 | 8 | import com.datastax.driver.core.KeyspaceMetadata; |
9 | 9 | import com.datastax.driver.core.querybuilder.QueryBuilder; |
10 | 10 | import com.datastax.driver.core.schemabuilder.Create; |
| 11 | +import com.datastax.driver.core.schemabuilder.CreateIndex; |
11 | 12 | import com.datastax.driver.core.schemabuilder.CreateKeyspace; |
12 | 13 | import com.datastax.driver.core.schemabuilder.SchemaBuilder; |
13 | 14 | import com.datastax.driver.core.schemabuilder.SchemaBuilder.Direction; |
@@ -92,8 +93,8 @@ public void createTable( |
92 | 93 | // Create the system namespace if it does not exist |
93 | 94 | createKeyspace(systemNamespace, true, options); |
94 | 95 |
|
95 | | - createTableInternal(namespace, table, metadata, options); |
96 | | - createSecondaryIndexes(namespace, table, metadata.getSecondaryIndexNames(), options); |
| 96 | + createTableInternal(namespace, table, metadata, false, options); |
| 97 | + createSecondaryIndexes(namespace, table, metadata.getSecondaryIndexNames(), false); |
97 | 98 | } |
98 | 99 |
|
99 | 100 | @Override |
@@ -170,17 +171,29 @@ public void truncateTable(String namespace, String table) throws ExecutionExcept |
170 | 171 | public void createIndex( |
171 | 172 | String namespace, String table, String columnName, Map<String, String> options) |
172 | 173 | throws ExecutionException { |
| 174 | + createIndexInternal(namespace, table, columnName, false); |
| 175 | + } |
| 176 | + |
| 177 | + public void createIndexInternal( |
| 178 | + String namespace, String table, String columnName, boolean ifNotExists) |
| 179 | + throws ExecutionException { |
173 | 180 | String indexName = getIndexName(table, columnName); |
174 | | - SchemaStatement createIndex = |
175 | | - SchemaBuilder.createIndex(indexName) |
| 181 | + CreateIndex createIndex = SchemaBuilder.createIndex(indexName); |
| 182 | + if (ifNotExists) { |
| 183 | + createIndex = createIndex.ifNotExists(); |
| 184 | + } |
| 185 | + SchemaStatement createIndexStatement = |
| 186 | + createIndex |
176 | 187 | .onTable(quoteIfNecessary(namespace), quoteIfNecessary(table)) |
177 | 188 | .andColumn(quoteIfNecessary(columnName)); |
| 189 | + |
178 | 190 | try { |
179 | | - clusterManager.getSession().execute(createIndex.getQueryString()); |
| 191 | + clusterManager.getSession().execute(createIndexStatement.getQueryString()); |
180 | 192 | } catch (RuntimeException e) { |
181 | 193 | throw new ExecutionException( |
182 | 194 | String.format( |
183 | | - "Creating the secondary index for %s.%s.%s failed", namespace, table, columnName), |
| 195 | + "Creating the secondary index on the %s column of the %s table failed", |
| 196 | + columnName, getFullTableName(namespace, table)), |
184 | 197 | e); |
185 | 198 | } |
186 | 199 | } |
@@ -300,12 +313,13 @@ public boolean namespaceExists(String namespace) throws ExecutionException { |
300 | 313 | public void repairTable( |
301 | 314 | String namespace, String table, TableMetadata metadata, Map<String, String> options) |
302 | 315 | throws ExecutionException { |
303 | | - // We have this check to stay consistent with the behavior of the other admins classes |
304 | | - if (!tableExists(namespace, table)) { |
305 | | - throw new IllegalArgumentException( |
306 | | - "The table " + getFullTableName(namespace, table) + " does not exist"); |
| 316 | + try { |
| 317 | + createTableInternal(namespace, table, metadata, true, options); |
| 318 | + createSecondaryIndexes(namespace, table, metadata.getSecondaryIndexNames(), true); |
| 319 | + } catch (ExecutionException e) { |
| 320 | + throw new ExecutionException( |
| 321 | + String.format("Repairing the %s table failed", getFullTableName(namespace, table)), e); |
307 | 322 | } |
308 | | - // The table metadata are not managed by ScalarDB, so we don't need to do anything here |
309 | 323 | } |
310 | 324 |
|
311 | 325 | @Override |
@@ -431,10 +445,17 @@ public Set<String> getNamespaceNames() throws ExecutionException { |
431 | 445 |
|
432 | 446 | @VisibleForTesting |
433 | 447 | void createTableInternal( |
434 | | - String keyspace, String table, TableMetadata metadata, Map<String, String> options) |
| 448 | + String keyspace, |
| 449 | + String table, |
| 450 | + TableMetadata metadata, |
| 451 | + boolean ifNotExists, |
| 452 | + Map<String, String> options) |
435 | 453 | throws ExecutionException { |
436 | 454 | Create createTable = |
437 | 455 | SchemaBuilder.createTable(quoteIfNecessary(keyspace), quoteIfNecessary(table)); |
| 456 | + if (ifNotExists) { |
| 457 | + createTable = createTable.ifNotExists(); |
| 458 | + } |
438 | 459 | // Add columns |
439 | 460 | for (String pk : metadata.getPartitionKeyNames()) { |
440 | 461 | createTable = |
@@ -487,16 +508,16 @@ void createTableInternal( |
487 | 508 | clusterManager.getSession().execute(createTableWithOptions.getQueryString()); |
488 | 509 | } catch (RuntimeException e) { |
489 | 510 | throw new ExecutionException( |
490 | | - String.format("Creating the table %s.%s failed", keyspace, table), e); |
| 511 | + String.format("Creating the %s table failed", getFullTableName(keyspace, table)), e); |
491 | 512 | } |
492 | 513 | } |
493 | 514 |
|
494 | 515 | @VisibleForTesting |
495 | 516 | void createSecondaryIndexes( |
496 | | - String keyspace, String table, Set<String> secondaryIndexNames, Map<String, String> options) |
| 517 | + String keyspace, String table, Set<String> secondaryIndexNames, boolean ifNotExists) |
497 | 518 | throws ExecutionException { |
498 | 519 | for (String name : secondaryIndexNames) { |
499 | | - createIndex(keyspace, table, name, options); |
| 520 | + createIndexInternal(keyspace, table, name, ifNotExists); |
500 | 521 | } |
501 | 522 | } |
502 | 523 |
|
|
0 commit comments