Skip to content

Commit ac1a0d6

Browse files
michaelsembwevertzolov
authored andcommitted
Avoid calling CREATE KEYSPACE|TABLE IF NOT EXISTS … unnecessarily
This also is a fix for AstraDB, which throws an exception if you try to `CREATE KEYSPACE IF NOT EXISTS …` And use more compatible `USING 'StorageAttachedIndex'` index creation syntax.
1 parent 3c40268 commit ac1a0d6

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

vector-stores/spring-ai-cassandra/src/main/java/org/springframework/ai/vectorstore/CassandraVectorStoreConfig.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ private void ensureIndexesExists() {
441441
{
442442
SimpleStatement indexStmt = SchemaBuilder.createIndex(this.schema.index)
443443
.ifNotExists()
444-
.custom("SAI")
444+
.custom("StorageAttachedIndex")
445445
.onTable(this.schema.keyspace, this.schema.table)
446446
.andColumn(this.schema.embedding)
447447
.build();
@@ -457,7 +457,7 @@ private void ensureIndexesExists() {
457457

458458
SimpleStatement indexStmt = SchemaBuilder.createIndex(String.format("%s_idx", metadata.name()))
459459
.ifNotExists()
460-
.custom("SAI")
460+
.custom("StorageAttachedIndex")
461461
.onTable(this.schema.keyspace, this.schema.table)
462462
.andColumn(metadata.name())
463463
.build();
@@ -468,39 +468,41 @@ private void ensureIndexesExists() {
468468
}
469469

470470
private void ensureTableExists(int vectorDimension) {
471+
if (this.session.getMetadata().getKeyspace(this.schema.keyspace).get().getTable(this.schema.table).isEmpty()) {
471472

472-
CreateTable createTable = null;
473+
CreateTable createTable = null;
473474

474-
CreateTableStart createTableStart = SchemaBuilder.createTable(this.schema.keyspace, this.schema.table)
475-
.ifNotExists();
475+
CreateTableStart createTableStart = SchemaBuilder.createTable(this.schema.keyspace, this.schema.table)
476+
.ifNotExists();
476477

477-
for (SchemaColumn partitionKey : this.schema.partitionKeys) {
478-
createTable = (null != createTable ? createTable : createTableStart).withPartitionKey(partitionKey.name,
479-
partitionKey.type);
480-
}
481-
for (SchemaColumn clusteringKey : this.schema.clusteringKeys) {
482-
createTable = createTable.withClusteringColumn(clusteringKey.name, clusteringKey.type);
483-
}
478+
for (SchemaColumn partitionKey : this.schema.partitionKeys) {
479+
createTable = (null != createTable ? createTable : createTableStart).withPartitionKey(partitionKey.name,
480+
partitionKey.type);
481+
}
482+
for (SchemaColumn clusteringKey : this.schema.clusteringKeys) {
483+
createTable = createTable.withClusteringColumn(clusteringKey.name, clusteringKey.type);
484+
}
484485

485-
createTable = createTable.withColumn(this.schema.content, DataTypes.TEXT);
486+
createTable = createTable.withColumn(this.schema.content, DataTypes.TEXT);
486487

487-
for (SchemaColumn metadata : this.schema.metadataColumns) {
488-
createTable = createTable.withColumn(metadata.name(), metadata.type());
489-
}
490-
491-
// https://datastax-oss.atlassian.net/browse/JAVA-3118
492-
// .withColumn(config.embedding, new DefaultVectorType(DataTypes.FLOAT,
493-
// vectorDimension));
494-
495-
StringBuilder tableStmt = new StringBuilder(createTable.asCql());
496-
tableStmt.setLength(tableStmt.length() - 1);
497-
tableStmt.append(',')
498-
.append(this.schema.embedding)
499-
.append(" vector<float,")
500-
.append(vectorDimension)
501-
.append(">)");
502-
logger.debug("Executing {}", tableStmt.toString());
503-
this.session.execute(tableStmt.toString());
488+
for (SchemaColumn metadata : this.schema.metadataColumns) {
489+
createTable = createTable.withColumn(metadata.name(), metadata.type());
490+
}
491+
492+
// https://datastax-oss.atlassian.net/browse/JAVA-3118
493+
// .withColumn(config.embedding, new DefaultVectorType(DataTypes.FLOAT,
494+
// vectorDimension));
495+
496+
StringBuilder tableStmt = new StringBuilder(createTable.asCql());
497+
tableStmt.setLength(tableStmt.length() - 1);
498+
tableStmt.append(',')
499+
.append(this.schema.embedding)
500+
.append(" vector<float,")
501+
.append(vectorDimension)
502+
.append(">)");
503+
logger.debug("Executing {}", tableStmt.toString());
504+
this.session.execute(tableStmt.toString());
505+
}
504506
}
505507

506508
private void ensureTableColumnsExist(int vectorDimension) {
@@ -563,14 +565,15 @@ private void ensureTableColumnsExist(int vectorDimension) {
563565
}
564566

565567
private void ensureKeyspaceExists() {
568+
if (this.session.getMetadata().getKeyspace(this.schema.keyspace).isEmpty()) {
569+
SimpleStatement keyspaceStmt = SchemaBuilder.createKeyspace(this.schema.keyspace)
570+
.ifNotExists()
571+
.withSimpleStrategy(1)
572+
.build();
566573

567-
SimpleStatement keyspaceStmt = SchemaBuilder.createKeyspace(this.schema.keyspace)
568-
.ifNotExists()
569-
.withSimpleStrategy(1)
570-
.build();
571-
572-
logger.debug("Executing {}", keyspaceStmt.getQuery());
573-
this.session.execute(keyspaceStmt);
574+
logger.debug("Executing {}", keyspaceStmt.getQuery());
575+
this.session.execute(keyspaceStmt);
576+
}
574577
}
575578

576579
}

0 commit comments

Comments
 (0)