-
Notifications
You must be signed in to change notification settings - Fork 16
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
UpdateOneCommand implementation. #50
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/io/stargate/sgv3/docsapi/api/model/command/impl/UpdateOneCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.stargate.sgv3.docsapi.api.model.command.Filterable; | ||
import io.stargate.sgv3.docsapi.api.model.command.ReadCommand; | ||
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause; | ||
import io.stargate.sgv3.docsapi.api.model.command.clause.update.UpdateClause; | ||
import javax.validation.Valid; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
@Schema( | ||
description = | ||
"Command that finds a single JSON document from a collection and updates the value provided in the update clause.") | ||
@JsonTypeName("updateOne") | ||
public record UpdateOneCommand( | ||
@Valid @JsonProperty("filter") FilterClause filterClause, | ||
@Valid @JsonProperty("update") UpdateClause updateClause) | ||
implements ReadCommand, Filterable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...n/java/io/stargate/sgv3/docsapi/service/resolver/model/impl/UpdateOneCommandResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.stargate.sgv3.docsapi.service.resolver.model.impl; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.stargate.sgv3.docsapi.api.model.command.CommandContext; | ||
import io.stargate.sgv3.docsapi.api.model.command.impl.UpdateOneCommand; | ||
import io.stargate.sgv3.docsapi.service.operation.model.Operation; | ||
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation; | ||
import io.stargate.sgv3.docsapi.service.operation.model.impl.ReadAndUpdateOperation; | ||
import io.stargate.sgv3.docsapi.service.resolver.model.CommandResolver; | ||
import io.stargate.sgv3.docsapi.service.resolver.model.impl.matcher.FilterableResolver; | ||
import io.stargate.sgv3.docsapi.service.shredding.Shredder; | ||
import io.stargate.sgv3.docsapi.service.updater.DocumentUpdater; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
/** Resolves the {@link UpdateOneCommand } */ | ||
@ApplicationScoped | ||
public class UpdateOneCommandResolver extends FilterableResolver<UpdateOneCommand> | ||
implements CommandResolver<UpdateOneCommand> { | ||
private Shredder shredder; | ||
|
||
@Inject | ||
public UpdateOneCommandResolver(ObjectMapper objectMapper, Shredder shredder) { | ||
super(objectMapper, true, true); | ||
this.shredder = shredder; | ||
} | ||
|
||
@Override | ||
public Class<UpdateOneCommand> getCommandClass() { | ||
return UpdateOneCommand.class; | ||
} | ||
|
||
@Override | ||
public Operation resolveCommand(CommandContext ctx, UpdateOneCommand command) { | ||
ReadOperation readOperation = resolve(ctx, command); | ||
DocumentUpdater documentUpdater = new DocumentUpdater(command.updateClause()); | ||
return new ReadAndUpdateOperation(ctx, readOperation, documentUpdater, false, shredder); | ||
} | ||
|
||
@Override | ||
protected FilteringOptions getFilteringOption(UpdateOneCommand command) { | ||
return new FilteringOptions(1, null, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems different from what Mongo returns as per https://www.mongodb.com/docs/manual/reference/method/db.collection.updateOne/, which says
we can talk about this tomorrow, change in follow-up PR if needs changing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. The distinction between
matchedCount
andmodifiedCount
is somewhat important as well depending on how you've implementedupdatedIds
.matchedCount
is the number of documents that matched the filter. 0 or 1 forupdateOne
. AndmodifiedCount
is the number of documents that changed.You may have a case where
matchedCount = 1
butmodifiedCount = 0
if either (1) no properties in the document changed, for example$set: { answer: 42 }
when answer is already 42, or (2) update operators didn't apply, for example$setOnInsert: { answer: 42 }
if no upsert occurred.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on details wrt
modifiedCount
. This needs to be considered on server side, so that update operations need to indicate if any changes were made.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also advise against using
updatedIds
because that might mean we need to have separate return values forupdateOne()
andupdateMany()
.updateMany()
may update millions of documents - sending all those ids over the network may be extremely slow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkarpov15 Are you suggesting not to return updateIds for updateOne() also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. For the sake of consistency between
updateOne()
andupdateMany()
.