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

Boolean index changes # #43

Merged
merged 2 commits into from
Jan 24, 2023
Merged
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
26 changes: 26 additions & 0 deletions src/main/java/io/stargate/sgv3/docsapi/StargateDocsV3Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@
}
}
"""),
@ExampleObject(
name = "insertMany",
summary = "`insertMany` command",
value =
"""
{
"insertMany": {
"documents": [{
"_id": "1",
"location": "London",
"race": {
"competitors": 100,
"start_date": "2022-08-15"
}
},
{
"_id": "2",
"location": "New York",
"race": {
"competitors": 150,
"start_date": "2022-09-15"
}
}]
}
}
"""),
@ExampleObject(
name = "createCollection",
summary = "`CreateCollection` command",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.sgv3.docsapi.api.model.command.impl.DeleteOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertManyCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertOneCommand;

/**
Expand Down Expand Up @@ -35,5 +36,6 @@
@JsonSubTypes.Type(value = FindCommand.class),
@JsonSubTypes.Type(value = FindOneCommand.class),
@JsonSubTypes.Type(value = InsertOneCommand.class),
@JsonSubTypes.Type(value = InsertManyCommand.class),
})
public interface Command {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.stargate.sgv3.docsapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv3.docsapi.api.model.command.Command;
import io.stargate.sgv3.docsapi.api.model.command.ModifyCommand;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* Representation of the insertMany API {@link Command}.
*
* @param document The document to insert.
*/
@Schema(description = "Command that inserts multiple JSON document to a collection.")
@JsonTypeName("insertMany")
public record InsertManyCommand(
@NotNull
@Schema(
description = "JSON document to insert.",
implementation = Object.class,
type = SchemaType.ARRAY)
List<JsonNode> documents)
implements ModifyCommand {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.stargate.sgv3.docsapi.api.model.command.impl.DeleteOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertManyCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertOneCommand;
import io.stargate.sgv3.docsapi.config.constants.OpenApiConstants;
import io.stargate.sgv3.docsapi.service.processor.CommandProcessor;
Expand Down Expand Up @@ -66,12 +67,14 @@ public CollectionResource(CommandProcessor commandProcessor) {
DeleteOneCommand.class,
FindOneCommand.class,
FindCommand.class,
InsertOneCommand.class
InsertOneCommand.class,
InsertManyCommand.class
}),
examples = {
@ExampleObject(ref = "findOne"),
@ExampleObject(ref = "find"),
@ExampleObject(ref = "insertOne"),
@ExampleObject(ref = "insertMany"),
@ExampleObject(ref = "deleteOne"),
}))
@APIResponses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static Map<QueryOuterClass.Value, QueryOuterClass.Value> getBooleanMapVal
Map<JsonPath, Boolean> from) {
final Map<QueryOuterClass.Value, QueryOuterClass.Value> to = new HashMap<>(from.size());
for (Map.Entry<JsonPath, Boolean> entry : from.entrySet()) {
to.put(Values.of(entry.getKey().toString()), Values.of(entry.getValue()));
to.put(Values.of(entry.getKey().toString()), Values.of((byte) (entry.getValue() ? 1 : 0)));
}
return to;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected QueryOuterClass.Query getCreateTable(String keyspace, String table) {
+ " array_size map<text, int>,"
+ " array_equals map<text, text>,"
+ " array_contains set<text>,"
+ " query_bool_values map<text, boolean>,"
+ " query_bool_values map<text, tinyint>,"
+ " query_dbl_values map<text, decimal>,"
+ " query_text_values map<text, text>, "
+ " query_null_values set<text>, "
Expand Down Expand Up @@ -101,10 +101,10 @@ protected List<QueryOuterClass.Query> getIndexStatements(String keyspace, String

String boolQuery =
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_bool_values ON %s.%s (entries(query_bool_values)) USING 'StorageAttachedIndex'";
/*statements.add(
QueryOuterClass.Query.newBuilder()
.setCql(String.format(boolQuery, table, keyspace, table))
.build());*/
statements.add(
QueryOuterClass.Query.newBuilder()
.setCql(String.format(boolQuery, table, keyspace, table))
.build());

String dblQuery =
"CREATE CUSTOM INDEX IF NOT EXISTS %s_query_dbl_values ON %s.%s (entries(query_dbl_values)) USING 'StorageAttachedIndex'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public TextFilter(String path, Operator operator, String value) {
}

/** Filters db documents based on a boolean field value */
public static class BoolFilter extends MapFilterBase<Boolean> {
public static class BoolFilter extends MapFilterBase<Byte> {
public BoolFilter(String path, Operator operator, Boolean value) {
super("query_bool_values", path, operator, value);
super("query_bool_values", path, operator, (byte) (value ? 1 : 0));
}
}

Expand Down Expand Up @@ -240,8 +240,8 @@ private static QueryOuterClass.Value getValue(Object value) {
return Values.of((String) value);
} else if (value instanceof BigDecimal) {
return Values.of((BigDecimal) value);
} else if (value instanceof Boolean) {
return Values.of((Boolean) value);
} else if (value instanceof Byte) {
return Values.of((Byte) value);
}
return Values.of((String) null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.stargate.sgv3.docsapi.service.resolver.model.impl;

import io.stargate.sgv3.docsapi.api.model.command.CommandContext;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertManyCommand;
import io.stargate.sgv3.docsapi.service.operation.model.Operation;
import io.stargate.sgv3.docsapi.service.operation.model.impl.InsertOperation;
import io.stargate.sgv3.docsapi.service.resolver.model.CommandResolver;
import io.stargate.sgv3.docsapi.service.shredding.Shredder;
import io.stargate.sgv3.docsapi.service.shredding.model.WritableShreddedDocument;
import java.util.List;
import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/** Resolves the {@link InsertManyCommand}. */
@ApplicationScoped
public class InsertManyCommandResolver implements CommandResolver<InsertManyCommand> {

private final Shredder shredder;

@Inject
public InsertManyCommandResolver(Shredder shredder) {
this.shredder = shredder;
}

@Override
public Class<InsertManyCommand> getCommandClass() {
return InsertManyCommand.class;
}

@Override
public Operation resolveCommand(CommandContext ctx, InsertManyCommand command) {
final List<WritableShreddedDocument> shreddedDocuments =
command.documents().stream().map(doc -> shredder.shred(doc)).collect(Collectors.toList());
return new InsertOperation(ctx, shreddedDocuments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,78 +168,6 @@ public void findOneByColumn() {
}
}

@Nested
class InsertOne {
@Test
public void insertDocument() {
String json =
"""
{
"insertOne": {
"document": {
"_id": "doc3",
"username": "user3"
}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.insertedIds[0]", is("doc3"));
}

@Test
public void emptyDocument() {
String json =
"""
{
"insertOne": {
"document": {
}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200);
}

@Test
public void notValidDocumentMissing() {
String json =
"""
{
"insertOne": {
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("errors[0].message", is(not(blankString())))
.body("errors[0].exceptionClass", is("ConstraintViolationException"));
}
}

@Nested
class ClientErrors {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void setUp() {
"insertOne": {
"document": {
"_id": "doc1",
"username": "user1"
"username": "user1",
"active_user" : true
}
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ public void findById() {
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\"}";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand All @@ -184,7 +185,30 @@ public void findByColumn() {
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\"}";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", jsonEquals(expected));
}

@Test
@Order(2)
public void findByBooleanColumn() {
String json =
"""
{
"find": {
"filter" : {"active_user" : true}
}
}
""";
String expected = "{\"_id\":\"doc1\", \"username\":\"user1\", \"active_user\":true}";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand Down
Loading