feat: adding upsert api with for conditial insert and update#5
Draft
aaron-steinfeld wants to merge 1 commit intocode-review-exercise-basefrom
Draft
feat: adding upsert api with for conditial insert and update#5aaron-steinfeld wants to merge 1 commit intocode-review-exercise-basefrom
aaron-steinfeld wants to merge 1 commit intocode-review-exercise-basefrom
Conversation
aaron-steinfeld
commented
Apr 29, 2021
| * False indicates if the document exists, update it otherwise don't do anything. | ||
| * @return True if success. False otherwise. | ||
| */ | ||
| boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) |
Owner
Author
There was a problem hiding this comment.
Final args (could be at impl level)
Owner
Author
There was a problem hiding this comment.
Documentation is unclear
| } | ||
|
|
||
| return writeResult.getModifiedCount() > 0; | ||
| return (writeResult.getUpsertedId() != null || writeResult.getModifiedCount() > 0); |
Owner
Author
There was a problem hiding this comment.
Null check on write result to be defensive
| public boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) | ||
| throws IOException { | ||
| try { | ||
| Map<String, Object> map = parseQuery(condition); |
| UpdateOptions options = new UpdateOptions().upsert(isUpsert != null ? isUpsert : true); | ||
| UpdateResult writeResult = | ||
| collection.updateOne(conditionObject, this.prepareUpsertWithCondition(key, document), options); | ||
| if (LOGGER.isDebugEnabled()) { |
Owner
Author
There was a problem hiding this comment.
This isn't needed
| map.put(ID_KEY, key.toString()); | ||
| BasicDBObject conditionObject = new BasicDBObject(map); | ||
| UpdateOptions options = new UpdateOptions().upsert(isUpsert != null ? isUpsert : true); | ||
| UpdateResult writeResult = |
| * parameter for explicitly controlling insert and update. | ||
| * */ | ||
| @Override | ||
| public boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) |
Owner
Author
There was a problem hiding this comment.
Can any of these args be null? check them
| return this.dbObjectToDocument(upsertResult); | ||
| } | ||
|
|
||
| private BasicDBObject prepareUpsertWithCondition(Key key, Document document) throws JsonProcessingException { |
Owner
Author
There was a problem hiding this comment.
method level comments
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.