Skip to content

feat: adding upsert api with for conditial insert and update#5

Draft
aaron-steinfeld wants to merge 1 commit intocode-review-exercise-basefrom
code-review-exercise-april-29
Draft

feat: adding upsert api with for conditial insert and update#5
aaron-steinfeld wants to merge 1 commit intocode-review-exercise-basefrom
code-review-exercise-april-29

Conversation

@aaron-steinfeld
Copy link
Copy Markdown
Owner

No description provided.

* 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)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final args (could be at impl level)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation is unclear

}

return writeResult.getModifiedCount() > 0;
return (writeResult.getUpsertedId() != null || writeResult.getModifiedCount() > 0);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null check

UpdateOptions options = new UpdateOptions().upsert(isUpsert != null ? isUpsert : true);
UpdateResult writeResult =
collection.updateOne(conditionObject, this.prepareUpsertWithCondition(key, document), options);
if (LOGGER.isDebugEnabled()) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 =
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null check

* parameter for explicitly controlling insert and update.
* */
@Override
public boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any of these args be null? check them

return this.dbObjectToDocument(upsertResult);
}

private BasicDBObject prepareUpsertWithCondition(Key key, Document document) throws JsonProcessingException {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method level comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant