Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #158: support empty Projection clause, validate non-empty #284

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.stargate.sgv2.jsonapi.api.model.command;

import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv2.jsonapi.service.projection.DocumentProjector;

/*
* All the commands that need Projection definitions will have to implement this.
*/
public interface Projectable {
JsonNode projectionDefinition();

default DocumentProjector buildProjector() {
return DocumentProjector.createFromDefinition(projectionDefinition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv2.jsonapi.api.model.command.Filterable;
import io.stargate.sgv2.jsonapi.api.model.command.ReadCommand;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.FilterClause;
Expand All @@ -15,6 +16,7 @@
@JsonTypeName("find")
public record FindCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@JsonProperty("projection") JsonNode projectionDefinition,
@Valid @JsonProperty("sort") SortClause sortClause,
@Valid @Nullable Options options)
implements ReadCommand, Filterable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv2.jsonapi.api.model.command.Filterable;
import io.stargate.sgv2.jsonapi.api.model.command.ReadCommand;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.FilterClause;
Expand All @@ -18,6 +19,7 @@
@JsonTypeName("findOneAndUpdate")
public record FindOneAndUpdateCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@JsonProperty("projection") JsonNode projectionDefinition,
@NotNull @Valid @JsonProperty("update") UpdateClause updateClause,
@Valid @Nullable Options options)
implements ReadCommand, Filterable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv2.jsonapi.api.model.command.Filterable;
import io.stargate.sgv2.jsonapi.api.model.command.ReadCommand;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.FilterClause;
Expand All @@ -13,5 +14,6 @@
@JsonTypeName("findOne")
public record FindOneCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@JsonProperty("projection") JsonNode projectionDefinition,
@Valid @JsonProperty("sort") SortClause sortClause)
implements ReadCommand, Filterable {}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public enum ErrorCode {

UNSUPPORTED_OPERATION("Unsupported operation class"),

UNSUPPORTED_PROJECTION_PARAM("Unsupported projection parameter"),

UNSUPPORTED_UPDATE_DATA_TYPE("Unsupported update data type"),

UNSUPPORTED_UPDATE_OPERATION("Unsupported update operation"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import io.stargate.sgv2.jsonapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
import io.stargate.sgv2.jsonapi.service.projection.DocumentProjector;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/** Operation that returns the documents or its key based on the filter condition. */
Expand All @@ -28,6 +30,7 @@ public record FindOperation(
int limit,
int pageSize,
ReadType readType,
Optional<DocumentProjector> projector,
ObjectMapper objectMapper)
implements ReadOperation {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.stargate.sgv2.jsonapi.service.projection;

import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;

/**
* Helper class that implements functionality needed to support projections on documents fetched via
* various {@code find} commands.
*/
public class DocumentProjector {
private DocumentProjector() {}

public static DocumentProjector createFromDefinition(JsonNode projectionDefinition) {
if (projectionDefinition == null) {
return null;
}
if (!projectionDefinition.isObject()) {
throw new JsonApiException(
ErrorCode.UNSUPPORTED_PROJECTION_PARAM,
ErrorCode.UNSUPPORTED_PROJECTION_PARAM.getMessage()
+ ": definition must be OBJECT, was "
+ projectionDefinition.getNodeType());
}
if (projectionDefinition.isEmpty()) {
return null;
}
return new DocumentProjector();
}

public void applyProjection(JsonNode document) {
; // To implement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -58,6 +59,7 @@ private FindOperation getFindOperation(CommandContext commandContext, DeleteMany
documentConfig.maxDocumentDeleteCount() + 1,
documentConfig.defaultPageSize(),
ReadType.KEY,
Optional.empty(),
objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -46,6 +47,7 @@ public Class<DeleteOneCommand> getCommandClass() {

private FindOperation getFindOperation(CommandContext commandContext, DeleteOneCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
return new FindOperation(commandContext, filters, null, 1, 1, ReadType.KEY, objectMapper);
return new FindOperation(
commandContext, filters, null, 1, 1, ReadType.KEY, Optional.empty(), objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -45,6 +46,13 @@ public Operation resolveCommand(CommandContext commandContext, FindCommand comma
int pageSize = documentConfig.defaultPageSize();
String pagingState = command.options() != null ? command.options().pagingState() : null;
return new FindOperation(
commandContext, filters, pagingState, limit, pageSize, ReadType.DOCUMENT, objectMapper);
commandContext,
filters,
pagingState,
limit,
pageSize,
ReadType.DOCUMENT,
Optional.empty(),
objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.stargate.sgv2.jsonapi.service.shredding.Shredder;
import io.stargate.sgv2.jsonapi.service.updater.DocumentUpdater;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -67,6 +68,7 @@ public Operation resolveCommand(CommandContext commandContext, FindOneAndUpdateC
private FindOperation getFindOperation(
CommandContext commandContext, FindOneAndUpdateCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
return new FindOperation(commandContext, filters, null, 1, 1, ReadType.DOCUMENT, objectMapper);
return new FindOperation(
commandContext, filters, null, 1, 1, ReadType.DOCUMENT, Optional.empty(), objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -34,6 +35,7 @@ public Class<FindOneCommand> getCommandClass() {
public Operation resolveCommand(CommandContext commandContext, FindOneCommand command) {

List<DBFilterBase> filters = resolve(commandContext, command);
return new FindOperation(commandContext, filters, null, 1, 1, ReadType.DOCUMENT, objectMapper);
return new FindOperation(
commandContext, filters, null, 1, 1, ReadType.DOCUMENT, Optional.empty(), objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.stargate.sgv2.jsonapi.service.shredding.Shredder;
import io.stargate.sgv2.jsonapi.service.updater.DocumentUpdater;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -71,6 +72,7 @@ private FindOperation getFindOperation(CommandContext commandContext, UpdateMany
documentConfig.maxDocumentUpdateCount() + 1,
documentConfig.defaultPageSize(),
ReadType.DOCUMENT,
Optional.empty(),
objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.stargate.sgv2.jsonapi.service.shredding.Shredder;
import io.stargate.sgv2.jsonapi.service.updater.DocumentUpdater;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand Down Expand Up @@ -64,6 +65,7 @@ public Operation resolveCommand(CommandContext commandContext, UpdateOneCommand

private FindOperation getFindOperation(CommandContext commandContext, UpdateOneCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
return new FindOperation(commandContext, filters, null, 1, 1, ReadType.DOCUMENT, objectMapper);
return new FindOperation(
commandContext, filters, null, 1, 1, ReadType.DOCUMENT, Optional.empty(), objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import io.stargate.sgv2.jsonapi.api.model.command.Command;
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.Filterable;
import io.stargate.sgv2.jsonapi.api.model.command.Projectable;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.ArrayComparisonOperator;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.ElementComparisonOperator;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.JsonType;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.ValueComparisonOperator;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.projection.DocumentProjector;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocValueHasher;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.inject.Inject;

/**
Expand Down Expand Up @@ -83,6 +86,13 @@ protected List<DBFilterBase> resolve(CommandContext commandContext, T command) {
return matchRules.apply(commandContext, command);
}

protected Optional<DocumentProjector> getProjector(T command) {
if (command instanceof Projectable) {
return Optional.ofNullable(((Projectable) command).buildProjector());
}
return Optional.empty();
}

private List<DBFilterBase> findById(CommandContext commandContext, CaptureGroups<T> captures) {
List<DBFilterBase> filters = new ArrayList<>();
final CaptureGroup<DocumentId> idGroup =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.stargate.sgv2.jsonapi.service.shredding.model.DocValueHasher;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
import javax.inject.Inject;
Expand Down Expand Up @@ -94,6 +95,7 @@ public void deleteWithId() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 3);
Supplier<CommandResult> execute =
Expand Down Expand Up @@ -146,6 +148,7 @@ public void deleteWithIdNoData() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);

DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 3);
Expand Down Expand Up @@ -215,6 +218,7 @@ public void deleteWithDynamic() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 3);

Expand Down Expand Up @@ -321,6 +325,7 @@ public void deleteWithDynamicRetry() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 2);

Expand Down Expand Up @@ -429,6 +434,7 @@ public void deleteWithDynamicRetryFailure() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 2);

Expand Down Expand Up @@ -532,6 +538,7 @@ public void deleteWithDynamicRetryConcurrentDelete() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 2);

Expand Down Expand Up @@ -617,6 +624,7 @@ public void deleteManyWithDynamic() {
3,
2,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 2, 3);

Expand Down Expand Up @@ -700,6 +708,7 @@ public void deleteManyWithDynamicPaging() {
3,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 2, 3);

Expand Down Expand Up @@ -789,6 +798,7 @@ public void deleteManyWithDynamicPagingAndMoreData() {
3,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 2, 3);

Expand Down Expand Up @@ -849,6 +859,7 @@ public void deleteWithNoResult() {
1,
1,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 1, 3);

Expand Down Expand Up @@ -969,6 +980,7 @@ public void errorPartial() {
3,
3,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 2, 3);

Expand Down Expand Up @@ -1139,6 +1151,7 @@ public void errorAll() {
3,
3,
ReadType.KEY,
Optional.empty(),
objectMapper);
DeleteOperation operation = new DeleteOperation(COMMAND_CONTEXT, findOperation, 2, 3);

Expand Down
Loading