Skip to content

Commit

Permalink
fix for createCollection
Browse files Browse the repository at this point in the history
and other commands that run against the namespace

FindIntegrationTest works, waiting to find out about others
  • Loading branch information
amorton committed Jul 8, 2024
1 parent 045fe36 commit 4681ccd
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,33 @@ public class KeyspaceSchemaObject extends SchemaObject {
new KeyspaceSchemaObject(SchemaObjectName.MISSING);

public KeyspaceSchemaObject(String keyspace) {
this(new SchemaObjectName(keyspace, SchemaObjectName.MISSING_NAME));
this(newObjectName(keyspace));
}

public KeyspaceSchemaObject(SchemaObjectName name) {
super(TYPE, name);
}

/**
* Construct a {@link KeyspaceSchemaObject} that represents the keyspace the collection is in.
*
* @param collection
* @return
*/
public static KeyspaceSchemaObject fromSchemaObject(CollectionSchemaObject collection) {
return new KeyspaceSchemaObject(newObjectName(collection.name.keyspace()));
}

/**
* Construct a {@link KeyspaceSchemaObject} that represents the keyspace the collection is in.
*
* @param table
* @return
*/
public static KeyspaceSchemaObject fromSchemaObject(TableSchemaObject table) {
return new KeyspaceSchemaObject(newObjectName(table.name.keyspace()));
}

@Override
public VectorConfig vectorConfig() {
return VectorConfig.notEnabledVectorConfig();
Expand All @@ -25,4 +45,15 @@ public VectorConfig vectorConfig() {
public IndexUsage newIndexUsage() {
return IndexUsage.NO_OP;
}

/**
* Centralised creation of the name for a Keyspace so we always use the correct marker object for
* collection name
*
* @param keyspaceName
* @return
*/
private static SchemaObjectName newObjectName(String keyspaceName) {
return new SchemaObjectName(keyspaceName, SchemaObjectName.MISSING_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.cqldriver.CQLSessionCache;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.schema.model.JsonapiTableMatcher;
Expand All @@ -31,7 +32,7 @@
import org.slf4j.LoggerFactory;

public record CreateCollectionOperation(
CommandContext<CollectionSchemaObject> commandContext,
CommandContext<KeyspaceSchemaObject> commandContext,
DatabaseLimitsConfig dbLimitsConfig,
ObjectMapper objectMapper,
CQLSessionCache cqlSessionCache,
Expand All @@ -51,7 +52,7 @@ public record CreateCollectionOperation(
private static final JsonapiTableMatcher COLLECTION_MATCHER = new JsonapiTableMatcher();

public static CreateCollectionOperation withVectorSearch(
CommandContext<CollectionSchemaObject> commandContext,
CommandContext<KeyspaceSchemaObject> commandContext,
DatabaseLimitsConfig dbLimitsConfig,
ObjectMapper objectMapper,
CQLSessionCache cqlSessionCache,
Expand All @@ -78,7 +79,7 @@ public static CreateCollectionOperation withVectorSearch(
}

public static CreateCollectionOperation withoutVectorSearch(
CommandContext<CollectionSchemaObject> commandContext,
CommandContext<KeyspaceSchemaObject> commandContext,
DatabaseLimitsConfig dbLimitsConfig,
ObjectMapper objectMapper,
CQLSessionCache cqlSessionCache,
Expand Down Expand Up @@ -308,6 +309,7 @@ private Multi<AsyncResultSet> createIndexParallel(

public Uni<JsonApiException> cleanUpCollectionFailedWithTooManyIndex(
DataApiRequestInfo dataApiRequestInfo, QueryExecutor queryExecutor) {

DeleteCollectionOperation deleteCollectionOperation =
new DeleteCollectionOperation(commandContext, name);
return deleteCollectionOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.api.request.DataApiRequestInfo;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import java.util.function.Supplier;
Expand All @@ -18,7 +18,7 @@
* @param context Command context, carries namespace of the collection.
* @param name Collection name.
*/
public record DeleteCollectionOperation(CommandContext<CollectionSchemaObject> context, String name)
public record DeleteCollectionOperation(CommandContext<KeyspaceSchemaObject> context, String name)
implements Operation {
private static final Logger logger = LoggerFactory.getLogger(DeleteCollectionOperation.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.cqldriver.CQLSessionCache;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.schema.model.JsonapiTableMatcher;
Expand All @@ -35,17 +36,19 @@ public record FindCollectionsOperation(
ObjectMapper objectMapper,
CQLSessionCache cqlSessionCache,
JsonapiTableMatcher tableMatcher,
CommandContext<CollectionSchemaObject> commandContext)
CommandContext<KeyspaceSchemaObject> commandContext)
implements Operation {

// shared table matcher instance
// TODO: if this is static why does the record that have an instance variable passed by the ctor
// below ?
private static final JsonapiTableMatcher TABLE_MATCHER = new JsonapiTableMatcher();

public FindCollectionsOperation(
boolean explain,
ObjectMapper objectMapper,
CQLSessionCache cqlSessionCache,
CommandContext<CollectionSchemaObject> commandContext) {
CommandContext<KeyspaceSchemaObject> commandContext) {
this(explain, objectMapper, cqlSessionCache, TABLE_MATCHER, commandContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ default Operation resolveCollectionCommand(
// commands are tested well
throw new UnsupportedOperationException(
String.format(
"%s Command does not support operating on Collectons, target was %s",
"%s Command does not support operating on Collections, target was %s",
command.getClass().getSimpleName(), ctx.schemaObject().name));
}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.cqldriver.CQLSessionCache;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.embedding.configuration.EmbeddingProvidersConfig;
import io.stargate.sgv2.jsonapi.service.embedding.configuration.ProviderConstants;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
Expand Down Expand Up @@ -65,8 +65,8 @@ public Class<CreateCollectionCommand> getCommandClass() {
}

@Override
public Operation resolveCollectionCommand(
CommandContext<CollectionSchemaObject> ctx, CreateCollectionCommand command) {
public Operation resolveKeyspaceCommand(
CommandContext<KeyspaceSchemaObject> ctx, CreateCollectionCommand command) {
if (command.options() == null) {
return CreateCollectionOperation.withoutVectorSearch(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteCollectionCommand;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.collections.DeleteCollectionOperation;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
Expand All @@ -17,8 +17,8 @@ public Class<DeleteCollectionCommand> getCommandClass() {
}

@Override
public Operation resolveCollectionCommand(
CommandContext<CollectionSchemaObject> ctx, DeleteCollectionCommand command) {
public Operation resolveKeyspaceCommand(
CommandContext<KeyspaceSchemaObject> ctx, DeleteCollectionCommand command) {
return new DeleteCollectionOperation(ctx, command.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCollectionsCommand;
import io.stargate.sgv2.jsonapi.service.cqldriver.CQLSessionCache;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.collections.FindCollectionsOperation;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
Expand Down Expand Up @@ -32,8 +32,9 @@ public Class<FindCollectionsCommand> getCommandClass() {

/** {@inheritDoc} */
@Override
public Operation resolveCollectionCommand(
CommandContext<CollectionSchemaObject> ctx, FindCollectionsCommand command) {
public Operation resolveKeyspaceCommand(
CommandContext<KeyspaceSchemaObject> ctx, FindCollectionsCommand command) {

boolean explain = command.options() != null ? command.options().explain() : false;
return new FindCollectionsOperation(explain, objectMapper, cqlSessionCache, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.request.FileWriterParams;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.KeyspaceSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.VectorConfig;
import io.stargate.sgv2.jsonapi.service.operation.model.collections.CreateCollectionOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.collections.InsertOperation;
Expand Down Expand Up @@ -153,9 +154,16 @@ private FileWriterParams buildFileWriterParams(CollectionSchemaObject collection
boolean hasIndexing = indexingConfig != null;
boolean hasVector = vectorSearchConfig != null;

var commandContext =
var tableCommandContext =
CommandContext.forSchemaObject(collectionObject, null, this.createCollection.name(), null);

var keyspaceCommandContext =
CommandContext.forSchemaObject(
KeyspaceSchemaObject.fromSchemaObject(collectionObject),
null,
this.createCollection.name(),
null);

String comment =
CreateCollectionCommandResolver.generateComment(
new ObjectMapper(),
Expand All @@ -168,7 +176,7 @@ private FileWriterParams buildFileWriterParams(CollectionSchemaObject collection
CreateCollectionOperation createCollectionOperation =
hasVector
? CreateCollectionOperation.withVectorSearch(
commandContext,
keyspaceCommandContext,
null,
new ObjectMapper(),
null,
Expand All @@ -182,7 +190,7 @@ private FileWriterParams buildFileWriterParams(CollectionSchemaObject collection
false,
false)
: CreateCollectionOperation.withoutVectorSearch(
commandContext,
keyspaceCommandContext,
null,
new ObjectMapper(),
null,
Expand All @@ -202,7 +210,7 @@ private FileWriterParams buildFileWriterParams(CollectionSchemaObject collection
.map(SimpleStatement::getQuery)
.toList();
InsertOperation insertOperation =
InsertOperation.create(commandContext, List.of(), true, true, false);
InsertOperation.create(tableCommandContext, List.of(), true, true, false);
String insertStatementCQL = insertOperation.buildInsertQuery(hasVector);
return new FileWriterParams(
this.namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void countWithNoFilter() {
return CompletableFuture.supplyAsync(() -> mockResults).minimalCompletionStage();
});

CountOperation countOperation = new CountOperation(CONTEXT, LogicalExpression.and(), 100, -1);
CountOperation countOperation =
new CountOperation(COLLECTION_CONTEXT, LogicalExpression.and(), 100, -1);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -113,7 +114,7 @@ public void countWithDynamic() {
.setDBFilters(
List.of(
new TextCollectionFilter("username", MapCollectionFilter.Operator.EQ, "user1")));
CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, -1);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, -1);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -164,7 +165,7 @@ public void countWithDynamicNoMatch() {
new TextCollectionFilter(
"username", MapCollectionFilter.Operator.EQ, "user_all")));

CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, -1);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, -1);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -203,7 +204,7 @@ public void error() {
});

LogicalExpression implicitAnd = LogicalExpression.and();
CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, -1);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, -1);
Throwable result =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -247,7 +248,8 @@ public void countWithNoFilter() {
return CompletableFuture.supplyAsync(() -> mockResults).minimalCompletionStage();
});

CountOperation countOperation = new CountOperation(CONTEXT, LogicalExpression.and(), 100, 10);
CountOperation countOperation =
new CountOperation(COLLECTION_CONTEXT, LogicalExpression.and(), 100, 10);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -295,7 +297,7 @@ public void countWithDynamic() {
.setDBFilters(
List.of(
new TextCollectionFilter("username", MapCollectionFilter.Operator.EQ, "user2")));
CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, 10);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, 10);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -345,7 +347,7 @@ public void countWithDynamicNoMatch() {
new TextCollectionFilter(
"username", MapCollectionFilter.Operator.EQ, "user_all")));

CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, 10);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, 10);
Supplier<CommandResult> execute =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down Expand Up @@ -384,7 +386,7 @@ public void error() {
});

LogicalExpression implicitAnd = LogicalExpression.and();
CountOperation countOperation = new CountOperation(CONTEXT, implicitAnd, 100, 10);
CountOperation countOperation = new CountOperation(COLLECTION_CONTEXT, implicitAnd, 100, 10);
Throwable result =
countOperation
.execute(dataApiRequestInfo, queryExecutor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void createCollectionNoVector() {

CreateCollectionOperation operation =
CreateCollectionOperation.withoutVectorSearch(
COMMAND_CONTEXT,
KEYSPACE_CONTEXT,
databaseLimitsConfig,
objectMapper,
sessionCache,
Expand Down Expand Up @@ -161,7 +161,7 @@ public void createCollectionVector() {

CreateCollectionOperation operation =
CreateCollectionOperation.withVectorSearch(
COMMAND_CONTEXT,
KEYSPACE_CONTEXT,
databaseLimitsConfig,
objectMapper,
sessionCache,
Expand Down Expand Up @@ -221,7 +221,7 @@ public void denyAllCollectionNoVector() {

CreateCollectionOperation operation =
CreateCollectionOperation.withoutVectorSearch(
COMMAND_CONTEXT,
KEYSPACE_CONTEXT,
databaseLimitsConfig,
objectMapper,
sessionCache,
Expand Down Expand Up @@ -279,7 +279,7 @@ public void denyAllCollectionVector() {

CreateCollectionOperation operation =
CreateCollectionOperation.withVectorSearch(
COMMAND_CONTEXT,
KEYSPACE_CONTEXT,
databaseLimitsConfig,
objectMapper,
sessionCache,
Expand Down Expand Up @@ -364,7 +364,7 @@ public void indexAlreadyDropTable() {

CreateCollectionOperation operation =
CreateCollectionOperation.withoutVectorSearch(
COMMAND_CONTEXT,
KEYSPACE_CONTEXT,
databaseLimitsConfig,
objectMapper,
sessionCache,
Expand Down
Loading

0 comments on commit 4681ccd

Please sign in to comment.