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

Implementation for findOneAndReplace command #324

Merged
merged 7 commits into from
Mar 30, 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
207 changes: 124 additions & 83 deletions src/main/java/io/stargate/sgv2/jsonapi/StargateJsonApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@
}
}
"""),
@ExampleObject(
name = "findOneAndReplace",
summary = "`findOneAndReplace` command",
value =
"""
{
"findOneAndReplace": {
"filter": {"location": "London"},
"sort" : ["race.start_date"]
"replacement": {
"location": "New York",
"count": 3
},
"options" : {
"returnDocument" : "before"
}
}
}
"""),
@ExampleObject(
name = "updateOne",
summary = "`updateOne` command",
Expand All @@ -138,40 +157,40 @@
summary = "`updateMany` command",
value =
"""
{
"updateMany": {
"filter": {"location": "London"},
"update": {
"$set": {"location": "New York"},
"$push": {"tags": "marathon"}
},
"options" : {
"upsert" : true
}
}
}
"""),
{
"updateMany": {
"filter": {"location": "London"},
"update": {
"$set": {"location": "New York"},
"$push": {"tags": "marathon"}
},
"options" : {
"upsert" : true
}
}
}
"""),
@ExampleObject(
name = "deleteOne",
summary = "`deleteOne` command",
value =
"""
{
"deleteOne": {
"filter": {"_id": "1"}
}
}
"""),
{
"deleteOne": {
"filter": {"_id": "1"}
}
}
"""),
@ExampleObject(
name = "deleteMany",
summary = "`deleteMany` command",
value =
"""
{
"deleteMany": {
"filter": {"location": "London"}
}
}
{
"deleteMany": {
"filter": {"location": "London"}
}
}
"""),
@ExampleObject(
name = "insertOne",
Expand All @@ -197,95 +216,95 @@
summary = "`insertMany` command",
value =
"""
{
"insertMany": {
"documents": [
{
"_id": "1",
"location": "London",
"race": {
"competitors": 100,
"start_date": "2022-08-15"
},
"tags" : [ "eu" ]
},
{
"_id": "2",
"location": "New York",
"race": {
"competitors": 150,
"start_date": "2022-09-15"
},
"tags": [ "us" ]
}
],
"options": {
"ordered": true
}
}
}
"""),
{
"insertMany": {
"documents": [
{
"_id": "1",
"location": "London",
"race": {
"competitors": 100,
"start_date": "2022-08-15"
},
"tags" : [ "eu" ]
},
{
"_id": "2",
"location": "New York",
"race": {
"competitors": 150,
"start_date": "2022-09-15"
},
"tags": [ "us" ]
}
],
"options": {
"ordered": true
}
}
}
"""),
@ExampleObject(
name = "createNamespace",
summary = "`CreateNamespace` command",
value =
"""
{
"createNamespace": {
"name": "cycling"
}
{
"createNamespace": {
"name": "cycling"
}
"""),
}
"""),
@ExampleObject(
name = "createNamespaceWithReplication",
summary = "`CreateNamespace` command with replication",
value =
"""
{
"createNamespace": {
"name": "cycling",
"options": {
"replication": {
"class": "SimpleStrategy",
"replication_factor": 3
}
}
{
"createNamespace": {
"name": "cycling",
"options": {
"replication": {
"class": "SimpleStrategy",
"replication_factor": 3
}
}
}
"""),
}
"""),
@ExampleObject(
name = "createCollection",
summary = "`CreateCollection` command",
value =
"""
{
"createCollection": {
"name": "events"
}
{
"createCollection": {
"name": "events"
}
"""),
}
"""),
@ExampleObject(
name = "deleteCollection",
summary = "`DeleteCollection` command",
value =
"""
{
"deleteCollection": {
"name": "events"
}
{
"deleteCollection": {
"name": "events"
}
"""),
}
"""),
@ExampleObject(
name = "resultCount",
summary = "`countDocuments` command result",
value =
"""
{
"status": {
"count": 2
}
}
"""),
{
"status": {
"count": 2
}
}
"""),
@ExampleObject(
name = "resultFind",
summary = "`find` command result",
Expand Down Expand Up @@ -366,6 +385,28 @@
}
}
"""),
@ExampleObject(
name = "resultFindOneAndReplace",
summary = "`findOneAndReplace` command result",
value =
"""
{
"data": {
"docs": [
{
"_id": "1",
"location": "New York",
"count": 3
}
],
"count": 1,
"status": {
"matchedCount": 1,
"modifiedCount": 1
}
}
}
"""),
@ExampleObject(
name = "resultFindOneAndUpdateUpsert",
summary = "`findOneAndUpdate` command with upsert result",
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.impl.DeleteManyCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteOneCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneAndReplaceCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneAndUpdateCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.InsertManyCommand;
Expand Down Expand Up @@ -46,6 +47,7 @@
@JsonSubTypes.Type(value = DeleteManyCommand.class),
@JsonSubTypes.Type(value = FindCommand.class),
@JsonSubTypes.Type(value = FindOneCommand.class),
@JsonSubTypes.Type(value = FindOneAndReplaceCommand.class),
@JsonSubTypes.Type(value = FindOneAndUpdateCommand.class),
@JsonSubTypes.Type(value = InsertOneCommand.class),
@JsonSubTypes.Type(value = InsertManyCommand.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.stargate.sgv2.jsonapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.ReadCommand;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.SortClause;
import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Schema(
description =
"Command that finds a single JSON document from a collection and replaces it with the replacement document.")
@JsonTypeName("findOneAndReplace")
public record FindOneAndReplaceCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@Valid @JsonProperty("sort") SortClause sortClause,
@JsonProperty("projection") JsonNode projectionDefinition,
@NotNull @Valid @JsonProperty("replacement") ObjectNode replacementDocument,
@Valid @Nullable Options options)
implements ReadCommand, Filterable, Projectable {

@Schema(
name = "FindOneAndReplaceCommand.Options",
description = "Options for `findOneAndReplace` command.")
public record Options(
@Nullable
@Pattern(
regexp = "(after|before)",
message = "returnDocument value can only be 'before' or 'after'")
@Schema(
description =
"Specifies which document to perform the projection on. If `before` the projection is performed on the document before the replacement is applied, if `after` the document projection is from the document after the replacement.",
defaultValue = "before")
String returnDocument) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteManyCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteOneCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneAndReplaceCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneAndUpdateCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.InsertManyCommand;
Expand Down Expand Up @@ -74,6 +75,7 @@ public CollectionResource(CommandProcessor commandProcessor) {
DeleteManyCommand.class,
FindOneCommand.class,
FindCommand.class,
FindOneAndReplaceCommand.class,
FindOneAndUpdateCommand.class,
InsertOneCommand.class,
InsertManyCommand.class,
Expand All @@ -86,6 +88,7 @@ public CollectionResource(CommandProcessor commandProcessor) {
@ExampleObject(ref = "deleteMany"),
@ExampleObject(ref = "find"),
@ExampleObject(ref = "findOne"),
@ExampleObject(ref = "findOneAndReplace"),
@ExampleObject(ref = "findOneAndUpdate"),
@ExampleObject(ref = "insertOne"),
@ExampleObject(ref = "insertMany"),
Expand All @@ -107,6 +110,7 @@ public CollectionResource(CommandProcessor commandProcessor) {
@ExampleObject(ref = "resultDeleteMany"),
@ExampleObject(ref = "resultFind"),
@ExampleObject(ref = "resultFindOne"),
@ExampleObject(ref = "resultFindOneAndReplace"),
@ExampleObject(ref = "resultFindOneAndUpdate"),
@ExampleObject(ref = "resultFindOneAndUpdateUpsert"),
@ExampleObject(ref = "resultInsert"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum ErrorCode {

DOCUMENT_UNPARSEABLE("Unable to parse the document"),

DOCUMENT_REPLACE_DIFFERENT_DOCID(
"The replace document and document resolved using filter have different _id"),

FILTER_UNRESOLVABLE("Unable to resolve the filter"),

SHRED_BAD_DOCUMENT_TYPE("Bad document type to shred"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private Uni<UpdatedDocument> processUpdate(
// apply document updates
// if no changes return null item
DocumentUpdater.DocumentUpdaterResponse documentUpdaterResponse =
documentUpdater().applyUpdates(readDocument.document().deepCopy(), upsert);
documentUpdater().apply(readDocument.document().deepCopy(), upsert);
if (!documentUpdaterResponse.modified()) {
return Uni.createFrom().nullItem();
}
Expand Down
Loading